[add] multiple single page applications support
This commit is contained in:
parent
047eff9e45
commit
68ebf1e6cc
3 changed files with 32 additions and 1 deletions
|
|
@ -38,6 +38,16 @@ func withStaticFileServer(c *gin.Context) {
|
||||||
return
|
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
|
// static file server
|
||||||
AppFileServer.ServeHTTP(c.Writer, c.Request)
|
AppFileServer.ServeHTTP(c.Writer, c.Request)
|
||||||
c.Abort()
|
c.Abort()
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ package service
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/yaoapp/yao/data"
|
"github.com/yaoapp/yao/data"
|
||||||
|
|
@ -13,6 +14,12 @@ import (
|
||||||
// AppFileServer static file server
|
// AppFileServer static file server
|
||||||
var AppFileServer http.Handler
|
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
|
// XGenFileServerV1 XGen v1.0
|
||||||
var XGenFileServerV1 http.Handler = http.FileServer(data.XgenV1())
|
var XGenFileServerV1 http.Handler = http.FileServer(data.XgenV1())
|
||||||
|
|
||||||
|
|
@ -33,6 +40,11 @@ func SetupStatic() error {
|
||||||
return nil
|
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"))
|
AppFileServer = http.FileServer(fs.Dir("public"))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
@ -45,6 +57,14 @@ func isPWA() bool {
|
||||||
return share.App.Static.PWA
|
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
|
// SetupAdmin setup admin static root
|
||||||
func adminRoot() (string, int) {
|
func adminRoot() (string, int) {
|
||||||
if AdminRoot != "" {
|
if AdminRoot != "" {
|
||||||
|
|
|
||||||
|
|
@ -88,6 +88,7 @@ type AppInfo struct {
|
||||||
// Static setting
|
// Static setting
|
||||||
type Static struct {
|
type Static struct {
|
||||||
PWA bool `json:"pwa,omitempty"`
|
PWA bool `json:"pwa,omitempty"`
|
||||||
|
Apps []string `json:"apps,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// AppStorage 应用存储
|
// AppStorage 应用存储
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue