-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Description
Problem
So I'm not really sure if this is a bug or anything, and this is also my first bug report so I apologize for my lack of knowledge on correctly reporting a bug.
I was trying to create a SPA using gorilla/mux and kept running into issues with displaying my webpage. I knew my code was correct but it had something to do with the file paths inside the SPA handler itself because when I was running my code I was getting a StatusInternalServerError. All my file paths were correct so it had to be something with the ServeHTTP function itself. I later found it to be an issue with the path as shown below.
Versions
go version go1.13.4 windows/amd64
github.com/gorilla/mux v1.7.4
…
main.go
type spaHandler struct {
staticPath string
indexPath string
}
func (h spaHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
path, err := filepath.Abs(r.URL.Path)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
path = filepath.Join(h.staticPath, path) //this is the part of the code I changed to fix my issue
_, err = os.Stat(path)
if os.IsNotExist(err) {
http.ServeFile(w, r, filepath.Join(h.staticPath, h.indexPath))
return
} else if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
http.FileServer(http.Dir(h.staticPath)).ServeHTTP(w, r)
}
func main() {
router := mux.NewRouter()
spa := spaHandler{staticPath: "./admin/build", indexPath: "index.html"}
router.PathPrefix("/").Handler(spa)
log.Fatal(http.ListenAndServe(":8080", router))
}
Code was changed to below, and started to work perfectly fine.
path = filepath.Join(h.staticPath, r.URL.Path)
…
I don't know whether or not to report this as a fix to the actual public code or not. Still new to contributing.
Metadata
Metadata
Assignees
Labels
Type
Projects
Status