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 | |
| 12 "github.com/julienschmidt/httprouter" | |
| 13 "golang.org/x/net/context" | |
| 14 ) | |
| 15 | |
| 16 type ( | |
| 17 // Router is the main type for the package. To create a Router, use New. | |
| 18 Router struct { | |
| 19 hrouter *httprouter.Router | |
| 20 middleware MiddlewareChain | |
| 21 BasePath string | |
| 22 } | |
| 23 | |
| 24 // Context contains the context, response writer, request, and params sh ared | |
| 25 // across Middleware and Handler functions. | |
| 26 Context struct { | |
| 27 context.Context | |
| 28 writer http.ResponseWriter | |
| 29 Request *http.Request | |
| 30 Params httprouter.Params | |
| 31 status int | |
| 32 } | |
| 33 ) | |
| 34 | |
| 35 var ( | |
| 36 _ http.Handler = (*Router)(nil) | |
| 37 _ http.ResponseWriter = (*Context)(nil) | |
| 38 _ context.Context = (*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) { | |
| 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 // TODO(maybe): Use a free list for making Contexts. | |
| 123 run(&Context{ | |
| 124 Context: context.Background(), | |
| 125 writer: rw, | |
| 126 Request: req, | |
| 127 Params: p, | |
| 128 }, r.middleware, mc, h) | |
| 129 }) | |
| 130 } | |
| 131 | |
| 132 func makeBasePath(base, relative string) string { | |
| 133 path := httprouter.CleanPath(base + relative) | |
| 134 // After CleanPath, len(path) >= 0. | |
| 135 if path[len(path)-1] != '/' { | |
|
nodir
2016/06/16 04:20:25
I am not really sure this is correct because the a
nishanths
2016/06/16 22:11:26
Done.
| |
| 136 path = path + "/" | |
|
nodir
2016/06/16 04:20:25
`+=`?
nishanths
2016/06/16 22:11:26
Done.
| |
| 137 } | |
| 138 return path | |
| 139 } | |
| 140 | |
| 141 // Header is used to implement http.ResponseWriter interface. | |
| 142 func (c *Context) Header() http.Header { | |
| 143 return c.writer.Header() | |
| 144 } | |
| 145 | |
| 146 // Write is used to implement http.ResponseWriter interface. | |
| 147 // If called from a Middleware function, the rules mentioned in the | |
| 148 // documentation for Middleware must be followed. | |
| 149 func (c *Context) Write(p []byte) (int, error) { | |
| 150 if !c.Written() { | |
| 151 c.status = http.StatusOK | |
| 152 } | |
| 153 return c.writer.Write(p) | |
| 154 } | |
| 155 | |
| 156 // WriteHeader is used to implement http.ResponseWriter interface. | |
| 157 // If called from a Middleware function, the rules mentioned in the | |
| 158 // documentation for Middleware must be followed. | |
| 159 func (c *Context) WriteHeader(code int) { | |
| 160 if !c.Written() { | |
| 161 c.status = code | |
| 162 } | |
| 163 c.writer.WriteHeader(code) | |
| 164 } | |
| 165 | |
| 166 // StatusCode returns the written HTTP status code or 0 if it was not written. | |
| 167 func (c *Context) StatusCode() int { | |
| 168 return c.status | |
| 169 } | |
| 170 | |
| 171 // Written indicates whether a HTTP response has been written. | |
| 172 func (c *Context) Written() bool { | |
| 173 return c.status != 0 | |
| 174 } | |
| OLD | NEW |