我有以下代码:
func MainRouter() *mux.Router {
prefix := "/"
adminus_prefix := "/adminus/"
r := mux.NewRouter()
publicSub := r.PathPrefix("/").Subrouter()
publicus.Routers(publicSub, prefix)
return r
}
以下路由器:
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/"))))
}
这是路由处理程序的简单组织,每个处理程序呈现HTML模板如下:
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)
}
}
在HTML中,我从文件夹中获取静态文件,如下所示:
<link rel="stylesheet" type="text/css" href="css/css.css">
它在根目录中工作正常,但是如果我想添加路由,例如:
router.HandleFunc("/help/bablala/vlavlavla", handlers.BlablaHandler)
呈现HTML模板,但CSS和JS文件出现404错误。我认为问题出在这一部分:
router.PathPrefix("/").Handler(http.StripPrefix(prefix, http.FileServer(http.Dir("./static/"))))
我该如何解决这个问题?