Skip to content

Commit

Permalink
Merge pull request #455 from trheyi/main
Browse files Browse the repository at this point in the history
[add] multiple single page applications support
  • Loading branch information
trheyi authored Sep 6, 2023
2 parents 5305c5a + 68ebf1e commit 30073f0
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
10 changes: 10 additions & 0 deletions service/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
20 changes: 20 additions & 0 deletions service/static.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package service
import (
"fmt"
"net/http"
"path/filepath"
"strings"

"github.com/yaoapp/yao/data"
Expand All @@ -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())

Expand All @@ -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
}
Expand All @@ -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 != "" {
Expand Down
3 changes: 2 additions & 1 deletion share/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 应用存储
Expand Down

0 comments on commit 30073f0

Please sign in to comment.