Fix static assets with subpath (#1783)

Closes #1773
This commit is contained in:
qwerty287 2023-05-30 17:25:18 +02:00 committed by GitHub
parent 9b3929b7fa
commit 540ad108df
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -18,7 +18,9 @@ import (
"crypto/md5" "crypto/md5"
"fmt" "fmt"
"net/http" "net/http"
"net/url"
"regexp" "regexp"
"strings"
"time" "time"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
@ -35,6 +37,15 @@ var (
indexHTML []byte indexHTML []byte
) )
type prefixFS struct {
fs http.FileSystem
prefix string
}
func (f *prefixFS) Open(name string) (http.File, error) {
return f.fs.Open(strings.TrimPrefix(name, f.prefix))
}
// New returns a gin engine to serve the web frontend. // New returns a gin engine to serve the web frontend.
func New() (*gin.Engine, error) { func New() (*gin.Engine, error) {
e := gin.New() e := gin.New()
@ -42,14 +53,17 @@ func New() (*gin.Engine, error) {
e.Use(setupCache) e.Use(setupCache)
rootURL, _ := url.Parse(server.Config.Server.RootURL)
rootPath := rootURL.Path
httpFS, err := web.HTTPFS() httpFS, err := web.HTTPFS()
if err != nil { if err != nil {
return nil, err return nil, err
} }
h := http.FileServer(httpFS) h := http.FileServer(&prefixFS{httpFS, rootPath})
e.GET("/favicon.svg", redirect("/favicons/favicon-light-default.svg", http.StatusPermanentRedirect)) e.GET(rootPath+"/favicon.svg", redirect(server.Config.Server.RootURL+"/favicons/favicon-light-default.svg", http.StatusPermanentRedirect))
e.GET("/favicons/*filepath", gin.WrapH(h)) e.GET(rootPath+"/favicons/*filepath", gin.WrapH(h))
e.GET("/assets/*filepath", gin.WrapH(h)) e.GET(rootPath+"/assets/*filepath", gin.WrapH(h))
e.NoRoute(handleIndex) e.NoRoute(handleIndex)