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 type ( | |
|
Vadim Sh.
2016/06/16 22:47:14
nit: it's the first chunk of code that uses type (
nishanths
2016/06/16 23:33:00
Done.
| |
| 8 // Handler is the type for all request handlers. | |
| 9 Handler func(*Context) | |
| 10 | |
| 11 // Middleware is a function that accepts a shared context and the next | |
| 12 // function. Since Middleware is typically part of a chain of functions | |
| 13 // that handles an HTTP request, it must obey the following rules. | |
| 14 // | |
| 15 // - Middleware must call next if it has not written to the Context | |
| 16 // by the end of the function. | |
| 17 // - Middleware must not call next if it has written to the Context. | |
| 18 // - Middleware must not write to the Context after next is called a nd | |
| 19 // c.Written()==true. | |
| 20 // - Middleware may modify the embedded context before calling next. | |
| 21 Middleware func(c *Context, next Handler) | |
| 22 | |
| 23 // MiddlewareChain is a list of Middleware. | |
| 24 MiddlewareChain []Middleware | |
| 25 ) | |
| 26 | |
| 27 // Run executes the middleware chain and final handler with the given context as | |
| 28 // the initial context. | |
| 29 func (mc MiddlewareChain) Run(c *Context, h Handler) { | |
| 30 run(c, mc, nil, h) | |
| 31 } | |
| 32 | |
| 33 // run executes the middleware chains m and n, and the handler h using the | |
| 34 // Context c as the initial context. | |
| 35 func run(c *Context, m, n MiddlewareChain, h Handler) { | |
| 36 switch { | |
| 37 case len(m) > 0: | |
| 38 m[0](c, func(ctx *Context) { | |
| 39 run(ctx, m[1:], n, h) | |
| 40 }) | |
| 41 case len(n) > 0: | |
| 42 n[0](c, func(ctx *Context) { | |
| 43 run(ctx, m, n[1:], h) | |
|
Vadim Sh.
2016/06/16 22:47:14
nit: run(ctx, nil, n[1:], h)
(to make it clear th
nishanths
2016/06/16 23:33:00
Done.
| |
| 44 }) | |
| 45 default: | |
| 46 h(c) | |
| 47 } | |
| 48 } | |
| OLD | NEW |