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
}
fileServer := http.FileServer(http.FS(distFS))
r.GET("/assets/*filepath", func(c *gin.Context) {
c.FileFromFS(c.Request.URL.Path, http.FS(distFS))
})
r.GET("/*filepath", func(c *gin.Context) {
path := c.Param("filepath")
if path == "" || path == "/" {
serveIndexHTML(c, string(indexHTML))
r.NoRoute(func(c *gin.Context) {
if c.Request.Method != "GET" || strings.HasPrefix(c.Request.URL.Path, "/api/") {
c.JSON(404, gin.H{"error": "not found"})
return
}
path = strings.TrimPrefix(path, "/")
f, err := distFS.Open(path)
if err == nil {
path := strings.TrimPrefix(c.Request.URL.Path, "/")
if f, err := distFS.Open(path); err == nil {
f.Close()
fileServer.ServeHTTP(c.Writer, c.Request)
c.FileFromFS(c.Request.URL.Path, http.FS(distFS))
return
}