I have the following code:
func MainRouter() *mux.Router {
prefix := "/"
adminus_prefix := "/adminus/"
r := mux.NewRouter()
publicSub := r.PathPrefix("/").Subrouter()
publicus.Routers(publicSub, prefix)
return r
}
And the following routers:
func Routers(router *mux.Router, prefix string) {
router.HandleFunc("/", handlers.MainHandler)
router.HandleFunc("/help", handlers.HelpHandler)
router.HandleFunc("/company", handlers.CompanyHandler)
router.HandleFunc("/documents", handlers.DocumentsHandler)
router.HandleFunc("/addcomplaint", handlers.AddcomplaintHandler).Methods("POST")
router.PathPrefix("/").Handler(http.StripPrefix(prefix, http.FileServer(http.Dir("./static/"))))
}
That's simple organisation of route handlers, each handler renders HTML templates as following:
var maintlp = pongo2.Must(pongo2.FromFile("templates/index.html"))
func MainHandler(w http.ResponseWriter, request *http.Request) {
fmt.Println("MainHandler")
Session := utils.NewDbinstanse()
notices := make([]documents.Notice, 0)
Session.C("notice").Find(nil).Sort("-time").All(¬ices)
err := maintlp.ExecuteWriter(pongo2.Context{"notices": notices}, w)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
And in HTML I get static files from folder as following:
<link rel="stylesheet" type="text/css" href="css/css.css">
It works fine in root directory, but if I want to add a route, e.g.:
router.HandleFunc("/help/bablala/vlavlavla", handlers.BlablaHandler)
The HTML templates are rendered, but the CSS and JS files give a 404 error. I think the problem is in this part:
router.PathPrefix("/").Handler(http.StripPrefix(prefix, http.FileServer(http.Dir("./static/"))))
How can I fix this?