@@ -32,15 +32,14 @@ func (a *App) addMgmt(r *mux.Router) {
3232
3333 cssBox = rice .MustFindBox ("mgmt/css" )
3434 templateBox = rice .MustFindBox ("mgmt/templates" )
35- r .HandleFunc ("/mgmt/css/{file}" , cssHandler )
35+ r .HandleFunc ("/mgmt/css/{file}" , basicAuth ( cssHandler ) )
3636}
3737
3838func cssHandler (w http.ResponseWriter , r * http.Request ) {
3939 file := mux .Vars (r )["file" ]
4040 f , err := cssBox .Open (file )
4141 if err != nil {
42- http .Error (w , "Not Found" , 404 )
43- logRequest (r , 404 )
42+ writeStatus (w , r , 404 )
4443 return
4544 }
4645
@@ -53,23 +52,20 @@ func cssHandler(w http.ResponseWriter, r *http.Request) {
5352func basicAuth (h http.HandlerFunc ) http.HandlerFunc {
5453 return func (w http.ResponseWriter , r * http.Request ) {
5554 if Config .AdminUser == "" || Config .AdminPass == "" {
56- http .Error (w , "Not Found" , 404 )
57- logRequest (r , 404 )
55+ writeStatus (w , r , 404 )
5856 return
5957 }
6058
6159 user , pass , ok := r .BasicAuth ()
6260 if ! ok {
6361 w .Header ().Set ("WWW-Authenticate" , "Basic realm=mgmt" )
64- http .Error (w , "authorization failed" , 401 )
65- logRequest (r , 401 )
62+ writeStatus (w , r , 401 )
6663 return
6764 }
6865
6966 if user != Config .AdminUser || pass != Config .AdminPass {
7067 w .Header ().Set ("WWW-Authenticate" , "Basic realm=mgmt" )
71- http .Error (w , "authorization failed" , 401 )
72- logRequest (r , 401 )
68+ writeStatus (w , r , 401 )
7369 return
7470 }
7571
@@ -79,78 +75,33 @@ func basicAuth(h http.HandlerFunc) http.HandlerFunc {
7975}
8076
8177func (a * App ) indexHandler (w http.ResponseWriter , r * http.Request ) {
82- bodyString , err := templateBox .String ("body.tmpl" )
83- if err != nil {
84- http .Error (w , "Not Found" , 404 )
85- logRequest (r , 404 )
86- return
87- }
88-
89- configString , err := templateBox .String ("config.tmpl" )
90- if err != nil {
91- http .Error (w , "Not Found" , 404 )
92- logRequest (r , 404 )
93- return
78+ if err := render (w , "config.tmpl" , pageData {Name : "index" , Config : Config }); err != nil {
79+ writeStatus (w , r , 404 )
9480 }
95-
96- t := template .Must (template .New ("main" ).Parse (bodyString ))
97- t .New ("content" ).Parse (configString )
98-
99- t .Execute (w , pageData {Name : "index" , Config : Config })
10081}
10182
10283func (a * App ) objectsHandler (w http.ResponseWriter , r * http.Request ) {
103- bodyString , err := templateBox .String ("body.tmpl" )
104- if err != nil {
105- http .Error (w , "Not Found" , 404 )
106- logRequest (r , 404 )
107- return
108- }
109-
110- contentString , err := templateBox .String ("objects.tmpl" )
111- if err != nil {
112- http .Error (w , "Not Found" , 404 )
113- logRequest (r , 404 )
114- return
115- }
116-
117- t := template .Must (template .New ("main" ).Parse (bodyString ))
118- t .New ("content" ).Parse (contentString )
119-
12084 objects , err := a .metaStore .Objects ()
12185 if err != nil {
12286 fmt .Fprintf (w , "Error retrieving objects: %s" , err )
12387 return
12488 }
12589
126- t .Execute (w , pageData {Name : "objects" , Objects : objects })
90+ if err := render (w , "objects.tmpl" , pageData {Name : "objects" , Objects : objects }); err != nil {
91+ writeStatus (w , r , 404 )
92+ }
12793}
12894
12995func (a * App ) usersHandler (w http.ResponseWriter , r * http.Request ) {
130- bodyString , err := templateBox .String ("body.tmpl" )
131- if err != nil {
132- http .Error (w , "Not Found" , 404 )
133- logRequest (r , 404 )
134- return
135- }
136-
137- contentString , err := templateBox .String ("users.tmpl" )
138- if err != nil {
139- http .Error (w , "Not Found" , 404 )
140- logRequest (r , 404 )
141- return
142- }
143-
144- t := template .Must (template .New ("main" ).Parse (bodyString ))
145- t .New ("content" ).Parse (contentString )
146-
14796 users , err := a .metaStore .Users ()
14897 if err != nil {
14998 fmt .Fprintf (w , "Error retrieving users: %s" , err )
15099 return
151100 }
152101
153- t .Execute (w , pageData {Name : "users" , Users : users })
102+ if err := render (w , "users.tmpl" , pageData {Name : "users" , Users : users }); err != nil {
103+ writeStatus (w , r , 404 )
104+ }
154105}
155106
156107func (a * App ) addUserHandler (w http.ResponseWriter , r * http.Request ) {
@@ -184,6 +135,23 @@ func (a *App) delUserHandler(w http.ResponseWriter, r *http.Request) {
184135 http .Redirect (w , r , "/mgmt/users" , 302 )
185136}
186137
138+ func render (w http.ResponseWriter , tmpl string , data pageData ) error {
139+ bodyString , err := templateBox .String ("body.tmpl" )
140+ if err != nil {
141+ return err
142+ }
143+
144+ contentString , err := templateBox .String (tmpl )
145+ if err != nil {
146+ return err
147+ }
148+
149+ t := template .Must (template .New ("main" ).Parse (bodyString ))
150+ t .New ("content" ).Parse (contentString )
151+
152+ return t .Execute (w , data )
153+ }
154+
187155func authenticate (r * http.Request ) error {
188156 err := errors .New ("Forbidden" )
189157
@@ -201,17 +169,3 @@ func authenticate(r *http.Request) error {
201169 }
202170 return err
203171}
204-
205- var indexTemplate = `
206- <h2>Users</h2>
207- {{range .Users}}
208- <div>{{.Name}} <form method="POST" action="/mgmt/del"><input type="hidden" name="name" value="{{.Name}}"/><input type="submit" value="Delete"/></form></div>
209- {{end}}
210-
211- <form method="POST" action="/mgmt/add">
212- <label id="name">Name:</label>
213- <input type="text" name="name" />
214- <input type="password" name="password" />
215- <input type="submit" value="Add User" />
216- </form>
217- `
0 commit comments