Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 The LUCI Authors. All rights reserved. | |
|
hinoka
2017/03/15 18:51:16
This was settings.go, but renamed and trimmed down
nodir
2017/03/16 17:42:51
why is this generic file in settings package? i'd
hinoka
2017/03/16 21:04:06
ack, splitting settings into config and common
hinoka
2017/03/17 20:00:21
Actually theres an import loop this way, just rena
| |
| 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 settings | |
| 6 | |
| 7 import ( | |
| 8 "fmt" | |
| 9 "net/http" | |
| 10 "strings" | |
| 11 | |
| 12 "github.com/julienschmidt/httprouter" | |
| 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/milo/common/miloerror" | |
| 18 "github.com/luci/luci-go/server/analytics" | |
| 19 "github.com/luci/luci-go/server/auth" | |
| 20 "github.com/luci/luci-go/server/router" | |
| 21 "github.com/luci/luci-go/server/templates" | |
| 22 "golang.org/x/net/context" | |
|
nodir
2017/03/16 17:42:51
put 3p imports before 1p, in a separate section
al
hinoka
2017/03/17 20:00:21
Done.
| |
| 23 ) | |
| 24 | |
| 25 type Handler func(c context.Context, r *http.Request, p httprouter.Params) (*tem plates.Args, error) | |
| 26 | |
| 27 // getTemplateBundles is used to render HTML templates. It provides a base args | |
| 28 // passed to all templates. | |
| 29 func GetTemplateBundle() *templates.Bundle { | |
| 30 return &templates.Bundle{ | |
| 31 Loader: templates.FileSystemLoader("templates"), | |
| 32 DebugMode: info.IsDevAppServer, | |
| 33 DefaultTemplate: "base", | |
| 34 DefaultArgs: func(c context.Context) (templates.Args, error) { | |
| 35 r := getRequest(c) | |
| 36 path := r.URL.Path | |
| 37 loginURL, err := auth.LoginURL(c, path) | |
| 38 if err != nil { | |
| 39 return nil, err | |
| 40 } | |
| 41 logoutURL, err := auth.LogoutURL(c, path) | |
| 42 if err != nil { | |
| 43 return nil, err | |
| 44 } | |
| 45 if err != nil { | |
| 46 return nil, err | |
| 47 } | |
| 48 return templates.Args{ | |
| 49 "AppVersion": strings.Split(info.VersionID(c), ".")[0], | |
| 50 "IsAnonymous": auth.CurrentIdentity(c) == "anony mous:anonymous", | |
| 51 "User": auth.CurrentUser(c), | |
| 52 "LoginURL": loginURL, | |
| 53 "LogoutURL": logoutURL, | |
| 54 "CurrentTime": clock.Now(c), | |
| 55 "Analytics": analytics.Snippet(c), | |
| 56 "RequestID": info.RequestID(c), | |
| 57 }, nil | |
| 58 }, | |
| 59 FuncMap: funcMap, | |
| 60 } | |
| 61 } | |
| 62 | |
| 63 // Base returns the basic luci appengine middlewares. | |
| 64 func Base() router.MiddlewareChain { | |
| 65 methods := auth.Authenticator{ | |
| 66 &server.OAuth2Method{Scopes: []string{server.EmailScope}}, | |
| 67 server.CookieAuth, | |
| 68 &server.InboundAppIDAuthMethod{}, | |
| 69 } | |
| 70 m := gaemiddleware.BaseProd().Extend(auth.Use(methods), auth.Authenticat e) | |
| 71 m = m.Extend(templates.WithTemplates(GetTemplateBundle())) | |
|
nodir
2017/03/16 17:42:51
i think it is more natural to inject http request
hinoka
2017/03/16 21:04:06
You can't though... you don't get the request obje
hinoka
2017/03/17 20:00:21
Turns out you can, changed.
| |
| 72 return m | |
| 73 } | |
| 74 | |
| 75 // Wrap wraps a Milo Handler and returns router.Handler that writes into | |
| 76 // c.Writer with a template named templateName. | |
| 77 func Wrap(h Handler, templateName string) router.Handler { | |
| 78 return func(c *router.Context) { | |
| 79 // Inject the request into the context. | |
| 80 c.Context = WithRequest(c.Context, c.Request) | |
| 81 | |
| 82 // Do the things. | |
| 83 args, err := h(c.Context, c.Request, c.Params) | |
|
nodir
2017/03/16 17:42:51
why not pass c itself?
hinoka
2017/03/16 21:04:06
It's a different type of context. I think estaab'
hinoka
2017/03/17 20:00:21
Done.
| |
| 84 | |
| 85 // Throw errors | |
| 86 if err != nil { | |
| 87 msg := err.Error() | |
| 88 code := http.StatusInternalServerError | |
| 89 if merr, ok := err.(miloerror.Error); ok { | |
|
nodir
2017/03/16 17:42:51
without templates, I don't see why do we need milo
hinoka
2017/03/16 21:04:06
To do default error templates? Otherwise this erro
hinoka
2017/03/17 20:00:21
Changed error handling code to replicate everywher
| |
| 90 msg = merr.Message | |
| 91 code = merr.Code | |
| 92 } | |
| 93 c.Writer.WriteHeader(code) | |
| 94 name := "pages/error.html" | |
| 95 templates.MustRender(c.Context, c.Writer, name, template s.Args{ | |
| 96 "Code": code, | |
| 97 "Message": msg, | |
| 98 }) | |
| 99 return | |
| 100 } | |
| 101 | |
| 102 // Render the stuff. | |
| 103 name := fmt.Sprintf("pages/%s", templateName) | |
| 104 templates.MustRender(c.Context, c.Writer, name, *args) | |
| 105 } | |
| 106 } | |
| 107 | |
| 108 // The context key, so that we can embed the http.Request object into | |
| 109 // the context. | |
| 110 var requestKey = "http.request" | |
| 111 | |
| 112 func WithRequest(c context.Context, r *http.Request) context.Context { | |
| 113 return context.WithValue(c, &requestKey, r) | |
| 114 } | |
| 115 | |
| 116 func getRequest(c context.Context) *http.Request { | |
| 117 if req, ok := c.Value(&requestKey).(*http.Request); ok { | |
| 118 return req | |
| 119 } | |
| 120 panic("No http.request found in context") | |
| 121 } | |
| OLD | NEW |