| OLD | NEW |
| 1 // Copyright 2016 The LUCI Authors. All rights reserved. | 1 // Copyright 2016 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
| 4 | 4 |
| 5 // Package router provides an HTTP router with support for middleware and | 5 // Package router provides an HTTP router with support for middleware and |
| 6 // subrouters. It wraps around julienschmidt/httprouter. | 6 // subrouters. It wraps around julienschmidt/httprouter. |
| 7 package router | 7 package router |
| 8 | 8 |
| 9 import ( | 9 import ( |
| 10 "net/http" | 10 "net/http" |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 111 // GetParams parases the httprouter.Params from the supplied method and path. | 111 // GetParams parases the httprouter.Params from the supplied method and path. |
| 112 // | 112 // |
| 113 // If nothing is registered for method/path, GetParams will return false. | 113 // If nothing is registered for method/path, GetParams will return false. |
| 114 func (r *Router) GetParams(method, path string) (httprouter.Params, bool) { | 114 func (r *Router) GetParams(method, path string) (httprouter.Params, bool) { |
| 115 if h, p, _ := r.hrouter.Lookup(method, path); h != nil { | 115 if h, p, _ := r.hrouter.Lookup(method, path); h != nil { |
| 116 return p, true | 116 return p, true |
| 117 } | 117 } |
| 118 return nil, false | 118 return nil, false |
| 119 } | 119 } |
| 120 | 120 |
| 121 // NotFound sets the handler to be called when no matching route is found. |
| 122 func (r *Router) NotFound(mc MiddlewareChain, h Handler) { |
| 123 handle := r.adapt(mc, h) |
| 124 r.hrouter.NotFound = http.HandlerFunc(func(rw http.ResponseWriter, req *
http.Request) { |
| 125 handle(rw, req, nil) |
| 126 }) |
| 127 } |
| 128 |
| 121 // adapt adapts given middleware chain and handler into a httprouter-style handl
e. | 129 // adapt adapts given middleware chain and handler into a httprouter-style handl
e. |
| 122 func (r *Router) adapt(mc MiddlewareChain, h Handler) httprouter.Handle { | 130 func (r *Router) adapt(mc MiddlewareChain, h Handler) httprouter.Handle { |
| 123 return httprouter.Handle(func(rw http.ResponseWriter, req *http.Request,
p httprouter.Params) { | 131 return httprouter.Handle(func(rw http.ResponseWriter, req *http.Request,
p httprouter.Params) { |
| 124 runChains(&Context{ | 132 runChains(&Context{ |
| 125 Context: context.Background(), | 133 Context: context.Background(), |
| 126 Writer: rw, | 134 Writer: rw, |
| 127 Request: req, | 135 Request: req, |
| 128 Params: p, | 136 Params: p, |
| 129 }, r.middleware, mc, h) | 137 }, r.middleware, mc, h) |
| 130 }) | 138 }) |
| 131 } | 139 } |
| 132 | 140 |
| 133 // makeBasePath combines the given base and relative path using "/". | 141 // makeBasePath combines the given base and relative path using "/". |
| 134 // The result is: "/"+base+"/"+relative. Consecutive "/" are collapsed | 142 // The result is: "/"+base+"/"+relative. Consecutive "/" are collapsed |
| 135 // into a single "/". In addition, the following rules apply: | 143 // into a single "/". In addition, the following rules apply: |
| 136 // - The "/" between base and relative exists only if either base has a | 144 // - The "/" between base and relative exists only if either base has a |
| 137 // trailing "/" or relative is not the empty string. | 145 // trailing "/" or relative is not the empty string. |
| 138 // - A trailing "/" is added to the result if relative has a trailing | 146 // - A trailing "/" is added to the result if relative has a trailing |
| 139 // "/". | 147 // "/". |
| 140 func makeBasePath(base, relative string) string { | 148 func makeBasePath(base, relative string) string { |
| 141 if !strings.HasSuffix(base, "/") && relative != "" { | 149 if !strings.HasSuffix(base, "/") && relative != "" { |
| 142 base += "/" | 150 base += "/" |
| 143 } | 151 } |
| 144 return httprouter.CleanPath(base + relative) | 152 return httprouter.CleanPath(base + relative) |
| 145 } | 153 } |
| OLD | NEW |