Last Updated: February 25, 2016
· wari

Negroni with go-bindata

negroni.Classic() is useful if you need to cook up something quick with a working public/ directory serving. What if you want to compile the assets with go-bindata and serve everything up in a single binary?

You can do this easily with go-bindata-assetfs that works like an http.Fileserver(), but you go have to recompose the negroni.Classic() call to the following:

package main

import (

func myNegroni() *negroni.Negroni {
    return negroni.New(negroni.NewRecovery(), negroni.NewLogger(), negroni.NewStatic(assetFS()))

func main() {
    r := mux.NewRouter()
    r.HandleFunc("/about", aboutHandler)
    r.HandleFunc("/view/{id}", viewHandler)
    // etc...

    n := myNegroni()


Of course, this assumes that you've compiled your public/ directory with the following:

$ go-bindata-assetfs public/...

You can also compile with the -debug flag so that it uses the actual files, but provides the same interface for you so that you don't have to recompile every time you changed the html/js/css/images, etc.