From cea4031da3176d2b34f901abf45a9628943d928d Mon Sep 17 00:00:00 2001 From: quanljh <38105306+quanljh@users.noreply.github.com> Date: Mon, 30 Dec 2024 20:43:37 +0800 Subject: [PATCH] fix: return 404 when page not found (#927) --- cmd/dashboard/controller/controller.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/cmd/dashboard/controller/controller.go b/cmd/dashboard/controller/controller.go index 268f125..7f00bbb 100644 --- a/cmd/dashboard/controller/controller.go +++ b/cmd/dashboard/controller/controller.go @@ -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"))) } } }