Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 The LUCI Authors. All rights reserved. | |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | |
| 3 // that can be found in the LICENSE file. | |
| 4 | |
| 5 // Package router provides an HTTP router with support for middleware and | |
| 6 // subrouters. It wraps around julienschmidt/httprouter. | |
| 7 package router | |
| 8 | |
| 9 import ( | |
| 10 "net/http" | |
| 11 "strings" | |
| 12 | |
| 13 "github.com/julienschmidt/httprouter" | |
| 14 "golang.org/x/net/context" | |
| 15 ) | |
| 16 | |
| 17 // Router is the main type for the package. To create a Router, use New. | |
| 18 type Router struct { | |
| 19 hrouter *httprouter.Router | |
| 20 middleware MiddlewareChain | |
| 21 BasePath string | |
| 22 } | |
| 23 | |
| 24 // Context contains the context, response writer, request, and params shared | |
| 25 // across Middleware and Handler functions. | |
| 26 type Context struct { | |
| 27 context.Context | |
| 28 http.ResponseWriter | |
| 29 | |
| 30 Request *http.Request | |
| 31 Params httprouter.Params | |
| 32 status int | |
|
Vadim Sh.
2016/06/17 00:57:31
not needed anymore
nishanths
2016/06/17 01:48:51
Sorry I missed that. Done.
| |
| 33 } | |
| 34 | |
| 35 var ( | |
| 36 _ http.Handler = (*Router)(nil) | |
| 37 _ context.Context = (*Context)(nil) | |
| 38 _ http.ResponseWriter = (*Context)(nil) | |
| 39 ) | |
| 40 | |
| 41 // New creates a Router. | |
| 42 func New() *Router { | |
| 43 return &Router{ | |
| 44 hrouter: httprouter.New(), | |
| 45 BasePath: "/", | |
| 46 } | |
| 47 } | |
| 48 | |
| 49 // Use adds middleware chains to the group. The added middleware applies to | |
| 50 // all handlers registered on the router and to all handlers registered on | |
| 51 // routers that may be derived from the router (using Subrouter). | |
| 52 func (r *Router) Use(mc MiddlewareChain) { | |
| 53 r.middleware = append(r.middleware, mc...) | |
| 54 } | |
| 55 | |
| 56 // Subrouter creates a new router with an updated base path. | |
| 57 // The new router copies middleware and configuration from the | |
| 58 // router it derives from. | |
| 59 func (r *Router) Subrouter(relativePath string) *Router { | |
| 60 newRouter := &Router{ | |
| 61 hrouter: r.hrouter, | |
| 62 BasePath: makeBasePath(r.BasePath, relativePath), | |
| 63 } | |
| 64 if len(r.middleware) > 0 { | |
| 65 newRouter.middleware = make(MiddlewareChain, len(r.middleware)) | |
| 66 copy(newRouter.middleware, r.middleware) | |
| 67 } | |
| 68 return newRouter | |
| 69 } | |
| 70 | |
| 71 // GET is a shortcut for router.Handle("GET", path, mc, h) | |
| 72 func (r *Router) GET(path string, mc MiddlewareChain, h Handler) { | |
|
Vadim Sh.
2016/06/17 01:07:52
also, what do you feel about adding a bit more mag
nishanths
2016/06/17 01:48:51
panic if we cannot coerce to the types we expect?
Vadim Sh.
2016/06/17 01:58:14
Yeah... Explicit types are probably better than ma
nodir
2016/06/17 02:00:09
yes
| |
| 73 r.Handle("GET", path, mc, h) | |
| 74 } | |
| 75 | |
| 76 // HEAD is a shortcut for router.Handle("HEAD", path, mc, h) | |
| 77 func (r *Router) HEAD(path string, mc MiddlewareChain, h Handler) { | |
| 78 r.Handle("HEAD", path, mc, h) | |
| 79 } | |
| 80 | |
| 81 // OPTIONS is a shortcut for router.Handle("OPTIONS", path, mc, h) | |
| 82 func (r *Router) OPTIONS(path string, mc MiddlewareChain, h Handler) { | |
| 83 r.Handle("OPTIONS", path, mc, h) | |
| 84 } | |
| 85 | |
| 86 // POST is a shortcut for router.Handle("POST", path, mc, h) | |
| 87 func (r *Router) POST(path string, mc MiddlewareChain, h Handler) { | |
| 88 r.Handle("POST", path, mc, h) | |
| 89 } | |
| 90 | |
| 91 // PUT is a shortcut for router.Handle("PUT", path, mc, h) | |
| 92 func (r *Router) PUT(path string, mc MiddlewareChain, h Handler) { | |
| 93 r.Handle("PUT", path, mc, h) | |
| 94 } | |
| 95 | |
| 96 // PATCH is a shortcut for router.Handle("PATCH", path, mc, h) | |
| 97 func (r *Router) PATCH(path string, mc MiddlewareChain, h Handler) { | |
| 98 r.Handle("PATCH", path, mc, h) | |
| 99 } | |
| 100 | |
| 101 // DELETE is a shortcut for router.Handle("DELETE", path, mc, h) | |
| 102 func (r *Router) DELETE(path string, mc MiddlewareChain, h Handler) { | |
| 103 r.Handle("DELETE", path, mc, h) | |
| 104 } | |
| 105 | |
| 106 // Handle registers a middleware chain and a handler for the given method and | |
| 107 // path. len(mc)==0 is allowed. See https://godoc.org/github.com/julienschmidt/h ttprouter | |
| 108 // for documentation on how the path may be formatted. | |
| 109 func (r *Router) Handle(method, path string, mc MiddlewareChain, h Handler) { | |
| 110 handle := r.adapt(mc, h) | |
| 111 r.hrouter.Handle(method, httprouter.CleanPath(r.BasePath+path), handle) | |
| 112 } | |
| 113 | |
| 114 // ServeHTTP makes Router implement the http.Handler interface. | |
| 115 func (r *Router) ServeHTTP(rw http.ResponseWriter, req *http.Request) { | |
| 116 r.hrouter.ServeHTTP(rw, req) | |
| 117 } | |
| 118 | |
| 119 // adapt adapts given middleware chain and handler into a httprouter-style handl e. | |
| 120 func (r *Router) adapt(mc MiddlewareChain, h Handler) httprouter.Handle { | |
| 121 return httprouter.Handle(func(rw http.ResponseWriter, req *http.Request, p httprouter.Params) { | |
| 122 run(&Context{ | |
| 123 Context: context.Background(), | |
| 124 ResponseWriter: rw, | |
| 125 Request: req, | |
| 126 Params: p, | |
| 127 }, r.middleware, mc, h) | |
| 128 }) | |
| 129 } | |
| 130 | |
| 131 func makeBasePath(base, relative string) string { | |
| 132 path := httprouter.CleanPath(base + relative) | |
| 133 if !strings.HasSuffix(path, "/") { | |
| 134 path += "/" | |
| 135 } | |
| 136 return path | |
| 137 } | |
| OLD | NEW |