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

Side by Side Diff: server/router/handler.go

Issue 2043423004: Make HTTP middleware easier to use (Closed) Base URL: https://github.com/luci/luci-go@master
Patch Set: router: remove unecessary type assertion in tests Created 4 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 unified diff | Download patch
OLDNEW
(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 }
OLDNEW
« no previous file with comments | « server/router/example_test.go ('k') | server/router/router.go » ('j') | server/router/router.go » ('J')

Powered by Google App Engine
This is Rietveld 408576698