Before realizing that the http package has a builtin ServeFile method, I implemented a static handler more or less like this:
func StaticHandler(w http.ResponseWriter, r *http.Request) {
filename := mux.Vars(r)["static"] // using gorilla/mux
f, err := os.Open(fmt.Sprintf("%v/static/%v", webroot, filename))
if err != nil {
http.NotFound(w, r)
return
}
defer f.Close()
io.Copy(w, f)
}
And, for example, linked my style sheet and images this way:
<img href="/image.jpg" />
<link rel="stylesheet" type="text/css" href="/stylesheet.css">
This worked just fine, except for one thing: my linked stylesheet was not being applied by the browser (tested in Chrome, Firefox, Midori). The stylesheet could be served ( visiting MYSITE/stylesheet.css displayed the css plaintext) and images would load normally in a page, but none of my pages would have any style.
Any ideas as to why?