OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 package middleware | |
6 | |
7 import ( | |
8 "net/http" | |
9 | |
10 "github.com/julienschmidt/httprouter" | |
11 "github.com/luci/gae/impl/memory" | |
12 "github.com/luci/gae/impl/prod" | |
13 "github.com/luci/luci-go/appengine/gaelogger" | |
14 "github.com/luci/luci-go/common/logging/memlogger" | |
15 "golang.org/x/net/context" | |
16 ) | |
17 | |
18 // Handler is the type for all middleware handlers. Of particular note, it's the | |
19 // same has httprouder.Handle, except that it also has a context parameter. | |
Vadim Sh.
2015/10/13 01:29:14
typo: same as?
iannucci
2015/10/13 01:37:17
yup
| |
20 type Handler func(context.Context, http.ResponseWriter, *http.Request, httproute r.Params) | |
21 | |
22 // Base adapts a middleware-style handler to a httprouter.Handle. It passes | |
23 // a new, empty context to `h`. | |
24 func Base(h Handler) httprouter.Handle { | |
dnj
2015/10/13 01:44:49
Make this take context.Context. context.Background
iannucci
2015/10/13 01:56:34
I think you misunderstand the point of this. This
dnj
2015/10/13 04:39:10
Context generation shouldn't have to start at the
iannucci
2015/10/13 04:52:29
Ah, ah, ok... I misunderstood you.
The amount of
| |
25 return func(rw http.ResponseWriter, r *http.Request, p httprouter.Params ) { | |
26 h(context.Background(), rw, r, p) | |
27 } | |
28 } | |
29 | |
30 // BaseProd adapts a middleware-style handler to a httprouter.Handle. It passes | |
31 // a new context to `h` with the following services installed: | |
32 // * github.com/luci/gae/impl/prod (production appengine services) | |
33 // * github.com/luci/luci-go/appengine/gaelogger (appengine logging service) | |
34 func BaseProd(h Handler) httprouter.Handle { | |
35 return func(rw http.ResponseWriter, r *http.Request, p httprouter.Params ) { | |
36 h(gaelogger.Use(prod.UseRequest(r)), rw, r, p) | |
37 } | |
38 } | |
39 | |
40 // BaseTest adapts a middleware-style handler to a httprouter.Handle. It passes | |
41 // a new context to `h` with the following services installed: | |
42 // * github.com/luci/gae/impl/memory (in-memory appengine services) | |
43 // * github.com/luci/luci-go/common/logging/memlogger (in-memory logging servi ce) | |
44 func BaseTest(h Handler) httprouter.Handle { | |
45 return func(rw http.ResponseWriter, r *http.Request, p httprouter.Params ) { | |
46 h(memlogger.Use(memory.Use(context.Background())), rw, r, p) | |
47 } | |
48 } | |
OLD | NEW |