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

Side by Side Diff: server/settings/admin/settings.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 2016 The LUCI Authors. All rights reserved. 1 // Copyright 2016 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 admin 5 package admin
6 6
7 import ( 7 import (
8 "net/http" 8 "net/http"
9 9
10 "github.com/julienschmidt/httprouter" 10 "github.com/julienschmidt/httprouter"
11 "golang.org/x/net/context" 11 "golang.org/x/net/context"
12 12
13 "github.com/luci/luci-go/server/auth" 13 "github.com/luci/luci-go/server/auth"
14 "github.com/luci/luci-go/server/auth/xsrf" 14 "github.com/luci/luci-go/server/auth/xsrf"
15 "github.com/luci/luci-go/server/router"
15 "github.com/luci/luci-go/server/settings" 16 "github.com/luci/luci-go/server/settings"
16 "github.com/luci/luci-go/server/templates" 17 "github.com/luci/luci-go/server/templates"
17 ) 18 )
18 19
19 type fieldWithValue struct { 20 type fieldWithValue struct {
20 settings.UIField 21 settings.UIField
21 Value string 22 Value string
22 } 23 }
23 24
24 type validationError struct { 25 type validationError struct {
(...skipping 12 matching lines...) Expand all
37 templates.MustRender(c, rw, "pages/error.html", templates.Args{ 38 templates.MustRender(c, rw, "pages/error.html", templates.Args{
38 "Error": "No such settings", 39 "Error": "No such settings",
39 }) 40 })
40 return 41 return
41 } 42 }
42 if err := cb(id, page); err != nil { 43 if err := cb(id, page); err != nil {
43 replyError(c, rw, err) 44 replyError(c, rw, err)
44 } 45 }
45 } 46 }
46 47
47 func settingsPageGET(c context.Context, rw http.ResponseWriter, r *http.Request, p httprouter.Params) { 48 func settingsPageGET(c *router.Context) {
48 » withPage(c, rw, p, func(id string, page settings.UIPage) error { 49 » withPage(c.Context, c.Writer, c.Params, func(id string, page settings.UI Page) error {
49 » » title, err := page.Title(c) 50 » » title, err := page.Title(c.Context)
50 if err != nil { 51 if err != nil {
51 return err 52 return err
52 } 53 }
53 » » overview, err := page.Overview(c) 54 » » overview, err := page.Overview(c.Context)
54 if err != nil { 55 if err != nil {
55 return err 56 return err
56 } 57 }
57 » » fields, err := page.Fields(c) 58 » » fields, err := page.Fields(c.Context)
58 if err != nil { 59 if err != nil {
59 return err 60 return err
60 } 61 }
61 » » values, err := page.ReadSettings(c) 62 » » values, err := page.ReadSettings(c.Context)
62 if err != nil { 63 if err != nil {
63 return err 64 return err
64 } 65 }
65 66
66 withValues := make([]fieldWithValue, len(fields)) 67 withValues := make([]fieldWithValue, len(fields))
67 for i, f := range fields { 68 for i, f := range fields {
68 withValues[i] = fieldWithValue{ 69 withValues[i] = fieldWithValue{
69 UIField: f, 70 UIField: f,
70 Value: values[f.ID], 71 Value: values[f.ID],
71 } 72 }
72 } 73 }
73 74
74 » » templates.MustRender(c, rw, "pages/settings.html", templates.Arg s{ 75 » » templates.MustRender(c.Context, c.Writer, "pages/settings.html", templates.Args{
75 "ID": id, 76 "ID": id,
76 "Title": title, 77 "Title": title,
77 "Overview": overview, 78 "Overview": overview,
78 "Fields": withValues, 79 "Fields": withValues,
79 » » » "XsrfTokenField": xsrf.TokenField(c), 80 » » » "XsrfTokenField": xsrf.TokenField(c.Context),
80 }) 81 })
81 return nil 82 return nil
82 }) 83 })
83 } 84 }
84 85
85 func settingsPagePOST(c context.Context, rw http.ResponseWriter, r *http.Request , p httprouter.Params) { 86 func settingsPagePOST(c *router.Context) {
86 » withPage(c, rw, p, func(id string, page settings.UIPage) error { 87 » withPage(c.Context, c.Writer, c.Params, func(id string, page settings.UI Page) error {
87 » » title, err := page.Title(c) 88 » » title, err := page.Title(c.Context)
88 if err != nil { 89 if err != nil {
89 return err 90 return err
90 } 91 }
91 » » fields, err := page.Fields(c) 92 » » fields, err := page.Fields(c.Context)
92 if err != nil { 93 if err != nil {
93 return err 94 return err
94 } 95 }
95 96
96 // Extract values from the page and validate them. 97 // Extract values from the page and validate them.
97 values := make(map[string]string, len(fields)) 98 values := make(map[string]string, len(fields))
98 validationErrors := []validationError{} 99 validationErrors := []validationError{}
99 for _, f := range fields { 100 for _, f := range fields {
100 if !f.Type.IsEditable() { 101 if !f.Type.IsEditable() {
101 continue 102 continue
102 } 103 }
103 » » » val := r.PostFormValue(f.ID) 104 » » » val := c.Request.PostFormValue(f.ID)
104 values[f.ID] = val 105 values[f.ID] = val
105 if f.Validator != nil { 106 if f.Validator != nil {
106 if err := f.Validator(val); err != nil { 107 if err := f.Validator(val); err != nil {
107 validationErrors = append(validationErro rs, validationError{ 108 validationErrors = append(validationErro rs, validationError{
108 FieldTitle: f.Title, 109 FieldTitle: f.Title,
109 Value: val, 110 Value: val,
110 Error: err.Error(), 111 Error: err.Error(),
111 }) 112 })
112 } 113 }
113 } 114 }
114 } 115 }
115 if len(validationErrors) != 0 { 116 if len(validationErrors) != 0 {
116 » » » rw.WriteHeader(http.StatusBadRequest) 117 » » » c.Writer.WriteHeader(http.StatusBadRequest)
117 » » » templates.MustRender(c, rw, "pages/validation_error.html ", templates.Args{ 118 » » » templates.MustRender(c.Context, c.Writer, "pages/validat ion_error.html", templates.Args{
118 "ID": id, 119 "ID": id,
119 "Title": title, 120 "Title": title,
120 "Errors": validationErrors, 121 "Errors": validationErrors,
121 }) 122 })
122 return nil 123 return nil
123 } 124 }
124 125
125 // Store. 126 // Store.
126 » » err = page.WriteSettings(c, values, auth.CurrentUser(c).Email, " modified via web UI") 127 » » err = page.WriteSettings(c.Context, values, auth.CurrentUser(c.C ontext).Email, "modified via web UI")
127 if err != nil { 128 if err != nil {
128 return err 129 return err
129 } 130 }
130 » » templates.MustRender(c, rw, "pages/done.html", templates.Args{ 131 » » templates.MustRender(c.Context, c.Writer, "pages/done.html", tem plates.Args{
131 "ID": id, 132 "ID": id,
132 "Title": title, 133 "Title": title,
133 }) 134 })
134 return nil 135 return nil
135 }) 136 })
136 } 137 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698