使用gorilla mux在Go中提供CSS文件。

huangapple go评论80阅读模式
英文:

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{
    &quot;styles&quot;: staticDirectory + &quot;stylesheets&quot;,
}
for pathName, pathValue := range staticPaths {
    pathPrefix := &quot;/&quot; + pathName + &quot;/&quot;
    router.PathPrefix(pathPrefix).Handler(http.StripPrefix(pathPrefix,
        http.FileServer(http.Dir(pathValue))))
}
// Add route prefix for stylesheets directory
router.PathPrefix(&quot;/stylesheets/&quot;).Handler(http.StripPrefix(&quot;/stylesheets/&quot;,
    http.FileServer(http.Dir(staticDirectory+&quot;stylesheets/&quot;))))
}

Then update your htmlfile with this link:

&lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;/stylesheets/log.css&quot;&gt;

huangapple
  • 本文由 发表于 2014年8月17日 17:32:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/25347963.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定