Feat/dev #2

Merged
Blizzard merged 2 commits from feat/dev into main 2026-10-01 06:25:07 +00:00
3 changed files with 23 additions and 3 deletions
+5 -1
View File
@@ -31,6 +31,10 @@ func Register(r *gin.Engine) {
fileServer.ServeHTTP(c.Writer, c.Request) fileServer.ServeHTTP(c.Writer, c.Request)
return return
} }
if strings.HasPrefix(reqPath, "assets/") {
c.Status(http.StatusNotFound)
return
}
} }
// SPA fallback // SPA fallback
data, err := fs.ReadFile(sub, "index.html") data, err := fs.ReadFile(sub, "index.html")
@@ -43,7 +47,7 @@ func Register(r *gin.Engine) {
r.GET("/", handler) r.GET("/", handler)
r.NoRoute(func(c *gin.Context) { r.NoRoute(func(c *gin.Context) {
if c.Request.Method != http.MethodGet || strings.HasPrefix(c.Request.URL.Path, "/api/") || c.Request.URL.Path == "/api" || strings.HasPrefix(c.Request.URL.Path, "/assets/") { if (c.Request.Method != http.MethodGet && c.Request.Method != http.MethodHead) || strings.HasPrefix(c.Request.URL.Path, "/api/") || c.Request.URL.Path == "/api" {
c.Status(http.StatusNotFound) c.Status(http.StatusNotFound)
return return
} }
+17
View File
@@ -1,6 +1,7 @@
package admin package admin
import ( import (
"io/fs"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"strings" "strings"
@@ -16,6 +17,20 @@ func TestAdminAtRootAndAPIRoutes(t *testing.T) {
r := gin.New() r := gin.New()
httpapi.New(nil, nil, &config.Config{}).Register(r) httpapi.New(nil, nil, &config.Config{}).Register(r)
Register(r) Register(r)
assets, err := fs.ReadDir(distFS, "dist/assets")
if err != nil {
t.Fatal(err)
}
var script string
for _, asset := range assets {
if strings.HasSuffix(asset.Name(), ".js") {
script = "/assets/" + asset.Name()
break
}
}
if script == "" {
t.Fatal("admin bundle has no JavaScript asset")
}
for _, tc := range []struct { for _, tc := range []struct {
path string path string
@@ -23,6 +38,8 @@ func TestAdminAtRootAndAPIRoutes(t *testing.T) {
body string body string
}{ }{
{path: "/", status: http.StatusOK, body: "<div id=\"root\"></div>"}, {path: "/", status: http.StatusOK, body: "<div id=\"root\"></div>"},
{path: script, status: http.StatusOK},
{path: "/favicon.svg", status: http.StatusOK},
{path: "/api/ping", status: http.StatusOK, body: "pong"}, {path: "/api/ping", status: http.StatusOK, body: "pong"},
{path: "/api/missing", status: http.StatusNotFound}, {path: "/api/missing", status: http.StatusNotFound},
{path: "/assets/missing.js", status: http.StatusNotFound}, {path: "/assets/missing.js", status: http.StatusNotFound},
+1 -2
View File
@@ -1,4 +1,3 @@
const baseURL = 'http://192.168.100.3:9090/api' const baseURL = 'https://pet.sundynix.cn/api'
//const baseURL = 'https://pet.sundynix.cn'
module.exports = { baseURL}; module.exports = { baseURL};