| 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" | 18 "github.com/luci/luci-go/common/cloudlogging" |
| 19 "github.com/luci/luci-go/common/logging/cloudlog" | 19 "github.com/luci/luci-go/common/logging/cloudlog" |
| 20 "github.com/luci/luci-go/server/analytics" | 20 "github.com/luci/luci-go/server/analytics" |
| 21 "github.com/luci/luci-go/server/auth" | 21 "github.com/luci/luci-go/server/auth" |
| 22 "github.com/luci/luci-go/server/auth/identity" | 22 "github.com/luci/luci-go/server/auth/identity" |
| 23 "github.com/luci/luci-go/server/router" | 23 "github.com/luci/luci-go/server/router" |
| 24 "github.com/luci/luci-go/server/templates" | 24 "github.com/luci/luci-go/server/templates" |
| 25 ) | 25 ) |
| 26 | 26 |
| 27 var authconfig *auth.Config | 27 var authconfig *auth.Config |
| 28 | 28 |
| 29 // GetTemplateBundles is used to render HTML templates. It provides base args | 29 // GetTemplateBundles is used to render HTML templates. It provides base args |
| 30 // passed to all templates. | 30 // passed to all templates. It takes a path to the template folder, relative |
| 31 func GetTemplateBundle() *templates.Bundle { | 31 // to the path of the binary during runtime. |
| 32 func GetTemplateBundle(templatePath string) *templates.Bundle { |
| 32 return &templates.Bundle{ | 33 return &templates.Bundle{ |
| 33 » » Loader: templates.FileSystemLoader("../frontend/templat
es"), | 34 » » Loader: templates.FileSystemLoader(templatePath), |
| 34 DebugMode: info.IsDevAppServer, | 35 DebugMode: info.IsDevAppServer, |
| 35 DefaultTemplate: "base", | 36 DefaultTemplate: "base", |
| 36 DefaultArgs: func(c context.Context) (templates.Args, error) { | 37 DefaultArgs: func(c context.Context) (templates.Args, error) { |
| 37 r := getRequest(c) | 38 r := getRequest(c) |
| 38 path := r.URL.Path | 39 path := r.URL.Path |
| 39 loginURL, err := auth.LoginURL(c, path) | 40 loginURL, err := auth.LoginURL(c, path) |
| 40 if err != nil { | 41 if err != nil { |
| 41 return nil, err | 42 return nil, err |
| 42 } | 43 } |
| 43 logoutURL, err := auth.LogoutURL(c, path) | 44 logoutURL, err := auth.LogoutURL(c, path) |
| (...skipping 10 matching lines...) Expand all Loading... |
| 54 "Analytics": analytics.Snippet(c), | 55 "Analytics": analytics.Snippet(c), |
| 55 "RequestID": info.RequestID(c), | 56 "RequestID": info.RequestID(c), |
| 56 "Request": r, | 57 "Request": r, |
| 57 }, nil | 58 }, nil |
| 58 }, | 59 }, |
| 59 FuncMap: funcMap, | 60 FuncMap: funcMap, |
| 60 } | 61 } |
| 61 } | 62 } |
| 62 | 63 |
| 63 // Base returns the basic LUCI appengine middlewares. | 64 // Base returns the basic LUCI appengine middlewares. |
| 64 func Base() router.MiddlewareChain { | 65 func Base(templatePath string) router.MiddlewareChain { |
| 65 return gaemiddleware.BaseProd().Extend( | 66 return gaemiddleware.BaseProd().Extend( |
| 66 auth.Authenticate(server.CookieAuth), | 67 auth.Authenticate(server.CookieAuth), |
| 67 withRequestMiddleware, | 68 withRequestMiddleware, |
| 68 » » templates.WithTemplates(GetTemplateBundle()), | 69 » » templates.WithTemplates(GetTemplateBundle(templatePath)), |
| 69 ) | 70 ) |
| 70 } | 71 } |
| 71 | 72 |
| 72 // FlexBase returns the basic middleware for use on appengine flex. Flex does n
ot | 73 // FlexBase returns the basic middleware for use on appengine flex. Flex does n
ot |
| 73 // allow the use of appengine APIs. | 74 // allow the use of appengine APIs. |
| 74 func FlexBase() router.MiddlewareChain { | 75 func FlexBase() router.MiddlewareChain { |
| 75 // Use the cloud logger. | 76 // Use the cloud logger. |
| 76 logger := func(c *router.Context, next router.Handler) { | 77 logger := func(c *router.Context, next router.Handler) { |
| 77 project := info.AppID(c.Context) | 78 project := info.AppID(c.Context) |
| 78 logClient, err := cloudlogging.NewClient( | 79 logClient, err := cloudlogging.NewClient( |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 114 c.Context = WithRequest(c.Context, c.Request) | 115 c.Context = WithRequest(c.Context, c.Request) |
| 115 next(c) | 116 next(c) |
| 116 } | 117 } |
| 117 | 118 |
| 118 func getRequest(c context.Context) *http.Request { | 119 func getRequest(c context.Context) *http.Request { |
| 119 if req, ok := c.Value(&requestKey).(*http.Request); ok { | 120 if req, ok := c.Value(&requestKey).(*http.Request); ok { |
| 120 return req | 121 return req |
| 121 } | 122 } |
| 122 panic("No http.request found in context") | 123 panic("No http.request found in context") |
| 123 } | 124 } |
| OLD | NEW |