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