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

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: Update tests 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
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, handlers []router.Handler, cfg Config) {
33 tmpl := prepareTemplates(cfg.TemplatesPath) 32 tmpl := prepareTemplates(cfg.TemplatesPath)
34 33
35 » wrap := func(h middleware.Handler) httprouter.Handle { 34 » wrap := func() []router.Handler {
36 » » h = auth.Authenticate(h) 35 » » return append(
37 » » h = templates.WithTemplates(h, tmpl) 36 » » » handlers,
38 » » h = middleware.WithContextValue(h, configContextKey(0), &cfg) 37 » » » router.HandlerWithContextValue(configContextKey(0), &cfg ),
39 » » return base(h) 38 » » » templates.WithTemplates(tmpl),
39 » » » auth.Authenticate(),
40 » » )
40 } 41 }
41 42
42 » r.GET("/", wrap(indexPage)) 43 » r.GET("/", append(wrap(), indexPage)...)
43 » r.GET("/jobs/:ProjectID", wrap(projectPage)) 44 » r.GET("/jobs/:ProjectID", append(wrap(), projectPage)...)
44 » r.GET("/jobs/:ProjectID/:JobID", wrap(jobPage)) 45 » r.GET("/jobs/:ProjectID/:JobID", append(wrap(), jobPage)...)
45 » r.GET("/jobs/:ProjectID/:JobID/:InvID", wrap(invocationPage)) 46 » r.GET("/jobs/:ProjectID/:JobID/:InvID", append(wrap(), invocationPage).. .)
iannucci 2016/06/13 19:23:28 same here?
46 47
47 // All POST forms must be protected with XSRF token. 48 // All POST forms must be protected with XSRF token.
48 » r.POST("/actions/runJob/:ProjectID/:JobID", wrap(xsrf.WithTokenCheck(run JobAction))) 49 » r.POST("/actions/runJob/:ProjectID/:JobID", append(wrap(), xsrf.WithToke nCheck(), runJobAction)...)
49 » r.POST("/actions/pauseJob/:ProjectID/:JobID", wrap(xsrf.WithTokenCheck(p auseJobAction))) 50 » r.POST("/actions/pauseJob/:ProjectID/:JobID", append(wrap(), xsrf.WithTo kenCheck(), pauseJobAction)...)
50 » r.POST("/actions/resumeJob/:ProjectID/:JobID", wrap(xsrf.WithTokenCheck( resumeJobAction))) 51 » r.POST("/actions/resumeJob/:ProjectID/:JobID", append(wrap(), xsrf.WithT okenCheck(), resumeJobAction)...)
51 » r.POST("/actions/abortInvocation/:ProjectID/:JobID/:InvID", wrap(xsrf.Wi thTokenCheck(abortInvocationAction))) 52 » r.POST("/actions/abortInvocation/:ProjectID/:JobID/:InvID", append(wrap( ), xsrf.WithTokenCheck(), abortInvocationAction)...)
52 } 53 }
53 54
54 type configContextKey int 55 type configContextKey int
55 56
56 // config returns Config passed to InstallHandlers. 57 // config returns Config passed to InstallHandlers.
57 func config(c context.Context) *Config { 58 func config(c context.Context) *Config {
58 cfg, _ := c.Value(configContextKey(0)).(*Config) 59 cfg, _ := c.Value(configContextKey(0)).(*Config)
59 if cfg == nil { 60 if cfg == nil {
60 panic("impossible, configContextKey is not set") 61 panic("impossible, configContextKey is not set")
61 } 62 }
(...skipping 25 matching lines...) Expand all
87 "AppVersion": strings.Split(info.Get(c).Version ID(), ".")[0], 88 "AppVersion": strings.Split(info.Get(c).Version ID(), ".")[0],
88 "IsAnonymous": auth.CurrentIdentity(c) == "anony mous:anonymous", 89 "IsAnonymous": auth.CurrentIdentity(c) == "anony mous:anonymous",
89 "User": auth.CurrentUser(c), 90 "User": auth.CurrentUser(c),
90 "LoginURL": loginURL, 91 "LoginURL": loginURL,
91 "LogoutURL": logoutURL, 92 "LogoutURL": logoutURL,
92 "XsrfToken": token, 93 "XsrfToken": token,
93 }, nil 94 }, nil
94 }, 95 },
95 } 96 }
96 } 97 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698