Feat: web path flag

pull/44/head
zijiren233 2 years ago
parent e3e96a1914
commit 74e03e5063

@ -3,5 +3,6 @@ package flags
var (
DisableUpdateCheck bool
DisableWeb bool
WebPath string
DisableLogColor bool
)

@ -162,4 +162,5 @@ func init() {
ServerCmd.PersistentFlags().BoolVar(&flags.DisableUpdateCheck, "disable-update-check", false, "disable update check")
ServerCmd.PersistentFlags().BoolVar(&flags.DisableWeb, "disable-web", false, "disable web")
ServerCmd.PersistentFlags().BoolVar(&flags.DisableLogColor, "disable-log-color", false, "disable log color")
ServerCmd.PersistentFlags().StringVar(&flags.WebPath, "web-path", "", "if not set, use embed web")
}

@ -7,6 +7,7 @@ import (
"strings"
"github.com/gin-gonic/gin"
"github.com/synctv-org/synctv/cmd/flags"
"github.com/synctv-org/synctv/public"
"github.com/synctv-org/synctv/server/middlewares"
)
@ -18,19 +19,31 @@ func Init(e *gin.Engine) {
web := e.Group("/web")
web.Use(middlewares.NewDistCacheControl("/web/"))
if flags.WebPath == "" {
web.Use(middlewares.NewDistCacheControl("/web/"))
err := initFSRouter(web, public.Public.(fs.ReadDirFS), ".")
if err != nil {
panic(err)
err := initFSRouter(web, public.Public.(fs.ReadDirFS), ".")
if err != nil {
panic(err)
}
e.NoRoute(func(ctx *gin.Context) {
if strings.HasPrefix(ctx.Request.URL.Path, "/web/") {
ctx.FileFromFS("", http.FS(public.Public))
return
}
})
} else {
web.Static("/", flags.WebPath)
e.NoRoute(func(ctx *gin.Context) {
if strings.HasPrefix(ctx.Request.URL.Path, "/web/") {
ctx.FileFromFS("", http.Dir(flags.WebPath))
return
}
})
}
e.NoRoute(func(ctx *gin.Context) {
if strings.HasPrefix(ctx.Request.URL.Path, "/web/") {
ctx.FileFromFS("", http.FS(public.Public))
return
}
})
}
func initFSRouter(e *gin.RouterGroup, f fs.ReadDirFS, path string) error {

Loading…
Cancel
Save