Serving static content with a root URL with the Gorilla toolkit

后端 未结 5 2111
礼貌的吻别
礼貌的吻别 2020-12-12 18:42

I am attempting to use the Gorilla toolkit\'s mux package to route URLs in a Go web server. Using this question as a guide I have the following Go code:

fu         


        
5条回答
  •  隐瞒了意图╮
    2020-12-12 19:23

    I have this code here that works quite nice and is re-usable.

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

提交回复
热议问题