Go:用大猩猩多路复用器提供CSS文件 [英] Go: serve CSS files with gorilla mux
本文介绍了Go:用大猩猩多路复用器提供CSS文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个目录结构,我使用了Gorilla mux:
目录结构
$ pre > twitter
布局
样式表
log.css
log.html
twitter.go
遵循以下建议: http://www.shakedos.com/2014/Feb/08/serving-static-files-with-go.html 我这样做了:
var router = mux.NewRouter()
func ServeStatic(router * mux.Router,staticDirectory string){
staticPaths:= map [string] string {
styles:staticDirectory +stylesheets,
}
for pathName,pathValue:= range staticPaths {
pathPrefix :路径前缀(pathPrefix,
http.FileServer(http.Dir(pathValue))))$ b $(路径名称):=/+ pathName +/
router.PathPrefix(pathPrefix).Handler b}
}
var staticDirectory =/ layout /
func main(){
(// other code)
ServeStatic(router,staticDirectory)
}
我仍然无法链接CSS文件。我在做什么错了?
解决方案
解决。
这个在func中main()
router.PathPrefix(/)。Handler(http.FileServer(http.Dir( ./layout/)))
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?
解决方案
Resolved.
I added this in func main()
router.PathPrefix("/").Handler(http.FileServer(http.Dir("./layout/")))
这篇关于Go:用大猩猩多路复用器提供CSS文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文