Fix: sigle page static router

pull/31/head
zijiren233 2 years ago
parent 7eb97a3ad1
commit f7facc4bbf

@ -5,7 +5,7 @@ import (
"io/fs"
)
//go:embed dist/*
//go:embed dist
var dist embed.FS
var Public, _ = fs.Sub(dist, "dist")

@ -1,28 +1,13 @@
package handlers
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/synctv-org/synctv/public"
Vbilibili "github.com/synctv-org/synctv/server/handlers/vendors/bilibili"
"github.com/synctv-org/synctv/server/middlewares"
"github.com/synctv-org/synctv/utils"
)
func Init(e *gin.Engine) {
{
e.GET("/", func(ctx *gin.Context) {
ctx.Redirect(http.StatusMovedPermanently, "/web/")
})
web := e.Group("/web")
web.Use(middlewares.NewDistCacheControl("/web/"))
web.StaticFS("", http.FS(public.Public))
}
{
api := e.Group("/api")

@ -1,12 +0,0 @@
package handlers
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/synctv-org/synctv/public"
)
func WebServer(e gin.IRoutes) {
e.StaticFS("", http.FS(public.Public))
}

@ -5,12 +5,14 @@ import (
"github.com/synctv-org/synctv/server/handlers"
"github.com/synctv-org/synctv/server/middlewares"
auth "github.com/synctv-org/synctv/server/oauth2"
"github.com/synctv-org/synctv/server/static"
)
func Init(e *gin.Engine) {
middlewares.Init(e)
auth.Init(e)
handlers.Init(e)
static.Init(e)
}
func NewAndInit() (e *gin.Engine) {

@ -0,0 +1,48 @@
package static
import (
"io/fs"
"net/http"
"path/filepath"
"github.com/gin-gonic/gin"
"github.com/synctv-org/synctv/public"
"github.com/synctv-org/synctv/server/middlewares"
)
func Init(e *gin.Engine) {
{
e.GET("/", func(ctx *gin.Context) {
ctx.Redirect(http.StatusMovedPermanently, "/web/")
})
web := e.Group("/web")
web.Use(middlewares.NewDistCacheControl("/web/"))
err := initFSRouter(web, public.Public.(fs.ReadDirFS), ".")
if err != nil {
panic(err)
}
e.NoRoute(func(ctx *gin.Context) {
ctx.FileFromFS("", http.FS(public.Public))
})
}
}
func initFSRouter(e *gin.RouterGroup, f fs.ReadDirFS, path string) error {
dirs, err := f.ReadDir(path)
if err != nil {
return err
}
for _, dir := range dirs {
if dir.IsDir() {
return initFSRouter(e, f, filepath.Join(path, dir.Name()))
} else {
e.StaticFileFS(filepath.Join(path, dir.Name()), filepath.Join(path, dir.Name()), http.FS(f))
}
}
return nil
}
Loading…
Cancel
Save