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

Side by Side Diff: appengine/cmd/cron/ui/invocation.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 5 package ui
6 6
7 import ( 7 import (
8 "fmt" 8 "fmt"
9 "net/http" 9 "net/http"
10 "strconv" 10 "strconv"
11 11
12 "github.com/julienschmidt/httprouter" 12 "github.com/julienschmidt/httprouter"
13 "golang.org/x/net/context" 13 "golang.org/x/net/context"
14 14
15 "github.com/luci/luci-go/common/clock" 15 "github.com/luci/luci-go/common/clock"
16 "github.com/luci/luci-go/server/auth" 16 "github.com/luci/luci-go/server/auth"
17 "github.com/luci/luci-go/server/router"
17 "github.com/luci/luci-go/server/templates" 18 "github.com/luci/luci-go/server/templates"
18 ) 19 )
19 20
20 func invocationPage(c context.Context, w http.ResponseWriter, r *http.Request, p httprouter.Params) { 21 func invocationPage(c *router.Context) {
21 » projectID := p.ByName("ProjectID") 22 » projectID := c.Params.ByName("ProjectID")
22 » jobID := p.ByName("JobID") 23 » jobID := c.Params.ByName("JobID")
23 » invID, err := strconv.ParseInt(p.ByName("InvID"), 10, 64) 24 » invID, err := strconv.ParseInt(c.Params.ByName("InvID"), 10, 64)
24 if err != nil { 25 if err != nil {
25 » » http.Error(w, "No such invocation", http.StatusNotFound) 26 » » http.Error(c.Writer, "No such invocation", http.StatusNotFound)
27 » » c.Abort()
iannucci 2016/06/13 19:23:28 probably-unpopular-idea: instead of adding a new m
26 return 28 return
27 } 29 }
28 30
29 » inv, err := config(c).Engine.GetInvocation(c, projectID+"/"+jobID, invID ) 31 » inv, err := config(c.Context).Engine.GetInvocation(c.Context, projectID+ "/"+jobID, invID)
30 if err != nil { 32 if err != nil {
31 panic(err) 33 panic(err)
32 } 34 }
33 if inv == nil { 35 if inv == nil {
34 » » http.Error(w, "No such invocation", http.StatusNotFound) 36 » » http.Error(c.Writer, "No such invocation", http.StatusNotFound)
37 » » c.Abort()
35 return 38 return
36 } 39 }
37 40
38 » now := clock.Now(c).UTC() 41 » now := clock.Now(c.Context).UTC()
39 » templates.MustRender(c, w, "pages/invocation.html", map[string]interface {}{ 42 » templates.MustRender(c.Context, c.Writer, "pages/invocation.html", map[s tring]interface{}{
40 "ProjectID": projectID, 43 "ProjectID": projectID,
41 "JobID": jobID, 44 "JobID": jobID,
42 "Inv": makeInvocation(projectID, jobID, inv, now), 45 "Inv": makeInvocation(projectID, jobID, inv, now),
43 }) 46 })
44 } 47 }
45 48
46 //////////////////////////////////////////////////////////////////////////////// 49 ////////////////////////////////////////////////////////////////////////////////
47 // Actions. 50 // Actions.
48 51
49 func abortInvocationAction(c context.Context, w http.ResponseWriter, r *http.Req uest, p httprouter.Params) { 52 func abortInvocationAction(c *router.Context) {
50 » handleInvAction(c, w, r, p, func(jobID string, invID int64) error { 53 » handleInvAction(c.Context, c.Writer, c.Request, c.Params, func(jobID str ing, invID int64) error {
51 » » who := auth.CurrentIdentity(c) 54 » » who := auth.CurrentIdentity(c.Context)
52 » » return config(c).Engine.AbortInvocation(c, jobID, invID, who) 55 » » return config(c.Context).Engine.AbortInvocation(c.Context, jobID , invID, who)
53 }) 56 })
54 } 57 }
55 58
56 func handleInvAction(c context.Context, w http.ResponseWriter, r *http.Request, p httprouter.Params, cb func(string, int64) error) { 59 func handleInvAction(c context.Context, w http.ResponseWriter, r *http.Request, p httprouter.Params, cb func(string, int64) error) {
57 projectID := p.ByName("ProjectID") 60 projectID := p.ByName("ProjectID")
58 jobID := p.ByName("JobID") 61 jobID := p.ByName("JobID")
59 invID := p.ByName("InvID") 62 invID := p.ByName("InvID")
60 if !isJobOwner(c, projectID, jobID) { 63 if !isJobOwner(c, projectID, jobID) {
61 http.Error(w, "Forbidden", 403) 64 http.Error(w, "Forbidden", 403)
62 return 65 return
63 } 66 }
64 invIDAsInt, err := strconv.ParseInt(invID, 10, 64) 67 invIDAsInt, err := strconv.ParseInt(invID, 10, 64)
65 if err != nil { 68 if err != nil {
66 http.Error(w, "Bad invocation ID", 400) 69 http.Error(w, "Bad invocation ID", 400)
67 return 70 return
68 } 71 }
69 if err := cb(projectID+"/"+jobID, invIDAsInt); err != nil { 72 if err := cb(projectID+"/"+jobID, invIDAsInt); err != nil {
70 panic(err) 73 panic(err)
71 } 74 }
72 http.Redirect(w, r, fmt.Sprintf("/jobs/%s/%s/%s", projectID, jobID, invI D), http.StatusFound) 75 http.Redirect(w, r, fmt.Sprintf("/jobs/%s/%s/%s", projectID, jobID, invI D), http.StatusFound)
73 } 76 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698