fix: 修复Gin通配符路由冲突导致panic
- 改用NoRoute替代通配符路由/*filepath - NoRoute中先检查静态文件存在再返回SHTML
This commit is contained in:
+6
-10
@@ -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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user