Skip to content

Instantly share code, notes, and snippets.

@Gurpartap
Created September 20, 2014 04:18
Show Gist options
  • Save Gurpartap/04a8da84544d0da32693 to your computer and use it in GitHub Desktop.
Save Gurpartap/04a8da84544d0da32693 to your computer and use it in GitHub Desktop.
gorilla mux negroni routes abstraction
package main
import "net/http"
type Route struct {
Method string
Path string
HandlerFunc func(http.ResponseWriter, *http.Request)
}
func (app *App) AddRoute(route Route) {
app.router.Path(route.Path).Methods(route.Method).HandlerFunc(route.HandlerFunc)
}
func (app *App) AddRoutes(routes []Route) {
for _, route := range routes {
app.AddRoute(route)
}
}
func IndexHandler(rw http.ResponseWriter, req *http.Request) {
rw.Write([]byte("hellow world"))
}
func PingHandler(rw http.ResponseWriter, req *http.Request) {
rw.Write([]byte("pong"))
}
func (app *App) SetupRoutes() {
app.AddRoutes([]Route{
{"GET", "/", IndexHandler},
{"GET", "/ping", PingHandler},
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment