forked from git-lfs/lfs-test-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mgmt.go
171 lines (139 loc) · 3.75 KB
/
mgmt.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package main
import (
"errors"
"fmt"
"html/template"
"io"
"net/http"
"github.com/GeertJohan/go.rice"
"github.com/gorilla/mux"
)
var (
cssBox *rice.Box
templateBox *rice.Box
)
type pageData struct {
Name string
Config *Configuration
Users []*MetaUser
Objects []*MetaObject
}
func (a *App) addMgmt(r *mux.Router) {
r.HandleFunc("/mgmt", basicAuth(a.indexHandler)).Methods("GET")
r.HandleFunc("/mgmt/objects", basicAuth(a.objectsHandler)).Methods("GET")
r.HandleFunc("/mgmt/users", basicAuth(a.usersHandler)).Methods("GET")
r.HandleFunc("/mgmt/add", basicAuth(a.addUserHandler)).Methods("POST")
r.HandleFunc("/mgmt/del", basicAuth(a.delUserHandler)).Methods("POST")
cssBox = rice.MustFindBox("mgmt/css")
templateBox = rice.MustFindBox("mgmt/templates")
r.HandleFunc("/mgmt/css/{file}", basicAuth(cssHandler))
}
func cssHandler(w http.ResponseWriter, r *http.Request) {
file := mux.Vars(r)["file"]
f, err := cssBox.Open(file)
if err != nil {
writeStatus(w, r, 404)
return
}
w.Header().Set("Content-Type", "text/css")
io.Copy(w, f)
f.Close()
}
func basicAuth(h http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if Config.AdminUser == "" || Config.AdminPass == "" {
writeStatus(w, r, 404)
return
}
user, pass, ok := r.BasicAuth()
if !ok {
w.Header().Set("WWW-Authenticate", "Basic realm=mgmt")
writeStatus(w, r, 401)
return
}
if user != Config.AdminUser || pass != Config.AdminPass {
w.Header().Set("WWW-Authenticate", "Basic realm=mgmt")
writeStatus(w, r, 401)
return
}
h(w, r)
logRequest(r, 200)
}
}
func (a *App) indexHandler(w http.ResponseWriter, r *http.Request) {
if err := render(w, "config.tmpl", pageData{Name: "index", Config: Config}); err != nil {
writeStatus(w, r, 404)
}
}
func (a *App) objectsHandler(w http.ResponseWriter, r *http.Request) {
objects, err := a.metaStore.Objects()
if err != nil {
fmt.Fprintf(w, "Error retrieving objects: %s", err)
return
}
if err := render(w, "objects.tmpl", pageData{Name: "objects", Objects: objects}); err != nil {
writeStatus(w, r, 404)
}
}
func (a *App) usersHandler(w http.ResponseWriter, r *http.Request) {
users, err := a.metaStore.Users()
if err != nil {
fmt.Fprintf(w, "Error retrieving users: %s", err)
return
}
if err := render(w, "users.tmpl", pageData{Name: "users", Users: users}); err != nil {
writeStatus(w, r, 404)
}
}
func (a *App) addUserHandler(w http.ResponseWriter, r *http.Request) {
user := r.FormValue("name")
pass := r.FormValue("password")
if user == "" || pass == "" {
fmt.Fprintf(w, "Invalid username or password")
return
}
if err := a.metaStore.AddUser(user, pass); err != nil {
fmt.Fprintf(w, "Error adding user: %s", err)
return
}
http.Redirect(w, r, "/mgmt/users", 302)
}
func (a *App) delUserHandler(w http.ResponseWriter, r *http.Request) {
user := r.FormValue("name")
if user == "" {
fmt.Fprintf(w, "Invalid username")
return
}
if err := a.metaStore.DeleteUser(user); err != nil {
fmt.Fprintf(w, "Error deleting user: %s", err)
return
}
http.Redirect(w, r, "/mgmt/users", 302)
}
func render(w http.ResponseWriter, tmpl string, data pageData) error {
bodyString, err := templateBox.String("body.tmpl")
if err != nil {
return err
}
contentString, err := templateBox.String(tmpl)
if err != nil {
return err
}
t := template.Must(template.New("main").Parse(bodyString))
t.New("content").Parse(contentString)
return t.Execute(w, data)
}
func authenticate(r *http.Request) error {
err := errors.New("Forbidden")
if Config.AdminUser == "" || Config.AdminPass == "" {
return err
}
user, pass, ok := r.BasicAuth()
if !ok {
return err
}
if user == Config.AdminUser && pass == Config.AdminPass {
return nil
}
return err
}