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 | |
| 6 | |
| 7 // Handler is the type for all request handlers. | |
| 8 type Handler func(*Context) | |
| 9 | |
| 10 // Middleware is a function that accepts a shared context and the next | |
| 11 // function. Since Middleware is typically part of a chain of functions | |
| 12 // that handles an HTTP request, it must obey the following rules. | |
| 13 // | |
| 14 // - Middleware must call next if it has not written to the Context | |
| 15 // by the end of the function. | |
| 16 // - Middleware must not call next if it has written to the Context. | |
| 17 // - Middleware must not write to the Context after next is called and | |
| 18 // the Context has been written to. | |
| 19 // - Middleware may modify the embedded context before calling next. | |
| 20 type Middleware func(c *Context, next Handler) | |
| 21 | |
| 22 // MiddlewareChain is a list of Middleware. | |
| 23 type MiddlewareChain []Middleware | |
| 24 | |
| 25 // VoidMiddleware is middleware that simply calls next with the context. | |
| 26 var VoidMiddleware = func(c *Context, next Handler) { | |
|
Vadim Sh.
2016/06/18 16:57:20
what do you think about teaching 'run' to recogniz
nishanths
2016/06/19 02:47:02
Good idea; not too surprising to users either. Don
| |
| 27 next(c) | |
| 28 } | |
| 29 | |
| 30 // run executes the middleware chains m and n, and the handler h using the | |
| 31 // Context c as the initial context. | |
| 32 func run(c *Context, m, n MiddlewareChain, h Handler) { | |
| 33 switch { | |
| 34 case len(m) > 0: | |
| 35 m[0](c, func(ctx *Context) { | |
| 36 run(ctx, m[1:], n, h) | |
| 37 }) | |
| 38 case len(n) > 0: | |
| 39 n[0](c, func(ctx *Context) { | |
| 40 run(ctx, nil, n[1:], h) | |
| 41 }) | |
| 42 default: | |
| 43 h(c) | |
| 44 } | |
| 45 } | |
| OLD | NEW |