Created
September 20, 2014 04:18
-
-
Save Gurpartap/04a8da84544d0da32693 to your computer and use it in GitHub Desktop.
gorilla mux negroni routes abstraction
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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