| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 The LUCI Authors. | |
| 2 // | |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
| 4 // you may not use this file except in compliance with the License. | |
| 5 // You may obtain a copy of the License at | |
| 6 // | |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 | |
| 8 // | |
| 9 // Unless required by applicable law or agreed to in writing, software | |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, | |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 12 // See the License for the specific language governing permissions and | |
| 13 // limitations under the License. | |
| 14 | |
| 15 package common | |
| 16 | |
| 17 import ( | |
| 18 "net/http" | |
| 19 "strings" | |
| 20 | |
| 21 "golang.org/x/net/context" | |
| 22 | |
| 23 "github.com/luci/gae/impl/cloud" | |
| 24 "github.com/luci/gae/service/info" | |
| 25 "github.com/luci/luci-go/appengine/gaeauth/server" | |
| 26 "github.com/luci/luci-go/appengine/gaemiddleware" | |
| 27 "github.com/luci/luci-go/common/clock" | |
| 28 "github.com/luci/luci-go/server/analytics" | |
| 29 "github.com/luci/luci-go/server/auth" | |
| 30 "github.com/luci/luci-go/server/auth/identity" | |
| 31 "github.com/luci/luci-go/server/router" | |
| 32 "github.com/luci/luci-go/server/templates" | |
| 33 ) | |
| 34 | |
| 35 var authconfig *auth.Config | |
| 36 | |
| 37 // GetTemplateBundles is used to render HTML templates. It provides base args | |
| 38 // passed to all templates. It takes a path to the template folder, relative | |
| 39 // to the path of the binary during runtime. | |
| 40 func GetTemplateBundle(templatePath string) *templates.Bundle { | |
| 41 return &templates.Bundle{ | |
| 42 Loader: templates.FileSystemLoader(templatePath), | |
| 43 DebugMode: info.IsDevAppServer, | |
| 44 DefaultTemplate: "base", | |
| 45 DefaultArgs: func(c context.Context) (templates.Args, error) { | |
| 46 r := getRequest(c) | |
| 47 path := r.URL.Path | |
| 48 loginURL, err := auth.LoginURL(c, path) | |
| 49 if err != nil { | |
| 50 return nil, err | |
| 51 } | |
| 52 logoutURL, err := auth.LogoutURL(c, path) | |
| 53 if err != nil { | |
| 54 return nil, err | |
| 55 } | |
| 56 return templates.Args{ | |
| 57 "AppVersion": strings.Split(info.VersionID(c),
".")[0], | |
| 58 "IsAnonymous": auth.CurrentIdentity(c) == identi
ty.AnonymousIdentity, | |
| 59 "User": auth.CurrentUser(c), | |
| 60 "LoginURL": loginURL, | |
| 61 "LogoutURL": logoutURL, | |
| 62 "CurrentTime": clock.Now(c), | |
| 63 "Analytics": analytics.Snippet(c), | |
| 64 "RequestID": info.RequestID(c), | |
| 65 "Request": r, | |
| 66 }, nil | |
| 67 }, | |
| 68 FuncMap: funcMap, | |
| 69 } | |
| 70 } | |
| 71 | |
| 72 // Base returns the basic LUCI appengine middlewares. | |
| 73 func Base(templatePath string) router.MiddlewareChain { | |
| 74 return gaemiddleware.BaseProd().Extend( | |
| 75 auth.Authenticate(server.CookieAuth), | |
| 76 withRequestMiddleware, | |
| 77 templates.WithTemplates(GetTemplateBundle(templatePath)), | |
| 78 ) | |
| 79 } | |
| 80 | |
| 81 // FlexBase returns the basic middleware for use on appengine flex. Flex does n
ot | |
| 82 // allow the use of appengine APIs. | |
| 83 func FlexBase() router.MiddlewareChain { | |
| 84 // Installs the Info and Datastore services. | |
| 85 return router.NewMiddlewareChain(func(c *router.Context, next router.Han
dler) { | |
| 86 c.Context = cloud.UseFlex(c.Context) | |
| 87 next(c) | |
| 88 }) | |
| 89 } | |
| 90 | |
| 91 // The context key, so that we can embed the http.Request object into | |
| 92 // the context. | |
| 93 var requestKey = "http.request" | |
| 94 | |
| 95 // WithRequest returns a context with the http.Request object | |
| 96 // in it. | |
| 97 func WithRequest(c context.Context, r *http.Request) context.Context { | |
| 98 return context.WithValue(c, &requestKey, r) | |
| 99 } | |
| 100 | |
| 101 // withRequestMiddleware is a middleware that installs a request into the contex
t. | |
| 102 // This is used for various things in the default template. | |
| 103 func withRequestMiddleware(c *router.Context, next router.Handler) { | |
| 104 c.Context = WithRequest(c.Context, c.Request) | |
| 105 next(c) | |
| 106 } | |
| 107 | |
| 108 func getRequest(c context.Context) *http.Request { | |
| 109 if req, ok := c.Value(&requestKey).(*http.Request); ok { | |
| 110 return req | |
| 111 } | |
| 112 panic("No http.request found in context") | |
| 113 } | |
| OLD | NEW |