Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(873)

Side by Side Diff: appengine/cmd/cron/ui/common.go

Issue 2043423004: Make HTTP middleware easier to use (Closed) Base URL: https://github.com/luci/luci-go@master
Patch Set: gaemiddleware: add middleware func for WithProd Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « appengine/cmd/cron/frontend/handler.go ('k') | appengine/cmd/cron/ui/index.go » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The LUCI Authors. All rights reserved. 1 // Copyright 2015 The LUCI Authors. All rights reserved.
2 // Use of this source code is governed under the Apache License, Version 2.0 2 // Use of this source code is governed under the Apache License, Version 2.0
3 // that can be found in the LICENSE file. 3 // that can be found in the LICENSE file.
4 4
5 // Package ui implements request handlers that serve user facing HTML pages. 5 // Package ui implements request handlers that serve user facing HTML pages.
6 package ui 6 package ui
7 7
8 import ( 8 import (
9 "strings" 9 "strings"
10 10
11 "github.com/julienschmidt/httprouter"
12 "golang.org/x/net/context" 11 "golang.org/x/net/context"
13 "google.golang.org/appengine" 12 "google.golang.org/appengine"
14 13
15 "github.com/luci/gae/service/info" 14 "github.com/luci/gae/service/info"
16 15
17 "github.com/luci/luci-go/server/auth" 16 "github.com/luci/luci-go/server/auth"
18 "github.com/luci/luci-go/server/auth/xsrf" 17 "github.com/luci/luci-go/server/auth/xsrf"
19 » "github.com/luci/luci-go/server/middleware" 18 » "github.com/luci/luci-go/server/router"
20 "github.com/luci/luci-go/server/templates" 19 "github.com/luci/luci-go/server/templates"
21 20
22 "github.com/luci/luci-go/appengine/cmd/cron/engine" 21 "github.com/luci/luci-go/appengine/cmd/cron/engine"
23 ) 22 )
24 23
25 // Config is global configuration of UI handlers. 24 // Config is global configuration of UI handlers.
26 type Config struct { 25 type Config struct {
27 Engine engine.Engine 26 Engine engine.Engine
28 TemplatesPath string // path to templates directory deployed to GAE 27 TemplatesPath string // path to templates directory deployed to GAE
29 } 28 }
30 29
31 // InstallHandlers adds HTTP handlers that render HTML pages. 30 // InstallHandlers adds HTTP handlers that render HTML pages.
32 func InstallHandlers(r *httprouter.Router, base middleware.Base, cfg Config) { 31 func InstallHandlers(r *router.Router, base router.MiddlewareChain, cfg Config) {
33 tmpl := prepareTemplates(cfg.TemplatesPath) 32 tmpl := prepareTemplates(cfg.TemplatesPath)
34 33
35 » wrap := func(h middleware.Handler) httprouter.Handle { 34 » m := append(base, func(c *router.Context, next router.Handler) {
36 » » h = auth.Authenticate(h) 35 » » c.Context = context.WithValue(c.Context, configContextKey(0), &c fg)
37 » » h = templates.WithTemplates(h, tmpl) 36 » » next(c)
38 » » h = middleware.WithContextValue(h, configContextKey(0), &cfg) 37 » }, templates.WithTemplates(tmpl), auth.Authenticate)
39 » » return base(h)
40 » }
41 38
42 » r.GET("/", wrap(indexPage)) 39 » r.GET("/", m, indexPage)
43 » r.GET("/jobs/:ProjectID", wrap(projectPage)) 40 » r.GET("/jobs/:ProjectID", m, projectPage)
44 » r.GET("/jobs/:ProjectID/:JobID", wrap(jobPage)) 41 » r.GET("/jobs/:ProjectID/:JobID", m, jobPage)
45 » r.GET("/jobs/:ProjectID/:JobID/:InvID", wrap(invocationPage)) 42 » r.GET("/jobs/:ProjectID/:JobID/:InvID", m, invocationPage)
46 43
47 // All POST forms must be protected with XSRF token. 44 // All POST forms must be protected with XSRF token.
48 » r.POST("/actions/runJob/:ProjectID/:JobID", wrap(xsrf.WithTokenCheck(run JobAction))) 45 » mxsrf := append(m, xsrf.WithTokenCheck)
49 » r.POST("/actions/pauseJob/:ProjectID/:JobID", wrap(xsrf.WithTokenCheck(p auseJobAction))) 46 » r.POST("/actions/runJob/:ProjectID/:JobID", mxsrf, runJobAction)
50 » r.POST("/actions/resumeJob/:ProjectID/:JobID", wrap(xsrf.WithTokenCheck( resumeJobAction))) 47 » r.POST("/actions/pauseJob/:ProjectID/:JobID", mxsrf, pauseJobAction)
51 » r.POST("/actions/abortInvocation/:ProjectID/:JobID/:InvID", wrap(xsrf.Wi thTokenCheck(abortInvocationAction))) 48 » r.POST("/actions/resumeJob/:ProjectID/:JobID", mxsrf, resumeJobAction)
49 » r.POST("/actions/abortInvocation/:ProjectID/:JobID/:InvID", mxsrf, abort InvocationAction)
52 } 50 }
53 51
54 type configContextKey int 52 type configContextKey int
55 53
56 // config returns Config passed to InstallHandlers. 54 // config returns Config passed to InstallHandlers.
57 func config(c context.Context) *Config { 55 func config(c context.Context) *Config {
58 cfg, _ := c.Value(configContextKey(0)).(*Config) 56 cfg, _ := c.Value(configContextKey(0)).(*Config)
59 if cfg == nil { 57 if cfg == nil {
60 panic("impossible, configContextKey is not set") 58 panic("impossible, configContextKey is not set")
61 } 59 }
(...skipping 25 matching lines...) Expand all
87 "AppVersion": strings.Split(info.Get(c).Version ID(), ".")[0], 85 "AppVersion": strings.Split(info.Get(c).Version ID(), ".")[0],
88 "IsAnonymous": auth.CurrentIdentity(c) == "anony mous:anonymous", 86 "IsAnonymous": auth.CurrentIdentity(c) == "anony mous:anonymous",
89 "User": auth.CurrentUser(c), 87 "User": auth.CurrentUser(c),
90 "LoginURL": loginURL, 88 "LoginURL": loginURL,
91 "LogoutURL": logoutURL, 89 "LogoutURL": logoutURL,
92 "XsrfToken": token, 90 "XsrfToken": token,
93 }, nil 91 }, nil
94 }, 92 },
95 } 93 }
96 } 94 }
OLDNEW
« no previous file with comments | « appengine/cmd/cron/frontend/handler.go ('k') | appengine/cmd/cron/ui/index.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698