Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The LUCI Authors. All rights reserved. | 1 // Copyright 2015 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
| 4 | 4 |
| 5 package gaemiddleware | 5 package gaemiddleware |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "net/http" | |
| 9 | |
| 10 "google.golang.org/appengine" | 8 "google.golang.org/appengine" |
| 11 | 9 |
| 12 "github.com/julienschmidt/httprouter" | |
| 13 "golang.org/x/net/context" | |
| 14 | |
| 15 "github.com/luci/gae/filter/dscache" | 10 "github.com/luci/gae/filter/dscache" |
| 16 "github.com/luci/gae/impl/prod" | 11 "github.com/luci/gae/impl/prod" |
| 17 "github.com/luci/luci-go/appengine/gaeauth/client" | 12 "github.com/luci/luci-go/appengine/gaeauth/client" |
| 18 "github.com/luci/luci-go/appengine/gaeauth/server" | 13 "github.com/luci/luci-go/appengine/gaeauth/server" |
| 19 "github.com/luci/luci-go/appengine/gaeauth/server/gaesigner" | 14 "github.com/luci/luci-go/appengine/gaeauth/server/gaesigner" |
| 20 "github.com/luci/luci-go/appengine/gaesecrets" | 15 "github.com/luci/luci-go/appengine/gaesecrets" |
| 21 "github.com/luci/luci-go/appengine/gaesettings" | 16 "github.com/luci/luci-go/appengine/gaesettings" |
| 22 "github.com/luci/luci-go/appengine/tsmon" | 17 "github.com/luci/luci-go/appengine/tsmon" |
| 23 "github.com/luci/luci-go/common/cacheContext" | 18 "github.com/luci/luci-go/common/cacheContext" |
| 24 "github.com/luci/luci-go/common/logging" | 19 "github.com/luci/luci-go/common/logging" |
| 25 "github.com/luci/luci-go/server/auth" | 20 "github.com/luci/luci-go/server/auth" |
| 26 "github.com/luci/luci-go/server/middleware" | 21 "github.com/luci/luci-go/server/middleware" |
| 27 "github.com/luci/luci-go/server/proccache" | 22 "github.com/luci/luci-go/server/proccache" |
| 23 "github.com/luci/luci-go/server/router" | |
| 28 "github.com/luci/luci-go/server/settings" | 24 "github.com/luci/luci-go/server/settings" |
| 29 ) | 25 ) |
| 30 | 26 |
| 31 var ( | 27 var ( |
| 32 // globalProcessCache holds state cached between requests. | 28 // globalProcessCache holds state cached between requests. |
| 33 globalProcessCache = &proccache.Cache{} | 29 globalProcessCache = &proccache.Cache{} |
| 34 | 30 |
| 35 // globalSettings holds global app settings lazily updated from the data store. | 31 // globalSettings holds global app settings lazily updated from the data store. |
| 36 globalSettings = settings.New(gaesettings.Storage{}) | 32 globalSettings = settings.New(gaesettings.Storage{}) |
| 37 | 33 |
| 38 // globalAuthDBCache knows how to fetch auth.DB from datastore and keep it | 34 // globalAuthDBCache knows how to fetch auth.DB from datastore and keep it |
| 39 // in local memory cache. Used in prod contexts only. | 35 // in local memory cache. Used in prod contexts only. |
| 40 globalAuthDBCache = auth.NewDBCache(server.GetAuthDB) | 36 globalAuthDBCache = auth.NewDBCache(server.GetAuthDB) |
| 41 | 37 |
| 42 // globalTsMonState holds state related to time series monitoring. | 38 // globalTsMonState holds state related to time series monitoring. |
| 43 globalTsMonState = &tsmon.State{} | 39 globalTsMonState = &tsmon.State{} |
| 44 ) | 40 ) |
| 45 | 41 |
| 46 // WithProd installs the set of standard production AppEngine services: | 42 // WithProd installs the set of standard production AppEngine services: |
| 47 // * github.com/luci/luci-go/common/logging (set default level to debug). | 43 // * github.com/luci/luci-go/common/logging (set default level to debug). |
| 48 // * github.com/luci/gae/impl/prod (production appengine services) | 44 // * github.com/luci/gae/impl/prod (production appengine services) |
| 49 // * github.com/luci/luci-go/appengine/gaeauth/client (appengine urlfetch tran sport) | 45 // * github.com/luci/luci-go/appengine/gaeauth/client (appengine urlfetch tran sport) |
| 50 // * github.com/luci/luci-go/server/proccache (in process memory cache) | 46 // * github.com/luci/luci-go/server/proccache (in process memory cache) |
| 51 // * github.com/luci/luci-go/server/settings (global app settings) | 47 // * github.com/luci/luci-go/server/settings (global app settings) |
| 52 // * github.com/luci/luci-go/appengine/gaesecrets (access to secret keys in da tastore) | 48 // * github.com/luci/luci-go/appengine/gaesecrets (access to secret keys in da tastore) |
| 53 // * github.com/luci/luci-go/appengine/gaeauth/server/gaesigner (RSA signer) | 49 // * github.com/luci/luci-go/appengine/gaeauth/server/gaesigner (RSA signer) |
| 54 // * github.com/luci/luci-go/appengine/gaeauth/server/auth (user groups databa se) | 50 // * github.com/luci/luci-go/appengine/gaeauth/server/auth (user groups databa se) |
| 55 func WithProd(c context.Context, req *http.Request) context.Context { | 51 func WithProd(c *router.Context, next router.Handler) { |
| 56 // These are needed to use fetchCachedSettings. | 52 // These are needed to use fetchCachedSettings. |
| 57 » c = logging.SetLevel(c, logging.Debug) | 53 » c.Context = logging.SetLevel(c.Context, logging.Debug) |
| 58 » c = prod.Use(c, req) | 54 » c.Context = prod.Use(c.Context, c.Request) |
| 59 » c = settings.Use(c, globalSettings) | 55 » c.Context = settings.Use(c.Context, globalSettings) |
| 60 | 56 |
| 61 // Fetch and apply configuration stored in the datastore. | 57 // Fetch and apply configuration stored in the datastore. |
| 62 » settings := fetchCachedSettings(c) | 58 » settings := fetchCachedSettings(c.Context) |
| 63 » c = logging.SetLevel(c, settings.LoggingLevel) | 59 » c.Context = logging.SetLevel(c.Context, settings.LoggingLevel) |
| 64 if !settings.DisableDSCache { | 60 if !settings.DisableDSCache { |
| 65 » » c = dscache.AlwaysFilterRDS(c, nil) | 61 » » c.Context = dscache.AlwaysFilterRDS(c.Context, nil) |
| 66 } | 62 } |
| 67 | 63 |
| 68 // The rest of the service may use applied configuration. | 64 // The rest of the service may use applied configuration. |
| 69 » c = proccache.Use(c, globalProcessCache) | 65 » c.Context = proccache.Use(c.Context, globalProcessCache) |
| 70 » c = client.UseAnonymousTransport(c) | 66 » c.Context = client.UseAnonymousTransport(c.Context) |
| 71 » c = gaesecrets.Use(c, nil) | 67 » c.Context = gaesecrets.Use(c.Context, nil) |
| 72 » c = gaesigner.Use(c) | 68 » c.Context = gaesigner.Use(c.Context) |
| 73 » c = auth.UseDB(c, globalAuthDBCache) | 69 » c.Context = auth.UseDB(c.Context, globalAuthDBCache) |
| 74 » return cacheContext.Wrap(c) | 70 » c.Context = cacheContext.Wrap(c.Context) |
| 75 } | 71 } |
| 76 | 72 |
| 77 // BaseProd adapts a middleware-style handler to a httprouter.Handle. | 73 // BaseProd returns a list of middleware: WithProd middleware, a panic catcher i f this |
| 78 // | 74 // is not a devserver, and the monitoring middleware. |
| 79 // It installs services using WithProd, installs a panic catcher if this | 75 func BaseProd() router.MiddlewareChain { |
| 80 // is not a devserver, and injects the monitoring middleware. | 76 » if appengine.IsDevAppServer() { |
| 81 func BaseProd(h middleware.Handler) httprouter.Handle { | 77 » » return router.MiddlewareChain{WithProd, globalTsMonState.Middlew are} |
| 82 » h = globalTsMonState.Middleware(h) | |
| 83 » if !appengine.IsDevAppServer() { | |
| 84 » » h = middleware.WithPanicCatcher(h) | |
| 85 } | 78 } |
| 86 » return func(rw http.ResponseWriter, r *http.Request, p httprouter.Params ) { | 79 » return router.MiddlewareChain{ |
| 87 » » h(WithProd(context.Background(), r), rw, r, p) | 80 » » WithProd, middleware.WithPanicCatcher, globalTsMonState.Middlewa re, |
| 88 } | 81 } |
| 82 | |
|
Vadim Sh.
2016/06/18 16:57:20
nit: remove this new line :)
nishanths
2016/06/19 02:47:02
Done :)
| |
| 89 } | 83 } |
| OLD | NEW |