Skip to content

Instantly share code, notes, and snippets.

@alexedwards
Last active January 25, 2016 02:07
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save alexedwards/4d20c505f389597c3360 to your computer and use it in GitHub Desktop.
Save alexedwards/4d20c505f389597c3360 to your computer and use it in GitHub Desktop.
Illustration of using httprouter and stack
package main
import (
"fmt"
"github.com/alexedwards/stack"
"github.com/julienschmidt/httprouter"
"net/http"
)
func main() {
router := httprouter.New()
stdStack := stack.New(tokenMiddleware)
router.GET("/hello/:forename", InjectParams(stdStack.Then(helloHandler)))
http.ListenAndServe(":3000", router)
}
func InjectParams(hc stack.HandlerChain) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
newHandlerChain := stack.Inject(hc, "params", ps)
newHandlerChain.ServeHTTP(w, r)
}
}
func tokenMiddleware(ctx *stack.Context, next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx.Put("token", "c9e452805dee5044ba520198628abcaa")
next.ServeHTTP(w, r)
})
}
func helloHandler(ctx *stack.Context, w http.ResponseWriter, r *http.Request) {
params, ok := ctx.Get("params").(httprouter.Params)
if !ok {
http.Error(w, http.StatusText(500), 500)
return
}
token, ok := ctx.Get("token").(string)
if !ok {
http.Error(w, http.StatusText(500), 500)
return
}
fmt.Fprintf(w, "Hello %s. Your token is %s.", params.ByName("forename"), token)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment