fix: 修复Gin通配符路由冲突导致panic

- 改用NoRoute替代通配符路由/*filepath
- NoRoute中先检查静态文件存在再返回SHTML
This commit is contained in:
2026-05-03 17:25:23 +08:00
parent 20f1bfc32d
commit 8b9879816d
2 changed files with 1037 additions and 10 deletions
+6 -10
View File
@@ -261,24 +261,20 @@ func setupEmbeddedFrontend(r *gin.Engine) {
return return
} }
fileServer := http.FileServer(http.FS(distFS))
r.GET("/assets/*filepath", func(c *gin.Context) { r.GET("/assets/*filepath", func(c *gin.Context) {
c.FileFromFS(c.Request.URL.Path, http.FS(distFS)) c.FileFromFS(c.Request.URL.Path, http.FS(distFS))
}) })
r.GET("/*filepath", func(c *gin.Context) { r.NoRoute(func(c *gin.Context) {
path := c.Param("filepath") if c.Request.Method != "GET" || strings.HasPrefix(c.Request.URL.Path, "/api/") {
if path == "" || path == "/" { c.JSON(404, gin.H{"error": "not found"})
serveIndexHTML(c, string(indexHTML))
return return
} }
path = strings.TrimPrefix(path, "/")
f, err := distFS.Open(path) path := strings.TrimPrefix(c.Request.URL.Path, "/")
if err == nil { if f, err := distFS.Open(path); err == nil {
f.Close() f.Close()
fileServer.ServeHTTP(c.Writer, c.Request) c.FileFromFS(c.Request.URL.Path, http.FS(distFS))
return return
} }
+1031
View File
File diff suppressed because it is too large Load Diff