Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(327)

Unified Diff: server/router/router.go

Issue 2927093003: Add a way to set a NotFound HTTP handler. (Closed)
Patch Set: Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: server/router/router.go
diff --git a/server/router/router.go b/server/router/router.go
index 0f441c7676f9337a002258b09c7ebfef8e9db1ac..e7be323cd705058951d1b7aa9fa8b213aa88c32c 100644
--- a/server/router/router.go
+++ b/server/router/router.go
@@ -118,6 +118,14 @@ func (r *Router) GetParams(method, path string) (httprouter.Params, bool) {
return nil, false
}
+// NotFound sets the handler to be called when no matching route is found.
+func (r *Router) NotFound(mc MiddlewareChain, h Handler) {
+ handle := r.adapt(mc, h)
+ r.hrouter.NotFound = http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
+ handle(rw, req, nil)
+ })
+}
+
// adapt adapts given middleware chain and handler into a httprouter-style handle.
func (r *Router) adapt(mc MiddlewareChain, h Handler) httprouter.Handle {
return httprouter.Handle(func(rw http.ResponseWriter, req *http.Request, p httprouter.Params) {
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698