diff --git a/service/middleware.go b/service/middleware.go index 22af568a5..03f469c42 100644 --- a/service/middleware.go +++ b/service/middleware.go @@ -38,6 +38,16 @@ func withStaticFileServer(c *gin.Context) { return } + // PWA app static file server + for root, length := range SpaRoots { + if length >= length && c.Request.URL.Path[0:length] == root { + c.Request.URL.Path = strings.TrimPrefix(c.Request.URL.Path, root) + spaFileServers[root].ServeHTTP(c.Writer, c.Request) + c.Abort() + return + } + } + // static file server AppFileServer.ServeHTTP(c.Writer, c.Request) c.Abort() diff --git a/service/static.go b/service/static.go index 415ba44f1..d275fd203 100644 --- a/service/static.go +++ b/service/static.go @@ -3,6 +3,7 @@ package service import ( "fmt" "net/http" + "path/filepath" "strings" "github.com/yaoapp/yao/data" @@ -13,6 +14,12 @@ import ( // AppFileServer static file server var AppFileServer http.Handler +// spaFileServers spa static file server +var spaFileServers map[string]http.Handler = map[string]http.Handler{} + +// SpaRoots SPA static file server +var SpaRoots map[string]int = map[string]int{} + // XGenFileServerV1 XGen v1.0 var XGenFileServerV1 http.Handler = http.FileServer(data.XgenV1()) @@ -33,6 +40,11 @@ func SetupStatic() error { return nil } + for _, root := range spaApps() { + spaFileServers[root] = http.FileServer(fs.DirPWA(filepath.Join("public", root))) + SpaRoots[root] = len(root) + } + AppFileServer = http.FileServer(fs.Dir("public")) return nil } @@ -45,6 +57,14 @@ func isPWA() bool { return share.App.Static.PWA } +// rewrite path +func spaApps() []string { + if share.App.Static == nil { + return []string{} + } + return share.App.Static.Apps +} + // SetupAdmin setup admin static root func adminRoot() (string, int) { if AdminRoot != "" { diff --git a/share/types.go b/share/types.go index 97241c4d3..9b8d9524d 100644 --- a/share/types.go +++ b/share/types.go @@ -87,7 +87,8 @@ type AppInfo struct { // Static setting type Static struct { - PWA bool `json:"pwa,omitempty"` + PWA bool `json:"pwa,omitempty"` + Apps []string `json:"apps,omitempty"` } // AppStorage 应用存储