1package route 2 3import ( 4 "net/http" 5 6 "github.com/julienschmidt/httprouter" 7 "golang.org/x/net/context" 8) 9 10type param string 11 12// Param returns param p for the context. 13func Param(ctx context.Context, p string) string { 14 return ctx.Value(param(p)).(string) 15} 16 17// WithParam returns a new context with param p set to v. 18func WithParam(ctx context.Context, p, v string) context.Context { 19 return context.WithValue(ctx, param(p), v) 20} 21 22// Router wraps httprouter.Router and adds support for prefixed sub-routers, 23// per-request context injections and instrumentation. 24type Router struct { 25 rtr *httprouter.Router 26 prefix string 27 instrh func(handlerName string, handler http.HandlerFunc) http.HandlerFunc 28} 29 30// New returns a new Router. 31func New() *Router { 32 return &Router{ 33 rtr: httprouter.New(), 34 } 35} 36 37// WithInstrumentation returns a router with instrumentation support. 38func (r *Router) WithInstrumentation(instrh func(handlerName string, handler http.HandlerFunc) http.HandlerFunc) *Router { 39 return &Router{rtr: r.rtr, prefix: r.prefix, instrh: instrh} 40} 41 42// WithPrefix returns a router that prefixes all registered routes with prefix. 43func (r *Router) WithPrefix(prefix string) *Router { 44 return &Router{rtr: r.rtr, prefix: r.prefix + prefix, instrh: r.instrh} 45} 46 47// handle turns a HandlerFunc into an httprouter.Handle. 48func (r *Router) handle(handlerName string, h http.HandlerFunc) httprouter.Handle { 49 if r.instrh != nil { 50 // This needs to be outside the closure to avoid data race when reading and writing to 'h'. 51 h = r.instrh(handlerName, h) 52 } 53 return func(w http.ResponseWriter, req *http.Request, params httprouter.Params) { 54 ctx, cancel := context.WithCancel(req.Context()) 55 defer cancel() 56 57 for _, p := range params { 58 ctx = context.WithValue(ctx, param(p.Key), p.Value) 59 } 60 h(w, req.WithContext(ctx)) 61 } 62} 63 64// Get registers a new GET route. 65func (r *Router) Get(path string, h http.HandlerFunc) { 66 r.rtr.GET(r.prefix+path, r.handle(path, h)) 67} 68 69// Options registers a new OPTIONS route. 70func (r *Router) Options(path string, h http.HandlerFunc) { 71 r.rtr.OPTIONS(r.prefix+path, r.handle(path, h)) 72} 73 74// Del registers a new DELETE route. 75func (r *Router) Del(path string, h http.HandlerFunc) { 76 r.rtr.DELETE(r.prefix+path, r.handle(path, h)) 77} 78 79// Put registers a new PUT route. 80func (r *Router) Put(path string, h http.HandlerFunc) { 81 r.rtr.PUT(r.prefix+path, r.handle(path, h)) 82} 83 84// Post registers a new POST route. 85func (r *Router) Post(path string, h http.HandlerFunc) { 86 r.rtr.POST(r.prefix+path, r.handle(path, h)) 87} 88 89// Redirect takes an absolute path and sends an internal HTTP redirect for it, 90// prefixed by the router's path prefix. Note that this method does not include 91// functionality for handling relative paths or full URL redirects. 92func (r *Router) Redirect(w http.ResponseWriter, req *http.Request, path string, code int) { 93 http.Redirect(w, req, r.prefix+path, code) 94} 95 96// ServeHTTP implements http.Handler. 97func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) { 98 r.rtr.ServeHTTP(w, req) 99} 100 101// FileServe returns a new http.HandlerFunc that serves files from dir. 102// Using routes must provide the *filepath parameter. 103func FileServe(dir string) http.HandlerFunc { 104 fs := http.FileServer(http.Dir(dir)) 105 106 return func(w http.ResponseWriter, r *http.Request) { 107 r.URL.Path = Param(r.Context(), "filepath") 108 fs.ServeHTTP(w, r) 109 } 110} 111