英文:
Go: serve CSS files with gorilla mux
问题
我有这样的目录结构,并且我正在使用Gorilla mux:
目录结构
twitter
layout
stylesheets
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 := "/" + pathName + "/"
router.PathPrefix(pathPrefix).Handler(http.StripPrefix(pathPrefix,
http.FileServer(http.Dir(pathValue))))
}
}
var staticDirectory = "/layout/"
func main() {
(//其他代码)
ServeStatic(router, staticDirectory)
}
但是我仍然无法链接CSS文件。我做错了什么?
英文:
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?
答案1
得分: 6
已解决。
我在 func main()
中添加了以下内容:
router.PathPrefix("/").Handler(http.FileServer(http.Dir("./layout/")))
英文:
Resolved.
I added this in func main()
router.PathPrefix("/").Handler(http.FileServer(http.Dir("./layout/")))
答案2
得分: 0
你可以更简单地完成这个任务,而不需要在main()函数中添加额外的行:
在ServeStatic函数内部:
在pathValue之前添加"."。
router.PathPrefix(pathPrefix).Handler(http.StripPrefix(pathPrefix,
http.FileServer(http.Dir("."/pathValue))))
英文:
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))))
答案3
得分: 0
在你的Go
文件中:
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))))
}
// 为样式表目录添加路由前缀
router.PathPrefix("/stylesheets/").Handler(http.StripPrefix("/stylesheets/",
http.FileServer(http.Dir(staticDirectory+"stylesheets/"))))
}
然后在你的html
文件中使用以下链接进行更新:
<link rel="stylesheet" type="text/css" href="/stylesheets/log.css">
请注意,这是一个示例代码片段,用于在Go中为静态文件提供服务并添加样式表链接。你需要根据你的实际需求进行适当的修改和集成。
英文:
In your Go
file:
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))))
}
// Add route prefix for stylesheets directory
router.PathPrefix("/stylesheets/").Handler(http.StripPrefix("/stylesheets/",
http.FileServer(http.Dir(staticDirectory+"stylesheets/"))))
}
Then update your html
file with this link:
<link rel="stylesheet" type="text/css" href="/stylesheets/log.css">
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论