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