| 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/service/info" |
| 14 "github.com/luci/luci-go/appengine/gaeauth/server" |
| 15 "github.com/luci/luci-go/appengine/gaemiddleware" |
| 16 "github.com/luci/luci-go/common/clock" |
| 17 "github.com/luci/luci-go/server/analytics" |
| 18 "github.com/luci/luci-go/server/auth" |
| 19 "github.com/luci/luci-go/server/auth/identity" |
| 20 "github.com/luci/luci-go/server/router" |
| 21 "github.com/luci/luci-go/server/templates" |
| 22 ) |
| 23 |
| 24 // GetTemplateBundles is used to render HTML templates. It provides base args |
| 25 // passed to all templates. |
| 26 func GetTemplateBundle() *templates.Bundle { |
| 27 return &templates.Bundle{ |
| 28 Loader: templates.FileSystemLoader("templates"), |
| 29 DebugMode: info.IsDevAppServer, |
| 30 DefaultTemplate: "base", |
| 31 DefaultArgs: func(c context.Context) (templates.Args, error) { |
| 32 r := getRequest(c) |
| 33 path := r.URL.Path |
| 34 loginURL, err := auth.LoginURL(c, path) |
| 35 if err != nil { |
| 36 return nil, err |
| 37 } |
| 38 logoutURL, err := auth.LogoutURL(c, path) |
| 39 if err != nil { |
| 40 return nil, err |
| 41 } |
| 42 return templates.Args{ |
| 43 "AppVersion": strings.Split(info.VersionID(c),
".")[0], |
| 44 "IsAnonymous": auth.CurrentIdentity(c) == identi
ty.AnonymousIdentity, |
| 45 "User": auth.CurrentUser(c), |
| 46 "LoginURL": loginURL, |
| 47 "LogoutURL": logoutURL, |
| 48 "CurrentTime": clock.Now(c), |
| 49 "Analytics": analytics.Snippet(c), |
| 50 "RequestID": info.RequestID(c), |
| 51 }, nil |
| 52 }, |
| 53 FuncMap: funcMap, |
| 54 } |
| 55 } |
| 56 |
| 57 // Base returns the basic LUCI appengine middlewares. |
| 58 func Base() router.MiddlewareChain { |
| 59 methods := auth.Authenticator{ |
| 60 &server.OAuth2Method{Scopes: []string{server.EmailScope}}, |
| 61 server.CookieAuth, |
| 62 &server.InboundAppIDAuthMethod{}, |
| 63 } |
| 64 m := gaemiddleware.BaseProd().Extend(auth.Use(methods), auth.Authenticat
e) |
| 65 m = m.Extend(withRequestMiddleware) |
| 66 m = m.Extend(templates.WithTemplates(GetTemplateBundle())) |
| 67 return m |
| 68 } |
| 69 |
| 70 // The context key, so that we can embed the http.Request object into |
| 71 // the context. |
| 72 var requestKey = "http.request" |
| 73 |
| 74 // WithRequest returns a context with the http.Request object |
| 75 // in it. |
| 76 func WithRequest(c context.Context, r *http.Request) context.Context { |
| 77 return context.WithValue(c, &requestKey, r) |
| 78 } |
| 79 |
| 80 // withRequestMiddleware is a middleware that installs a request into the contex
t. |
| 81 // This is used for various things in the default template. |
| 82 func withRequestMiddleware(c *router.Context, next router.Handler) { |
| 83 c.Context = WithRequest(c.Context, c.Request) |
| 84 next(c) |
| 85 } |
| 86 |
| 87 func getRequest(c context.Context) *http.Request { |
| 88 if req, ok := c.Value(&requestKey).(*http.Request); ok { |
| 89 return req |
| 90 } |
| 91 panic("No http.request found in context") |
| 92 } |
| OLD | NEW |