Merge pull request #455 from trheyi/main

[add] multiple single page applications support
This commit is contained in:
Max 2023-09-06 09:14:16 -05:00 committed by GitHub
commit 30073f011e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 1 deletions

View file

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

View file

@ -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 != "" {

View file

@ -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 应用存储