fix: 修复 Docker 部署时 r.Static 与 /api 路由冲突
This commit is contained in:
@@ -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")
|
||||
|
||||
Reference in New Issue
Block a user