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