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 common | 5 package common |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "net/http" | 8 "net/http" |
| 9 "strings" | 9 "strings" |
| 10 | 10 |
| 11 "cloud.google.com/go/compute/metadata" | |
| 12 | |
| 11 "golang.org/x/net/context" | 13 "golang.org/x/net/context" |
| 12 | 14 |
| 15 "github.com/luci/gae/impl/cloud" | |
| 13 "github.com/luci/gae/service/info" | 16 "github.com/luci/gae/service/info" |
| 14 "github.com/luci/luci-go/appengine/gaeauth/server" | 17 "github.com/luci/luci-go/appengine/gaeauth/server" |
| 15 "github.com/luci/luci-go/appengine/gaemiddleware" | 18 "github.com/luci/luci-go/appengine/gaemiddleware" |
| 16 "github.com/luci/luci-go/common/clock" | 19 "github.com/luci/luci-go/common/clock" |
| 20 "github.com/luci/luci-go/common/cloudlogging" | |
| 21 "github.com/luci/luci-go/common/logging/cloudlog" | |
| 17 "github.com/luci/luci-go/server/analytics" | 22 "github.com/luci/luci-go/server/analytics" |
| 18 "github.com/luci/luci-go/server/auth" | 23 "github.com/luci/luci-go/server/auth" |
| 19 "github.com/luci/luci-go/server/auth/identity" | 24 "github.com/luci/luci-go/server/auth/identity" |
| 20 "github.com/luci/luci-go/server/router" | 25 "github.com/luci/luci-go/server/router" |
| 21 "github.com/luci/luci-go/server/templates" | 26 "github.com/luci/luci-go/server/templates" |
| 22 ) | 27 ) |
| 23 | 28 |
| 29 var authconfig *auth.Config | |
| 30 | |
| 24 // GetTemplateBundles is used to render HTML templates. It provides base args | 31 // GetTemplateBundles is used to render HTML templates. It provides base args |
| 25 // passed to all templates. | 32 // passed to all templates. |
| 26 func GetTemplateBundle() *templates.Bundle { | 33 func GetTemplateBundle() *templates.Bundle { |
| 27 return &templates.Bundle{ | 34 return &templates.Bundle{ |
| 28 Loader: templates.FileSystemLoader("templates"), | 35 Loader: templates.FileSystemLoader("templates"), |
| 29 DebugMode: info.IsDevAppServer, | 36 DebugMode: info.IsDevAppServer, |
| 30 DefaultTemplate: "base", | 37 DefaultTemplate: "base", |
| 31 DefaultArgs: func(c context.Context) (templates.Args, error) { | 38 DefaultArgs: func(c context.Context) (templates.Args, error) { |
| 32 r := getRequest(c) | 39 r := getRequest(c) |
| 33 path := r.URL.Path | 40 path := r.URL.Path |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 57 | 64 |
| 58 // Base returns the basic LUCI appengine middlewares. | 65 // Base returns the basic LUCI appengine middlewares. |
| 59 func Base() router.MiddlewareChain { | 66 func Base() router.MiddlewareChain { |
| 60 return gaemiddleware.BaseProd().Extend( | 67 return gaemiddleware.BaseProd().Extend( |
| 61 auth.Authenticate(server.CookieAuth), | 68 auth.Authenticate(server.CookieAuth), |
| 62 withRequestMiddleware, | 69 withRequestMiddleware, |
| 63 templates.WithTemplates(GetTemplateBundle()), | 70 templates.WithTemplates(GetTemplateBundle()), |
| 64 ) | 71 ) |
| 65 } | 72 } |
| 66 | 73 |
| 74 // Flex returns the basic middleware for use on appengine flex. Flex does not | |
|
nodir
2017/04/29 00:59:44
s/Flex/FlexBase/
hinoka
2017/05/01 23:08:02
Done.
| |
| 75 // allow the use of appengine APIs. | |
| 76 func FlexBase() router.MiddlewareChain { | |
| 77 // Get the name of this project from the metadata server, since we're on GCE. | |
| 78 project, err := metadata.Get("project/project-id") | |
| 79 if err != nil { | |
| 80 panic(err) | |
| 81 } | |
| 82 // Use the cloud logger. | |
| 83 logger := func(c *router.Context, next router.Handler) { | |
| 84 logClient, err := cloudlogging.NewClient( | |
| 85 cloudlogging.ClientOptions{ | |
| 86 ProjectID: project, | |
| 87 LogID: "gae_app", | |
| 88 ResourceType: "gae_app", | |
| 89 }, | |
| 90 http.DefaultClient) | |
|
nodir
2017/04/29 00:59:44
i doubt cloud logging API will accept anonymous re
hinoka
2017/05/01 23:08:02
Acknowledged. This probably doesn't work, adding
| |
| 91 if err != nil { | |
| 92 panic(err) | |
| 93 } | |
| 94 cloudlog.Use(c.Context, cloudlog.Config{}, logClient) | |
|
nodir
2017/04/29 00:59:45
this is noop because it misses c.Context = ...
po
hinoka
2017/05/01 23:08:02
Oh you're right.. that explains a lot.
| |
| 95 next(c) | |
| 96 } | |
| 97 // Installs the Info and Datastore services. | |
| 98 base := func(c *router.Context, next router.Handler) { | |
| 99 c.Context = cloud.UseFlex(c.Context) | |
| 100 next(c) | |
| 101 } | |
| 102 // Now chain it all together! | |
| 103 return router.NewMiddlewareChain(logger, base) | |
|
nodir
2017/04/29 00:59:44
why logger comes before base? if you plan to log u
hinoka
2017/05/01 23:08:02
Done.
| |
| 104 } | |
| 105 | |
| 67 // The context key, so that we can embed the http.Request object into | 106 // The context key, so that we can embed the http.Request object into |
| 68 // the context. | 107 // the context. |
| 69 var requestKey = "http.request" | 108 var requestKey = "http.request" |
| 70 | 109 |
| 71 // WithRequest returns a context with the http.Request object | 110 // WithRequest returns a context with the http.Request object |
| 72 // in it. | 111 // in it. |
| 73 func WithRequest(c context.Context, r *http.Request) context.Context { | 112 func WithRequest(c context.Context, r *http.Request) context.Context { |
| 74 return context.WithValue(c, &requestKey, r) | 113 return context.WithValue(c, &requestKey, r) |
| 75 } | 114 } |
| 76 | 115 |
| 77 // withRequestMiddleware is a middleware that installs a request into the contex t. | 116 // withRequestMiddleware is a middleware that installs a request into the contex t. |
| 78 // This is used for various things in the default template. | 117 // This is used for various things in the default template. |
| 79 func withRequestMiddleware(c *router.Context, next router.Handler) { | 118 func withRequestMiddleware(c *router.Context, next router.Handler) { |
| 80 c.Context = WithRequest(c.Context, c.Request) | 119 c.Context = WithRequest(c.Context, c.Request) |
| 81 next(c) | 120 next(c) |
| 82 } | 121 } |
| 83 | 122 |
| 84 func getRequest(c context.Context) *http.Request { | 123 func getRequest(c context.Context) *http.Request { |
| 85 if req, ok := c.Value(&requestKey).(*http.Request); ok { | 124 if req, ok := c.Value(&requestKey).(*http.Request); ok { |
| 86 return req | 125 return req |
| 87 } | 126 } |
| 88 panic("No http.request found in context") | 127 panic("No http.request found in context") |
| 89 } | 128 } |
| OLD | NEW |