@@ -4,37 +4,68 @@ import (
4
4
"errors"
5
5
"fmt"
6
6
"html/template"
7
+ "io"
7
8
"net/http"
8
9
10
+ "github.com/GeertJohan/go.rice"
9
11
"github.com/gorilla/mux"
10
12
)
11
13
14
+ var (
15
+ cssBox * rice.Box
16
+ templateBox * rice.Box
17
+ )
18
+
19
+ type pageData struct {
20
+ Name string
21
+ Config * Configuration
22
+ Users []* MetaUser
23
+ Objects []* MetaObject
24
+ }
25
+
12
26
func (a * App ) addMgmt (r * mux.Router ) {
13
27
r .HandleFunc ("/mgmt" , basicAuth (a .indexHandler )).Methods ("GET" )
28
+ r .HandleFunc ("/mgmt/objects" , basicAuth (a .objectsHandler )).Methods ("GET" )
29
+ r .HandleFunc ("/mgmt/users" , basicAuth (a .usersHandler )).Methods ("GET" )
14
30
r .HandleFunc ("/mgmt/add" , basicAuth (a .addUserHandler )).Methods ("POST" )
15
31
r .HandleFunc ("/mgmt/del" , basicAuth (a .delUserHandler )).Methods ("POST" )
32
+
33
+ cssBox = rice .MustFindBox ("mgmt/css" )
34
+ templateBox = rice .MustFindBox ("mgmt/templates" )
35
+ r .HandleFunc ("/mgmt/css/{file}" , basicAuth (cssHandler ))
36
+ }
37
+
38
+ func cssHandler (w http.ResponseWriter , r * http.Request ) {
39
+ file := mux .Vars (r )["file" ]
40
+ f , err := cssBox .Open (file )
41
+ if err != nil {
42
+ writeStatus (w , r , 404 )
43
+ return
44
+ }
45
+
46
+ w .Header ().Set ("Content-Type" , "text/css" )
47
+
48
+ io .Copy (w , f )
49
+ f .Close ()
16
50
}
17
51
18
52
func basicAuth (h http.HandlerFunc ) http.HandlerFunc {
19
53
return func (w http.ResponseWriter , r * http.Request ) {
20
54
if Config .AdminUser == "" || Config .AdminPass == "" {
21
- http .Error (w , "Not Found" , 404 )
22
- logRequest (r , 404 )
55
+ writeStatus (w , r , 404 )
23
56
return
24
57
}
25
58
26
59
user , pass , ok := r .BasicAuth ()
27
60
if ! ok {
28
61
w .Header ().Set ("WWW-Authenticate" , "Basic realm=mgmt" )
29
- http .Error (w , "authorization failed" , 401 )
30
- logRequest (r , 401 )
62
+ writeStatus (w , r , 401 )
31
63
return
32
64
}
33
65
34
66
if user != Config .AdminUser || pass != Config .AdminPass {
35
67
w .Header ().Set ("WWW-Authenticate" , "Basic realm=mgmt" )
36
- http .Error (w , "authorization failed" , 401 )
37
- logRequest (r , 401 )
68
+ writeStatus (w , r , 401 )
38
69
return
39
70
}
40
71
@@ -44,22 +75,33 @@ func basicAuth(h http.HandlerFunc) http.HandlerFunc {
44
75
}
45
76
46
77
func (a * App ) indexHandler (w http.ResponseWriter , r * http.Request ) {
47
- t := template .Must (template .New ("main" ).Parse (bodyTemplate ))
48
- t .New ("body" ).Parse (indexTemplate )
78
+ if err := render (w , "config.tmpl" , pageData {Name : "index" , Config : Config }); err != nil {
79
+ writeStatus (w , r , 404 )
80
+ }
81
+ }
49
82
50
- type lfs struct {
51
- Users []* MetaUser
83
+ func (a * App ) objectsHandler (w http.ResponseWriter , r * http.Request ) {
84
+ objects , err := a .metaStore .Objects ()
85
+ if err != nil {
86
+ fmt .Fprintf (w , "Error retrieving objects: %s" , err )
87
+ return
52
88
}
53
89
90
+ if err := render (w , "objects.tmpl" , pageData {Name : "objects" , Objects : objects }); err != nil {
91
+ writeStatus (w , r , 404 )
92
+ }
93
+ }
94
+
95
+ func (a * App ) usersHandler (w http.ResponseWriter , r * http.Request ) {
54
96
users , err := a .metaStore .Users ()
55
97
if err != nil {
56
98
fmt .Fprintf (w , "Error retrieving users: %s" , err )
57
99
return
58
100
}
59
101
60
- l := & lfs { Users : users }
61
-
62
- t . Execute ( w , l )
102
+ if err := render ( w , "users.tmpl" , pageData { Name : "users" , Users : users }); err != nil {
103
+ writeStatus ( w , r , 404 )
104
+ }
63
105
}
64
106
65
107
func (a * App ) addUserHandler (w http.ResponseWriter , r * http.Request ) {
@@ -75,7 +117,7 @@ func (a *App) addUserHandler(w http.ResponseWriter, r *http.Request) {
75
117
return
76
118
}
77
119
78
- http .Redirect (w , r , "/mgmt" , 302 )
120
+ http .Redirect (w , r , "/mgmt/users " , 302 )
79
121
}
80
122
81
123
func (a * App ) delUserHandler (w http.ResponseWriter , r * http.Request ) {
@@ -90,7 +132,24 @@ func (a *App) delUserHandler(w http.ResponseWriter, r *http.Request) {
90
132
return
91
133
}
92
134
93
- http .Redirect (w , r , "/mgmt" , 302 )
135
+ http .Redirect (w , r , "/mgmt/users" , 302 )
136
+ }
137
+
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 )
94
153
}
95
154
96
155
func authenticate (r * http.Request ) error {
@@ -110,27 +169,3 @@ func authenticate(r *http.Request) error {
110
169
}
111
170
return err
112
171
}
113
-
114
- var bodyTemplate = `<html>
115
- <head>
116
- <title>Git LFS Server Management</title>
117
- </head>
118
- <body>
119
- {{template "body" .}}
120
- </body>
121
- </html>
122
- `
123
-
124
- var indexTemplate = `
125
- <h2>Users</h2>
126
- {{range .Users}}
127
- <div>{{.Name}} <form method="POST" action="/mgmt/del"><input type="hidden" name="name" value="{{.Name}}"/><input type="submit" value="Delete"/></form></div>
128
- {{end}}
129
-
130
- <form method="POST" action="/mgmt/add">
131
- <label id="name">Name:</label>
132
- <input type="text" name="name" />
133
- <input type="password" name="password" />
134
- <input type="submit" value="Add User" />
135
- </form>
136
- `
0 commit comments