fix: 修复 Docker 部署时 r.Static 与 /api 路由冲突

This commit is contained in:
shirainbown
2026-06-18 23:12:09 +08:00
parent 6f507b319e
commit 498bcf8a78

View File

@@ -120,9 +120,23 @@ func main() {
// Static files
if cfg.WebStaticPath != "" {
r.Static("/", cfg.WebStaticPath)
staticFS := os.DirFS(cfg.WebStaticPath)
fileServer := http.FileServer(http.FS(staticFS))
r.NoRoute(func(c *gin.Context) {
c.File(cfg.WebStaticPath + "/index.html")
path := c.Request.URL.Path
// Handle assets at any path depth
if idx := strings.Index(path, "/assets/"); idx >= 0 {
c.Request.URL.Path = path[idx:]
fileServer.ServeHTTP(c.Writer, c.Request)
return
}
if path == "/favicon.ico" {
fileServer.ServeHTTP(c.Writer, c.Request)
return
}
// SPA fallback: all other paths serve index.html
c.Request.URL.Path = "/"
fileServer.ServeHTTP(c.Writer, c.Request)
})
} else {
staticFS, err := fs.Sub(webFS, "static")