Home | About | Apps | Github | Rss

Go-snippet: static file server

Am working on building some tooling that involves cloning a git repository and passing its path to my go server. One of the duties of that server is to make a static files folder available at a specific path.

This is the code snippet I use to achieve it.

r := mux.NewRouter() //gorilla mux
r.PathPrefix("/static").Handler(
   http.StripPrefix(
     "/static/", 
     http.FileServer(http.Dir("./static-dir/"),
   ),
)
http.Handle("/", r)
http.ListenAndServe(":8080", nil)

Note: The comma at end of each line, including at the end of http.StripPrefix call, is required; otherwise go compiler complains of bad syntax. Using commas to enable splitting statements into multiple lines feels a little ridiculous.


More posts