-
Notifications
You must be signed in to change notification settings - Fork 5
/
http.go
208 lines (189 loc) · 5.61 KB
/
http.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package main
import (
"fmt"
"log"
"net/http"
"os"
"path"
"time"
"github.com/GeertJohan/go.rice"
"github.com/nmaggioni/goat"
)
type (
bucketKeys map[string][]KeyValue
stats struct {
BucketsNumber int `json:"bucketsNumber"`
KeysNumber int `json:"keysNumber"`
DiskUsed int64 `json:"diskUsedBytes"`
Keys bucketKeys `json:"keys"`
}
)
func getAllBucketsAndKeys() (bucketKeys, error) {
buckets, err := ListBuckets()
if err != nil {
return nil, err
}
bk := make(bucketKeys)
for _, bucketName := range buckets {
keys, _ := ListBucketKeys(bucketName)
bk[bucketName] = keys
}
return bk, nil
}
func setPoweredByHeader(res http.ResponseWriter) {
res.Header().Set("X-Powered-By", "gerph")
}
func listAllBucketsKeys(res http.ResponseWriter, req *http.Request, params goat.Params) {
setPoweredByHeader(res)
bk, err := getAllBucketsAndKeys()
if err != nil {
http.Error(res, err.Error(), http.StatusInternalServerError)
return
}
goat.WriteJSON(res, bk)
}
func listBucketKeys(res http.ResponseWriter, req *http.Request, params goat.Params) {
setPoweredByHeader(res)
keys, err := ListBucketKeys(params["bucket"])
if err != nil {
if err.Error() == "no such bucket" {
res.WriteHeader(http.StatusNoContent)
return
}
http.Error(res, err.Error(), http.StatusInternalServerError)
return
}
goat.WriteJSON(res, keys)
}
func deleteBucket(res http.ResponseWriter, req *http.Request, params goat.Params) {
setPoweredByHeader(res)
if params["bucket"] != "" {
err := DeleteBucket(params["bucket"])
if err != nil {
http.Error(res, err.Error(), http.StatusInternalServerError)
return
}
res.WriteHeader(http.StatusOK)
} else {
res.WriteHeader(http.StatusBadRequest)
}
}
func getBucketKey(res http.ResponseWriter, req *http.Request, params goat.Params) {
setPoweredByHeader(res)
if params["bucket"] != "" && params["key"] != "" {
value, err := GetKey(params["bucket"], params["key"])
if err != nil {
if err.Error() == "no such bucket" {
res.WriteHeader(http.StatusNoContent)
return
}
http.Error(res, err.Error(), http.StatusInternalServerError)
return
}
if value != "" {
res.WriteHeader(http.StatusOK)
goat.WriteJSON(res, map[string]string{
"key": params["key"],
"value": value,
})
return
}
res.WriteHeader(http.StatusNoContent)
} else {
res.WriteHeader(http.StatusBadRequest)
}
}
func setBucketKey(res http.ResponseWriter, req *http.Request, params goat.Params) {
setPoweredByHeader(res)
err := req.ParseForm()
if err != nil {
http.Error(res, err.Error(), http.StatusInternalServerError)
return
}
if len(req.Form["value"]) > 0 {
value := req.Form["value"][0]
err = SetKey(params["bucket"], params["key"], value)
if err != nil {
http.Error(res, err.Error(), http.StatusInternalServerError)
return
}
res.WriteHeader(http.StatusOK)
goat.WriteJSON(res, map[string]string{
"key": params["key"],
"value": value,
})
} else {
res.WriteHeader(http.StatusBadRequest)
}
}
func deleteBucketKey(res http.ResponseWriter, req *http.Request, params goat.Params) {
setPoweredByHeader(res)
if params["bucket"] != "" && params["key"] != "" {
err := DeleteKey(params["bucket"], params["key"])
if err != nil {
http.Error(res, err.Error(), http.StatusInternalServerError)
return
}
res.WriteHeader(http.StatusOK)
} else {
res.WriteHeader(http.StatusBadRequest)
}
}
func serveWebInterface(res http.ResponseWriter, req *http.Request, params goat.Params) {
setPoweredByHeader(res)
if params["asset"] != "" {
box := rice.MustFindBox("public/assets")
file, err := box.HTTPBox().Open(params["asset"])
if err != nil {
fmt.Println(err)
}
http.ServeContent(res, req, params["asset"], time.Time{}, file)
} else {
box := rice.MustFindBox("public")
file, err := box.HTTPBox().Open("index.html")
if err != nil {
fmt.Println(err)
}
http.ServeContent(res, req, req.URL.Path[1:], time.Time{}, file)
}
}
func serveWebStats(res http.ResponseWriter, req *http.Request, params goat.Params) {
bucketsNumber, _ := CountBuckets()
keysNumber, _ := CountKeys()
dbFileStats, _ := os.Stat(DBPath)
dbSizeBytes := dbFileStats.Size()
keys, _ := getAllBucketsAndKeys()
webStats := stats{bucketsNumber, keysNumber, dbSizeBytes, keys}
setPoweredByHeader(res)
goat.WriteJSON(res, webStats)
}
func serveBackup(res http.ResponseWriter, req *http.Request, params goat.Params) {
setPoweredByHeader(res)
res.Header().Set("Content-Disposition", "attachment; filename=\""+path.Base(DBPath)+"\"")
res.Header().Set("Content-Type", "application/octet-stream")
http.ServeFile(res, req, DBPath)
}
// Listen configures and starts a web server and its router.
func Listen(port string) {
router := goat.New()
router.Get("/", "web_ui", serveWebInterface)
router.Get("/asset/:asset", "serve_asset", serveWebInterface)
router.Get("/stats", "web_ui_stats", serveWebStats)
router.Get("/backup", "download_backup", serveBackup)
api := router.Subrouter("/api")
api.Options("/", "help", func(res http.ResponseWriter, req *http.Request, _ goat.Params) {
setPoweredByHeader(res)
goat.WriteJSON(res, api.Index())
})
api.Get("/", "list_all_buckets_and_keys", listAllBucketsKeys)
api.Get("/:bucket", "list_bucket_keys", listBucketKeys)
api.Delete("/:bucket", "delete_bucket", deleteBucket)
api.Get("/:bucket/:key", "get_bucket_key", getBucketKey)
api.Put("/:bucket/:key", "set_bucket_key", setBucketKey)
api.Post("/:bucket/:key", "set_bucket_key", setBucketKey)
api.Delete("/:bucket/:key", "delete_bucket_key", deleteBucketKey)
err := router.Run(":" + port)
if err != nil {
log.Fatalf("Unable to start web server: %s", err.Error())
}
}