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" | 8 "net/http" |
| 9 "os" | |
| 9 | 10 |
| 10 "golang.org/x/net/context" | 11 "golang.org/x/net/context" |
| 11 "google.golang.org/appengine" | 12 "google.golang.org/appengine" |
| 12 | 13 |
| 13 "github.com/luci/gae/filter/dscache" | 14 "github.com/luci/gae/filter/dscache" |
| 15 "github.com/luci/gae/impl/cloud" | |
| 14 "github.com/luci/gae/impl/prod" | 16 "github.com/luci/gae/impl/prod" |
| 15 "github.com/luci/gae/service/urlfetch" | 17 "github.com/luci/gae/service/urlfetch" |
| 16 | 18 |
| 17 "github.com/luci/luci-go/common/data/caching/cacheContext" | 19 "github.com/luci/luci-go/common/data/caching/cacheContext" |
| 18 "github.com/luci/luci-go/common/data/caching/proccache" | 20 "github.com/luci/luci-go/common/data/caching/proccache" |
| 19 "github.com/luci/luci-go/common/logging" | 21 "github.com/luci/luci-go/common/logging" |
| 20 | 22 |
| 21 "github.com/luci/luci-go/server/auth" | 23 "github.com/luci/luci-go/server/auth" |
| 22 "github.com/luci/luci-go/server/auth/authdb" | 24 "github.com/luci/luci-go/server/auth/authdb" |
| 23 "github.com/luci/luci-go/server/middleware" | 25 "github.com/luci/luci-go/server/middleware" |
| 24 "github.com/luci/luci-go/server/router" | 26 "github.com/luci/luci-go/server/router" |
| 25 "github.com/luci/luci-go/server/settings" | 27 "github.com/luci/luci-go/server/settings" |
| 26 | 28 |
| 27 "github.com/luci/luci-go/appengine/gaeauth/client" | 29 "github.com/luci/luci-go/appengine/gaeauth/client" |
| 28 "github.com/luci/luci-go/appengine/gaeauth/server" | 30 "github.com/luci/luci-go/appengine/gaeauth/server" |
| 29 "github.com/luci/luci-go/appengine/gaeauth/server/gaesigner" | 31 "github.com/luci/luci-go/appengine/gaeauth/server/gaesigner" |
| 30 "github.com/luci/luci-go/appengine/gaesecrets" | 32 "github.com/luci/luci-go/appengine/gaesecrets" |
| 31 "github.com/luci/luci-go/appengine/gaesettings" | 33 "github.com/luci/luci-go/appengine/gaesettings" |
| 32 "github.com/luci/luci-go/appengine/tsmon" | 34 "github.com/luci/luci-go/appengine/tsmon" |
| 33 "github.com/luci/luci-go/luci_config/appengine/gaeconfig" | 35 "github.com/luci/luci-go/luci_config/appengine/gaeconfig" |
| 36 | |
| 37 "cloud.google.com/go/datastore" | |
| 34 ) | 38 ) |
| 35 | 39 |
| 36 var ( | 40 var ( |
| 37 // globalProcessCache holds state cached between requests. | 41 // globalProcessCache holds state cached between requests. |
| 38 globalProcessCache = &proccache.Cache{} | 42 globalProcessCache = &proccache.Cache{} |
| 39 | 43 |
| 40 // globalSettings holds global app settings lazily updated from the data store. | 44 // globalSettings holds global app settings lazily updated from the data store. |
| 41 globalSettings = settings.New(gaesettings.Storage{}) | 45 globalSettings = settings.New(gaesettings.Storage{}) |
| 42 | 46 |
| 43 // globalAuthCache is used to cache various auth token. | 47 // globalAuthCache is used to cache various auth token. |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 59 AccessTokenProvider: client.GetAccessToken, | 63 AccessTokenProvider: client.GetAccessToken, |
| 60 AnonymousTransport: urlfetch.Get, | 64 AnonymousTransport: urlfetch.Get, |
| 61 Cache: globalAuthCache, | 65 Cache: globalAuthCache, |
| 62 IsDevMode: appengine.IsDevAppServer(), | 66 IsDevMode: appengine.IsDevAppServer(), |
| 63 } | 67 } |
| 64 | 68 |
| 65 // globalTsMonState holds state related to time series monitoring. | 69 // globalTsMonState holds state related to time series monitoring. |
| 66 globalTsMonState = &tsmon.State{} | 70 globalTsMonState = &tsmon.State{} |
| 67 ) | 71 ) |
| 68 | 72 |
| 73 // WithCloud installs the set of standard flexible environment services. | |
| 74 // Notably, this does not install Memcache (not available in Flex) or use DS cac he | |
| 75 // (requires memcache). | |
| 76 func WithCloud(c context.Context) context.Context { | |
| 77 c = logging.SetLevel(c, logging.Debug) | |
| 78 project := os.Getenv("GCLOUD_PROJECT") | |
|
hinoka
2017/04/17 23:31:17
This assumes we are in a GCE container.
| |
| 79 // Use the cloud datastore client. | |
| 80 dsClient, err := datastore.NewClient(c, project) | |
| 81 if err != nil { | |
| 82 panic(err) | |
| 83 } | |
| 84 c = cloud.Config{DS: dsClient}.Use(c) | |
| 85 c = settings.Use(c, globalSettings) | |
| 86 | |
| 87 // The rest of the service may use applied configuration. | |
| 88 c = proccache.Use(c, globalProcessCache) | |
| 89 c = gaeconfig.Use(c) | |
| 90 c = gaesecrets.Use(c, nil) | |
| 91 c = auth.SetConfig(c, globalAuthConfig) | |
| 92 | |
| 93 // Wrap this in a cache context so that lookups for any of the aforement ioned | |
| 94 // items are fast. | |
| 95 return cacheContext.Wrap(c) | |
| 96 } | |
| 97 | |
| 69 // WithProd adds various production GAE LUCI services to the context. | 98 // WithProd adds various production GAE LUCI services to the context. |
| 70 // | 99 // |
| 71 // Basically, it installs GAE-specific backends and caches for various | 100 // Basically, it installs GAE-specific backends and caches for various |
| 72 // subsystems to make them work in GAE environment. | 101 // subsystems to make them work in GAE environment. |
| 73 // | 102 // |
| 74 // One example is a backend for Logging: github.com/luci/luci-go/common/logging. | 103 // One example is a backend for Logging: github.com/luci/luci-go/common/logging. |
| 75 // Logs emitted through a WithProd() context go to GAE logs. | 104 // Logs emitted through a WithProd() context go to GAE logs. |
| 76 // | 105 // |
| 77 // 'Production' here means the services will use real GAE APIs (not mocks or | 106 // 'Production' here means the services will use real GAE APIs (not mocks or |
| 78 // stubs), so WithProd should never be used from unit tests. | 107 // stubs), so WithProd should never be used from unit tests. |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 125 // parent will ensure that any such resource leaks are cleaned up. | 154 // parent will ensure that any such resource leaks are cleaned up. |
| 126 var cancelFunc context.CancelFunc | 155 var cancelFunc context.CancelFunc |
| 127 c.Context, cancelFunc = context.WithCancel(c.Context) | 156 c.Context, cancelFunc = context.WithCancel(c.Context) |
| 128 defer cancelFunc() | 157 defer cancelFunc() |
| 129 | 158 |
| 130 // Apply production settings. | 159 // Apply production settings. |
| 131 c.Context = WithProd(c.Context, c.Request) | 160 c.Context = WithProd(c.Context, c.Request) |
| 132 | 161 |
| 133 next(c) | 162 next(c) |
| 134 } | 163 } |
| 164 | |
| 165 // CloudServices is a middleware that installs the set of standard production | |
| 166 // AppEngine services by calling WithCloud. | |
| 167 func CloudServices(c *router.Context, next router.Handler) { | |
| 168 // Create a cancelable Context that cancels when this request finishes. | |
| 169 // | |
| 170 // We do this because Contexts will leak resources and/or goroutines if they | |
| 171 // have timers or can be canceled, but aren't. Predictably canceling the | |
| 172 // parent will ensure that any such resource leaks are cleaned up. | |
| 173 var cancelFunc context.CancelFunc | |
| 174 c.Context, cancelFunc = context.WithCancel(c.Context) | |
| 175 defer cancelFunc() | |
| 176 | |
| 177 // Apply production settings. | |
| 178 c.Context = WithCloud(c.Context) | |
| 179 | |
| 180 next(c) | |
| 181 } | |
| 182 | |
| 183 // BaseCloud returns a list of middleware: CloudServices middleware, a panic cat cher if this | |
| 184 // is not a devserver, and the monitoring middleware. | |
| 185 func BaseCloud() router.MiddlewareChain { | |
| 186 return router.NewMiddlewareChain( | |
| 187 CloudServices, middleware.WithPanicCatcher, globalTsMonState.Mid dleware, | |
| 188 ) | |
| 189 } | |
| OLD | NEW |