Rendering CSS in a Go Web Application

13,274

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

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

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

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/

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

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.

type justFilesFilesystem struct {
    fs http.FileSystem
}

func (fs justFilesFilesystem) Open(name string) (http.File, error) {
    f, err := fs.fs.Open(name)
    if err != nil {
        return nil, err
    }
    return neuteredReaddirFile{f}, nil
}

type neuteredReaddirFile struct {
    http.File
}

func (f neuteredReaddirFile) Readdir(count int) ([]os.FileInfo, error) {
    return nil, nil
}

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

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

 fs := justFilesFilesystem{http.Dir("resources/")}
 http.Handle("/resources/", http.StripPrefix("/resources/", http.FileServer(fs)))
Share:
13,274

Related videos on Youtube

Lincoln Bergeson
Author by

Lincoln Bergeson

Updated on June 06, 2022

Comments

  • Lincoln Bergeson
    Lincoln Bergeson almost 2 years

    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 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?

    • mcriecken
      mcriecken over 11 years
      Are you sure the path to your CSS file that you are linking is correct? Can you comment the <link> tag you are using to link to your stylesheet?
    • Lincoln Bergeson
      Lincoln Bergeson over 11 years
      The rest of my code looks exactly like this: golang.org/doc/articles/wiki/final.go plus two html files that are the templates (edit.html and view.html). Everything's in the same folder. This is my <link> tag: <link rel = "stylesheet" style = "text/css" href = "css_1.css">
    • mcriecken
      mcriecken over 11 years
      Can you update the question with your CSS? If you have any syntax errors (i.e., missing colons) in the CSS it can cause the whole thing not to render properly.
    • Lincoln Bergeson
      Lincoln Bergeson over 11 years
      Well, I'm not on that machine right now but maybe I will I am. Thibg is, I copied the CSS directly into style tags and it worked fine, but as soon as I put it in link tags and an external stylesheet it stopped working.
  • rofrol
    rofrol about 10 years
    Works. I had to refresh with ctrl+shift+r in Firefox
  • sargas
    sargas over 7 years
    @rofrol Gotta not load from cache ;)