Serving static files over HTTP using Gorilla Mux
In the previous recipe, we served static
resources through Go's HTTP file server. In this recipe, we will look at how we can serve it through the Gorilla Mux router, which is also one of the most common ways of creating an HTTP router.
Getting ready…
As we have already created a template which serves main.css
from the static/css
directory present on the filesystem in our previous recipe, we will just update it to use the Gorilla Mux router.
How to do it…
- Install the
github.com/gorilla/mux
package using thego get
command, as follows:
$ go get github.com/gorilla/mux
- Create
serve-static-files-gorilla-mux.go
, where we will create a Gorilla Mux router instead of an HTTPFileServer
, as follows:
package main import ( "html/template" "log" "net/http" "github.com/gorilla/mux" ) const ( CONN_HOST = "localhost" CONN_PORT = "8080" ) type Person struct { Id string Name string } func renderTemplate(w http.ResponseWriter, r *http.Request) ...