| 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.Context |
| 28 Writer http.ResponseWriter |
| 29 Request *http.Request |
| 30 Params httprouter.Params |
| 31 } |
| 32 |
| 33 var _ http.Handler = (*Router)(nil) |
| 34 |
| 35 // New creates a Router. |
| 36 func New() *Router { |
| 37 return &Router{ |
| 38 hrouter: httprouter.New(), |
| 39 BasePath: "/", |
| 40 } |
| 41 } |
| 42 |
| 43 // Use adds middleware chains to the group. The added middleware applies to |
| 44 // all handlers registered on the router and to all handlers registered on |
| 45 // routers that may be derived from the router (using Subrouter). |
| 46 func (r *Router) Use(mc MiddlewareChain) { |
| 47 r.middleware = append(r.middleware, mc...) |
| 48 } |
| 49 |
| 50 // Subrouter creates a new router with an updated base path. |
| 51 // The new router copies middleware and configuration from the |
| 52 // router it derives from. |
| 53 func (r *Router) Subrouter(relativePath string) *Router { |
| 54 newRouter := &Router{ |
| 55 hrouter: r.hrouter, |
| 56 BasePath: makeBasePath(r.BasePath, relativePath), |
| 57 } |
| 58 if len(r.middleware) > 0 { |
| 59 newRouter.middleware = make(MiddlewareChain, len(r.middleware)) |
| 60 copy(newRouter.middleware, r.middleware) |
| 61 } |
| 62 return newRouter |
| 63 } |
| 64 |
| 65 // GET is a shortcut for router.Handle("GET", path, mc, h) |
| 66 func (r *Router) GET(path string, mc MiddlewareChain, h Handler) { |
| 67 r.Handle("GET", path, mc, h) |
| 68 } |
| 69 |
| 70 // HEAD is a shortcut for router.Handle("HEAD", path, mc, h) |
| 71 func (r *Router) HEAD(path string, mc MiddlewareChain, h Handler) { |
| 72 r.Handle("HEAD", path, mc, h) |
| 73 } |
| 74 |
| 75 // OPTIONS is a shortcut for router.Handle("OPTIONS", path, mc, h) |
| 76 func (r *Router) OPTIONS(path string, mc MiddlewareChain, h Handler) { |
| 77 r.Handle("OPTIONS", path, mc, h) |
| 78 } |
| 79 |
| 80 // POST is a shortcut for router.Handle("POST", path, mc, h) |
| 81 func (r *Router) POST(path string, mc MiddlewareChain, h Handler) { |
| 82 r.Handle("POST", path, mc, h) |
| 83 } |
| 84 |
| 85 // PUT is a shortcut for router.Handle("PUT", path, mc, h) |
| 86 func (r *Router) PUT(path string, mc MiddlewareChain, h Handler) { |
| 87 r.Handle("PUT", path, mc, h) |
| 88 } |
| 89 |
| 90 // PATCH is a shortcut for router.Handle("PATCH", path, mc, h) |
| 91 func (r *Router) PATCH(path string, mc MiddlewareChain, h Handler) { |
| 92 r.Handle("PATCH", path, mc, h) |
| 93 } |
| 94 |
| 95 // DELETE is a shortcut for router.Handle("DELETE", path, mc, h) |
| 96 func (r *Router) DELETE(path string, mc MiddlewareChain, h Handler) { |
| 97 r.Handle("DELETE", path, mc, h) |
| 98 } |
| 99 |
| 100 // Handle registers a middleware chain and a handler for the given method and |
| 101 // path. len(mc)==0 is allowed. See https://godoc.org/github.com/julienschmidt/h
ttprouter |
| 102 // for documentation on how the path may be formatted. |
| 103 func (r *Router) Handle(method, path string, mc MiddlewareChain, h Handler) { |
| 104 handle := r.adapt(mc, h) |
| 105 r.hrouter.Handle(method, makeBasePath(r.BasePath, path), handle) |
| 106 } |
| 107 |
| 108 // ServeHTTP makes Router implement the http.Handler interface. |
| 109 func (r *Router) ServeHTTP(rw http.ResponseWriter, req *http.Request) { |
| 110 r.hrouter.ServeHTTP(rw, req) |
| 111 } |
| 112 |
| 113 // adapt adapts given middleware chain and handler into a httprouter-style handl
e. |
| 114 func (r *Router) adapt(mc MiddlewareChain, h Handler) httprouter.Handle { |
| 115 return httprouter.Handle(func(rw http.ResponseWriter, req *http.Request,
p httprouter.Params) { |
| 116 run(&Context{ |
| 117 Context: context.Background(), |
| 118 Writer: rw, |
| 119 Request: req, |
| 120 Params: p, |
| 121 }, r.middleware, mc, h) |
| 122 }) |
| 123 } |
| 124 |
| 125 // makeBasePath combines the given base and relative path using "/". |
| 126 // The result is: "/"+base+"/"+relative. Consecutive "/" are collapsed |
| 127 // into a single "/". In addition, the following rules apply: |
| 128 // - The "/" between base and relative exists only if either base has a |
| 129 // trailing "/" or relative is not the empty string. |
| 130 // - A trailing "/" is added to the result if relative has a trailing |
| 131 // "/". |
| 132 func makeBasePath(base, relative string) string { |
| 133 if !strings.HasSuffix(base, "/") && relative != "" { |
| 134 base += "/" |
| 135 } |
| 136 return httprouter.CleanPath(base + relative) |
| 137 } |
| OLD | NEW |