@@ -4,37 +4,68 @@ import (
44 "errors"
55 "fmt"
66 "html/template"
7+ "io"
78 "net/http"
89
10+ "github.com/GeertJohan/go.rice"
911 "github.com/gorilla/mux"
1012)
1113
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+
1226func (a * App ) addMgmt (r * mux.Router ) {
1327 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" )
1430 r .HandleFunc ("/mgmt/add" , basicAuth (a .addUserHandler )).Methods ("POST" )
1531 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 ()
1650}
1751
1852func basicAuth (h http.HandlerFunc ) http.HandlerFunc {
1953 return func (w http.ResponseWriter , r * http.Request ) {
2054 if Config .AdminUser == "" || Config .AdminPass == "" {
21- http .Error (w , "Not Found" , 404 )
22- logRequest (r , 404 )
55+ writeStatus (w , r , 404 )
2356 return
2457 }
2558
2659 user , pass , ok := r .BasicAuth ()
2760 if ! ok {
2861 w .Header ().Set ("WWW-Authenticate" , "Basic realm=mgmt" )
29- http .Error (w , "authorization failed" , 401 )
30- logRequest (r , 401 )
62+ writeStatus (w , r , 401 )
3163 return
3264 }
3365
3466 if user != Config .AdminUser || pass != Config .AdminPass {
3567 w .Header ().Set ("WWW-Authenticate" , "Basic realm=mgmt" )
36- http .Error (w , "authorization failed" , 401 )
37- logRequest (r , 401 )
68+ writeStatus (w , r , 401 )
3869 return
3970 }
4071
@@ -44,22 +75,33 @@ func basicAuth(h http.HandlerFunc) http.HandlerFunc {
4475}
4576
4677func (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+ }
4982
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
5288 }
5389
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 ) {
5496 users , err := a .metaStore .Users ()
5597 if err != nil {
5698 fmt .Fprintf (w , "Error retrieving users: %s" , err )
5799 return
58100 }
59101
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+ }
63105}
64106
65107func (a * App ) addUserHandler (w http.ResponseWriter , r * http.Request ) {
@@ -75,7 +117,7 @@ func (a *App) addUserHandler(w http.ResponseWriter, r *http.Request) {
75117 return
76118 }
77119
78- http .Redirect (w , r , "/mgmt" , 302 )
120+ http .Redirect (w , r , "/mgmt/users " , 302 )
79121}
80122
81123func (a * App ) delUserHandler (w http.ResponseWriter , r * http.Request ) {
@@ -90,7 +132,24 @@ func (a *App) delUserHandler(w http.ResponseWriter, r *http.Request) {
90132 return
91133 }
92134
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 )
94153}
95154
96155func authenticate (r * http.Request ) error {
@@ -110,27 +169,3 @@ func authenticate(r *http.Request) error {
110169 }
111170 return err
112171}
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