1
votes

I have this directory structure and I'm using Gorilla mux:

Directory structure

twitter
    layout
        stylesheets
            log.css
        log.html
    twitter.go

Following the advice here: http://www.shakedos.com/2014/Feb/08/serving-static-files-with-go.html I did this:

var router = mux.NewRouter()

func ServeStatic(router *mux.Router, staticDirectory string) {
    staticPaths := map[string]string{
        "styles": staticDirectory + "stylesheets",
        }
    for pathName, pathValue := range staticPaths {
        pathPrefix := "/" + pathName + "/"
        router.PathPrefix(pathPrefix).Handler(http.StripPrefix(pathPrefix,
        http.FileServer(http.Dir(pathValue))))
    }
}

var staticDirectory = "/layout/"

func main() {
    (//other code)
    ServeStatic(router, staticDirectory)
}

Still I can't link the CSS file. What am I doing wrong?

2
How do you access the CSS file? Can you paste the URL you are trying to access?nemo

2 Answers

6
votes

Resolved.

I added this in func main()

router.PathPrefix("/").Handler(http.FileServer(http.Dir("./layout/")))
0
votes

You can do this in a easier way without adding the extra line in main():

inside ServeStatic: add this: "."+ before pathValue

router.PathPrefix(pathPrefix).Handler(http.StripPrefix(pathPrefix,
            http.FileServer(http.Dir("."/pathValue))))