fix: return 404 when page not found (#927)

This commit is contained in:
quanljh 2024-12-30 20:43:37 +08:00 committed by GitHub
parent 18020939da
commit cea4031da3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -303,7 +303,7 @@ func fallbackToFrontend(frontendDist fs.FS) func(*gin.Context) {
}
return func(c *gin.Context) {
if strings.HasPrefix(c.Request.URL.Path, "/api") {
c.JSON(http.StatusOK, newErrorResponse(errors.New("404 Not Found")))
c.JSON(http.StatusNotFound, newErrorResponse(errors.New("404 Not Found")))
return
}
if strings.HasPrefix(c.Request.URL.Path, "/dashboard") {
@ -311,18 +311,24 @@ func fallbackToFrontend(frontendDist fs.FS) func(*gin.Context) {
localFilePath := path.Join(singleton.Conf.AdminTemplate, stripPath)
if checkLocalFileOrFs(c, frontendDist, localFilePath) {
return
} else {
c.Status(http.StatusNotFound)
c.Writer.WriteHeaderNow();
}
if !checkLocalFileOrFs(c, frontendDist, singleton.Conf.AdminTemplate+"/index.html") {
c.JSON(http.StatusOK, newErrorResponse(errors.New("404 Not Found")))
c.JSON(http.StatusNotFound, newErrorResponse(errors.New("404 Not Found")))
}
return
}
localFilePath := path.Join(singleton.Conf.UserTemplate, c.Request.URL.Path)
if checkLocalFileOrFs(c, frontendDist, localFilePath) {
return
} else {
c.Status(http.StatusNotFound)
c.Writer.WriteHeaderNow();
}
if !checkLocalFileOrFs(c, frontendDist, singleton.Conf.UserTemplate+"/index.html") {
c.JSON(http.StatusOK, newErrorResponse(errors.New("404 Not Found")))
c.JSON(http.StatusNotFound, newErrorResponse(errors.New("404 Not Found")))
}
}
}