在Go Web应用程序中渲染CSS

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

Rendering CSS in a Go Web Application

问题

我写了一个非常基础的网页应用程序,按照这个教程进行。我想在外部样式表中添加CSS规则,但是它不起作用 - 当HTML模板被渲染时,出现了一些问题,CSS完全被忽略了。如果我把规则放在<style>标签中,它就可以工作,但是我不想处理那个。

在Go的网页应用程序中,我该如何渲染外部CSS样式表?

英文:

I wrote a very basic web application, following this tutorial. I want to add css rules in an external stylesheet, but it's not working - when the HTML templates are rendered, something goes wrong and the CSS is completely ignored. If I put the rules in <style> tags, it works, but I don't want to have to deal with that.

In a Go web application, how do I render an external CSS stylesheet?

答案1

得分: 37

添加一个处理程序来处理从指定目录中提供静态文件的请求。

例如,在{server.go目录}/resources/中创建文件夹,并使用以下代码:

  1. http.Handle("/resources/", http.StripPrefix("/resources/", http.FileServer(http.Dir("resources"))))

同时使用/resources/somethingsomething.css来访问。

使用StripPrefix的原因是,您可以随意更改提供的目录,但保持HTML中的引用不变。

例如,要从/home/www/中提供文件:

  1. http.Handle("/resources/", http.StripPrefix("/resources/", http.FileServer(http.Dir("/home/www/"))))

请注意,这将保留资源目录的列表。如果要防止这种情况发生,可以在Go代码片段博客中找到一个很好的代码片段:

http://gosnip.posterous.com/disable-directory-listing-with-httpfileserver

**编辑:**Posterous现在已经关闭,所以我从golang邮件列表中提取了代码,并在这里发布。

  1. type justFilesFilesystem struct {
  2. fs http.FileSystem
  3. }
  4. func (fs justFilesFilesystem) Open(name string) (http.File, error) {
  5. f, err := fs.fs.Open(name)
  6. if err != nil {
  7. return nil, err
  8. }
  9. return neuteredReaddirFile{f}, nil
  10. }
  11. type neuteredReaddirFile struct {
  12. http.File
  13. }
  14. func (f neuteredReaddirFile) Readdir(count int) ([]os.FileInfo, error) {
  15. return nil, nil
  16. }

原始帖子讨论它:https://groups.google.com/forum/#!topic/golang-nuts/bStLPdIVM6w

并将其替换为上面的代码行:

  1. fs := justFilesFilesystem{http.Dir("resources/")}
  2. http.Handle("/resources/", http.StripPrefix("/resources/", http.FileServer(fs)))
英文:

Add a handler to handle serving static files from a specified directory.

eg. create {server.go directory}/resources/ and use

  1. http.Handle(&quot;/resources/&quot;, http.StripPrefix(&quot;/resources/&quot;, http.FileServer(http.Dir(&quot;resources&quot;))))

along with /resources/somethingsomething.css

The reason for using StripPrefix is that you can change the served directory as you please, but keep the reference in HTML the same.

eg. to serve from /home/www/

  1. http.Handle(&quot;/resources/&quot;, http.StripPrefix(&quot;/resources/&quot;, http.FileServer(http.Dir(&quot;/home/www/&quot;))))

Note that this will leave the resources directory listing open.
If you want to prevent that, there is a good snippet on the go snippet blog:

http://gosnip.posterous.com/disable-directory-listing-with-httpfileserver

Edit: Posterous is now gone, so I just pulled the code off of the golang mailing list and will post it here.

  1. type justFilesFilesystem struct {
  2. fs http.FileSystem
  3. }
  4. func (fs justFilesFilesystem) Open(name string) (http.File, error) {
  5. f, err := fs.fs.Open(name)
  6. if err != nil {
  7. return nil, err
  8. }
  9. return neuteredReaddirFile{f}, nil
  10. }
  11. type neuteredReaddirFile struct {
  12. http.File
  13. }
  14. func (f neuteredReaddirFile) Readdir(count int) ([]os.FileInfo, error) {
  15. return nil, nil
  16. }

Original post discussing it: https://groups.google.com/forum/#!topic/golang-nuts/bStLPdIVM6w

And use it like this in place of the line above:

  1. fs := justFilesFilesystem{http.Dir(&quot;resources/&quot;)}
  2. http.Handle(&quot;/resources/&quot;, http.StripPrefix(&quot;/resources/&quot;, http.FileServer(fs)))

huangapple
  • 本文由 发表于 2012年11月9日 12:14:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/13302020.html
匿名

发表评论

匿名网友

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

确定