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