You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
synctv/server/static/static.go

70 lines
1.3 KiB
Go

package static
import (
"io/fs"
"net/http"
"net/url"
"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"
)
func Init(e *gin.Engine) {
e.GET("/", func(ctx *gin.Context) {
ctx.Redirect(http.StatusMovedPermanently, "/web/")
})
web := e.Group("/web")
if flags.WebPath == "" {
web.Use(middlewares.NewDistCacheControl("/web/"))
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
}
})
}
}
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 {
u, err := url.JoinPath(path, dir.Name())
if err != nil {
return err
}
if dir.IsDir() {
err = initFSRouter(e, f, u)
if err != nil {
return err
}
} else {
e.StaticFileFS(u, u, http.FS(f))
}
}
return nil
}