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 package main
import ( import (
"embed"
"encoding/json" "encoding/json"
"flag" "flag"
"io/fs"
"log" "log"
"msw-open-music/pkg/api" "msw-open-music/pkg/api"
"msw-open-music/pkg/commonconfig" "msw-open-music/pkg/commonconfig"
@@ -15,11 +17,21 @@ func init() {
flag.StringVar(&ConfigFilePath, "config", "config.json", "backend config file path") flag.StringVar(&ConfigFilePath, "config", "config.json", "backend config file path")
} }
//go:embed web/build
var WEBFILES embed.FS
func main() { func main() {
var err error var err error
flag.Parse() 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) configFile, err := os.Open(ConfigFilePath)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)

View File

@@ -1,11 +1,12 @@
package api package api
import ( import (
"github.com/gorilla/sessions"
"msw-open-music/pkg/commonconfig" "msw-open-music/pkg/commonconfig"
"msw-open-music/pkg/database" "msw-open-music/pkg/database"
"msw-open-music/pkg/tmpfs" "msw-open-music/pkg/tmpfs"
"net/http" "net/http"
"github.com/gorilla/sessions"
) )
type API struct { type API struct {
@@ -103,7 +104,7 @@ func NewAPI(config commonconfig.Config) (*API, error) {
apiMux.HandleFunc("/reset", api.HandleReset) apiMux.HandleFunc("/reset", api.HandleReset)
mux.Handle("/api/v1/", http.StripPrefix("/api/v1", api.PermissionMiddleware(apiMux))) 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 return api, nil
} }

View File

@@ -1,8 +1,11 @@
package commonconfig package commonconfig
import "io/fs"
type Config struct { type Config struct {
APIConfig APIConfig `json:"api"` APIConfig APIConfig `json:"api"`
TmpfsConfig TmpfsConfig `json:"tmpfs"` TmpfsConfig TmpfsConfig `json:"tmpfs"`
WEBFS fs.FS
} }
type APIConfig struct { type APIConfig struct {