1

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(&notices)

    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?

nexteL
  • 11
  • 2
  • 1
    Have you tried ``? With a `/` before `css`? – Machiel Mar 23 '16 at 12:57
  • There is another stackoverflow question which I think might answer your question - http://stackoverflow.com/questions/15834278/serving-static-content-with-a-root-url-with-the-gorilla-toolkit – Wade73 Mar 23 '16 at 17:06

0 Answers0