Skip to content

Commit

Permalink
fix: home page 404
Browse files Browse the repository at this point in the history
  • Loading branch information
naiba committed Dec 30, 2024
1 parent 5df4c62 commit 953fa15
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
8 changes: 6 additions & 2 deletions cmd/dashboard/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,23 +309,27 @@ func fallbackToFrontend(frontendDist fs.FS) func(*gin.Context) {
if strings.HasPrefix(c.Request.URL.Path, "/dashboard") {
stripPath := strings.TrimPrefix(c.Request.URL.Path, "/dashboard")
localFilePath := path.Join(singleton.Conf.AdminTemplate, stripPath)
if stripPath == "/" {
c.Status(http.StatusOK)
}
if checkLocalFileOrFs(c, frontendDist, localFilePath) {
return
} else {
c.Status(http.StatusNotFound)
c.Writer.WriteHeaderNow();

This comment has been minimized.

Copy link
@quanljh

quanljh Dec 30, 2024

Contributor

@naiba sorry 我本地测的时候没发现home page 404,可能是我用的windows

但是去掉这个WriteHeaderNow,就return 不了404, gin 这个库好像有自己的机制。我go 不太熟,你有空看看?

下面的代码就可以return 404

package main

import (
	"log"
	"net/http"
	"os"
	"path/filepath"
)

func main() {
	cwd, err := os.Getwd()
	if err != nil {
		log.Fatal(err)
	}
	static := cwd

    http.Handle("/", serveErrorPage(404, static))
    http.ListenAndServe(":8000", nil)
}

func serveErrorPage(code int, dir string) http.Handler {
    fn := func(w http.ResponseWriter, r *http.Request) {
        w.WriteHeader(code)
        path := filepath.Join(dir, r.URL.Path)
        log.Println(path)
        http.ServeFile(w, r, path)
    }

    return http.HandlerFunc(fn)
}
}
if !checkLocalFileOrFs(c, frontendDist, singleton.Conf.AdminTemplate+"/index.html") {
c.JSON(http.StatusNotFound, newErrorResponse(errors.New("404 Not Found")))
}
return
}
localFilePath := path.Join(singleton.Conf.UserTemplate, c.Request.URL.Path)
if c.Request.URL.Path == "/" {
c.Status(http.StatusOK)
}
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.StatusNotFound, newErrorResponse(errors.New("404 Not Found")))
Expand Down
2 changes: 1 addition & 1 deletion service/singleton/frontend-templates.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name: "OfficialAdmin"
repository: "https://github.com/nezhahq/admin-frontend"
author: "nezhahq"
version: "v1.5.0"
version: "v1.5.1"
isadmin: true
isofficial: true
- path: "user-dist"
Expand Down

0 comments on commit 953fa15

Please sign in to comment.