Add: embed web files

This commit is contained in:
2022-12-17 15:55:10 +08:00
parent d52c64d25a
commit 21b2098c78
3 changed files with 19 additions and 3 deletions

14
main.go
View File

@@ -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)

View File

@@ -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
}

View File

@@ -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 {