| 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 "golang.org/x/net/context" | 11 "golang.org/x/net/context" |
| 12 | 12 |
| 13 "github.com/luci/gae/impl/cloud" | 13 "github.com/luci/gae/impl/cloud" |
| 14 "github.com/luci/gae/service/info" | 14 "github.com/luci/gae/service/info" |
| 15 "github.com/luci/luci-go/appengine/gaeauth/server" | 15 "github.com/luci/luci-go/appengine/gaeauth/server" |
| 16 "github.com/luci/luci-go/appengine/gaemiddleware" | 16 "github.com/luci/luci-go/appengine/gaemiddleware" |
| 17 "github.com/luci/luci-go/common/clock" | 17 "github.com/luci/luci-go/common/clock" |
| 18 "github.com/luci/luci-go/common/cloudlogging" | |
| 19 "github.com/luci/luci-go/common/logging/cloudlog" | |
| 20 "github.com/luci/luci-go/server/analytics" | 18 "github.com/luci/luci-go/server/analytics" |
| 21 "github.com/luci/luci-go/server/auth" | 19 "github.com/luci/luci-go/server/auth" |
| 22 "github.com/luci/luci-go/server/auth/identity" | 20 "github.com/luci/luci-go/server/auth/identity" |
| 23 "github.com/luci/luci-go/server/router" | 21 "github.com/luci/luci-go/server/router" |
| 24 "github.com/luci/luci-go/server/templates" | 22 "github.com/luci/luci-go/server/templates" |
| 25 ) | 23 ) |
| 26 | 24 |
| 27 var authconfig *auth.Config | 25 var authconfig *auth.Config |
| 28 | 26 |
| 29 // GetTemplateBundles is used to render HTML templates. It provides base args | 27 // GetTemplateBundles is used to render HTML templates. It provides base args |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 66 return gaemiddleware.BaseProd().Extend( | 64 return gaemiddleware.BaseProd().Extend( |
| 67 auth.Authenticate(server.CookieAuth), | 65 auth.Authenticate(server.CookieAuth), |
| 68 withRequestMiddleware, | 66 withRequestMiddleware, |
| 69 templates.WithTemplates(GetTemplateBundle(templatePath)), | 67 templates.WithTemplates(GetTemplateBundle(templatePath)), |
| 70 ) | 68 ) |
| 71 } | 69 } |
| 72 | 70 |
| 73 // FlexBase returns the basic middleware for use on appengine flex. Flex does n
ot | 71 // FlexBase returns the basic middleware for use on appengine flex. Flex does n
ot |
| 74 // allow the use of appengine APIs. | 72 // allow the use of appengine APIs. |
| 75 func FlexBase() router.MiddlewareChain { | 73 func FlexBase() router.MiddlewareChain { |
| 76 // Use the cloud logger. | |
| 77 logger := func(c *router.Context, next router.Handler) { | |
| 78 project := info.AppID(c.Context) | |
| 79 logClient, err := cloudlogging.NewClient( | |
| 80 cloudlogging.ClientOptions{ | |
| 81 ProjectID: project, | |
| 82 LogID: "gae_app", | |
| 83 ResourceType: "gae_app", | |
| 84 }, | |
| 85 // TODO(hinoka): This may require authentication to actu
ally work. | |
| 86 http.DefaultClient) | |
| 87 if err != nil { | |
| 88 panic(err) | |
| 89 } | |
| 90 c.Context = cloudlog.Use(c.Context, cloudlog.Config{}, logClient
) | |
| 91 next(c) | |
| 92 } | |
| 93 // Installs the Info and Datastore services. | 74 // Installs the Info and Datastore services. |
| 94 » base := func(c *router.Context, next router.Handler) { | 75 » return router.NewMiddlewareChain(func(c *router.Context, next router.Han
dler) { |
| 95 c.Context = cloud.UseFlex(c.Context) | 76 c.Context = cloud.UseFlex(c.Context) |
| 96 next(c) | 77 next(c) |
| 97 » } | 78 » }) |
| 98 » // Now chain it all together! | |
| 99 » return router.NewMiddlewareChain(base, logger) | |
| 100 } | 79 } |
| 101 | 80 |
| 102 // The context key, so that we can embed the http.Request object into | 81 // The context key, so that we can embed the http.Request object into |
| 103 // the context. | 82 // the context. |
| 104 var requestKey = "http.request" | 83 var requestKey = "http.request" |
| 105 | 84 |
| 106 // WithRequest returns a context with the http.Request object | 85 // WithRequest returns a context with the http.Request object |
| 107 // in it. | 86 // in it. |
| 108 func WithRequest(c context.Context, r *http.Request) context.Context { | 87 func WithRequest(c context.Context, r *http.Request) context.Context { |
| 109 return context.WithValue(c, &requestKey, r) | 88 return context.WithValue(c, &requestKey, r) |
| 110 } | 89 } |
| 111 | 90 |
| 112 // withRequestMiddleware is a middleware that installs a request into the contex
t. | 91 // withRequestMiddleware is a middleware that installs a request into the contex
t. |
| 113 // This is used for various things in the default template. | 92 // This is used for various things in the default template. |
| 114 func withRequestMiddleware(c *router.Context, next router.Handler) { | 93 func withRequestMiddleware(c *router.Context, next router.Handler) { |
| 115 c.Context = WithRequest(c.Context, c.Request) | 94 c.Context = WithRequest(c.Context, c.Request) |
| 116 next(c) | 95 next(c) |
| 117 } | 96 } |
| 118 | 97 |
| 119 func getRequest(c context.Context) *http.Request { | 98 func getRequest(c context.Context) *http.Request { |
| 120 if req, ok := c.Value(&requestKey).(*http.Request); ok { | 99 if req, ok := c.Value(&requestKey).(*http.Request); ok { |
| 121 return req | 100 return req |
| 122 } | 101 } |
| 123 panic("No http.request found in context") | 102 panic("No http.request found in context") |
| 124 } | 103 } |
| OLD | NEW |