Add: embed web files
This commit is contained in:
14
main.go
14
main.go
@@ -1,8 +1,10 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"io/fs"
|
||||
"log"
|
||||
"msw-open-music/pkg/api"
|
||||
"msw-open-music/pkg/commonconfig"
|
||||
@@ -15,11 +17,21 @@ func init() {
|
||||
flag.StringVar(&ConfigFilePath, "config", "config.json", "backend config file path")
|
||||
}
|
||||
|
||||
//go:embed web/build
|
||||
var WEBFILES embed.FS
|
||||
|
||||
func main() {
|
||||
var err error
|
||||
flag.Parse()
|
||||
|
||||
config := commonconfig.Config{}
|
||||
WEBFS, err := fs.Sub(WEBFILES, "web/build")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
config := commonconfig.Config{
|
||||
WEBFS: WEBFS,
|
||||
}
|
||||
configFile, err := os.Open(ConfigFilePath)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"github.com/gorilla/sessions"
|
||||
"msw-open-music/pkg/commonconfig"
|
||||
"msw-open-music/pkg/database"
|
||||
"msw-open-music/pkg/tmpfs"
|
||||
"net/http"
|
||||
|
||||
"github.com/gorilla/sessions"
|
||||
)
|
||||
|
||||
type API struct {
|
||||
@@ -103,7 +104,7 @@ func NewAPI(config commonconfig.Config) (*API, error) {
|
||||
apiMux.HandleFunc("/reset", api.HandleReset)
|
||||
|
||||
mux.Handle("/api/v1/", http.StripPrefix("/api/v1", api.PermissionMiddleware(apiMux)))
|
||||
mux.Handle("/", http.StripPrefix("/", http.FileServer(http.Dir("web/build"))))
|
||||
mux.Handle("/", http.StripPrefix("/", http.FileServer(http.FS(config.WEBFS))))
|
||||
|
||||
return api, nil
|
||||
}
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
package commonconfig
|
||||
|
||||
import "io/fs"
|
||||
|
||||
type Config struct {
|
||||
APIConfig APIConfig `json:"api"`
|
||||
TmpfsConfig TmpfsConfig `json:"tmpfs"`
|
||||
WEBFS fs.FS
|
||||
}
|
||||
|
||||
type APIConfig struct {
|
||||
|
||||
Reference in New Issue
Block a user