-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
372 lines (328 loc) · 10.6 KB
/
main.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
package main
import (
"fmt"
"net/http"
"os"
"path"
"strconv"
"strings"
"time"
"github.com/gin-gonic/gin"
"github.com/jcelliott/lumber"
"gopkg.in/urfave/cli.v1"
)
// Testing
// curl -H "Content-Type: application/json" -X POST -d '{"bucket":"food","keystore":{"username":"xyz","password":"xyz"}}' 'http://zack:123@localhost:8080/'
var version string
var port, dbpath string
var compressOn, verbose bool
var log *lumber.ConsoleLogger
func main() {
// flag.BoolVar(&verbose, "verbose", false, "verbosity")
// flag.BoolVar(&compressOn, "compress", false, "use compression")
// flag.StringVar(&dbpath, "db", path.Join(".", "dbs"), "path to the database")
// flag.StringVar(&port, "port", "8080", "port to listen on")
// flag.Parse()
app := cli.NewApp()
app.Name = "boltdb-server"
app.Usage = "fancy server for connecting to a BoltDB keystore"
app.Version = version
app.Compiled = time.Now()
app.Action = func(c *cli.Context) error {
dbpath = c.GlobalString("db")
compressOn = c.GlobalBool("compress")
verbose = c.GlobalBool("debug")
port = c.GlobalString("port")
os.MkdirAll(dbpath, 0755)
if verbose {
log = lumber.NewConsoleLogger(lumber.TRACE)
} else {
log = lumber.NewConsoleLogger(lumber.WARN)
}
startTime := time.Now()
gin.SetMode(gin.ReleaseMode)
r := gin.Default()
r.GET("/v1/api", func(c *gin.Context) {
c.String(200, `
// Get map of buckets and the number of keys in each
GET /v1/db/<db>/stats
// Get list of all buckets
GET /v1/db/<db>/buckets
// Get all keys and values from a bucket
GET /v1/db/<db>/bucket/<bucket>/numkeys
// Get all keys and values from a bucket
GET /v1/db/<db>/bucket/<bucket>/all
// Get all keys and values specified by ?keys=key1,key2 or by JSON
GET /v1/db/<db>/bucket/<bucket>/some
// Delete and return first n keys
GET /v1/db/<db>/bucket/<bucket>/pop?n=X
// Get all keys in a bucket
GET /v1/db/<db>/bucket/<bucket>/keys", handleGetKeys)
// Return boolean of whether it has key
GET /v1/db/<db>/bucket/<bucket>/haskey/<key>
// Return boolean of whether any buckets contain any keys specified by JSON
GET /v1/db/<db>/haskeys
// Delete database file
DELETE /v1/db/<db>
// Delete bucket
DELETE /v1/db/<db>/bucket/<bucket>
// Delete keys, where keys are specified by JSON []string
DELETE /v1/db/<db>/bucket/<bucket>/keys
// Updates a database with keystore specified by JSON
POST /v1/db/<db>/bucket/<bucket>/update
// Move keys, with buckets and keys specified by JSON
POST /v1/db/<db>/move
// Create buckets specified by JSON
POST /v1/db/<db>/create
`)
})
r.GET("/v1/uptime", func(c *gin.Context) {
c.JSON(200, gin.H{
"uptime": time.Since(startTime).String(),
})
})
r.GET("/v1/db/:dbname/stats", handleGetDBStats) // Get map of buckets and the number of keys in each
r.GET("/v1/db/:dbname/buckets", handleGetBuckets) // Get list of all buckets
r.GET("/v1/db/:dbname/bucket/:bucket/numkeys", handleGetNumKeys) // Get all keys and values from a bucket (no parameters)
r.GET("/v1/db/:dbname/bucket/:bucket/all", handleGet) // Get all keys and values from a bucket (no parameters)
r.GET("/v1/db/:dbname/bucket/:bucket/some", handleGet) // Get all keys and values specified by ?keys=key1,key2 or by JSON
r.GET("/v1/db/:dbname/bucket/:bucket/pop", handlePop) // Delete and return first n keys + values, where n specified by ?n=100
r.GET("/v1/db/:dbname/bucket/:bucket/keys", handleGetKeys) // Get all keys in a bucket (no parameters)
r.GET("/v1/db/:dbname/bucket/:bucket/haskey/:key", handleHasKey) // Return boolean of whether it has key
r.GET("/v1/db/:dbname/haskeys", handleHasKeys) // Return boolean of whether any of the buckets contain the keys
// TODO: r.GET("/v1/db/:dbname/bucket/:bucket/data", getDataArchive) // Creates archive with keys as filenames and values as contents, returns archive
r.DELETE("/v1/db/:dbname", handleDeleteDatabase) // Delete database file (no parameters)
r.DELETE("/v1/db/:dbname/bucket/:bucket", handleDeleteBucket) // Delete bucket (no parameters)
r.DELETE("/v1/db/:dbname/bucket/:bucket/keys", handleDeleteKeys) // Delete keys, where keys are specified by JSON []string
//
r.POST("/v1/db/:dbname/bucket/:bucket/update", handleUpdate) // Updates a database with keystore specified by JSON
r.POST("/v1/db/:dbname/move", handleMove) // Move keys, with buckets and keys specified by JSON
r.POST("/v1/db/:dbname/create", handleCreateDB) // Move keys, with buckets and keys specified by JSON
fmt.Printf("boltdb-server (v.%s) running on http://%s:%s\n", version, GetLocalIP(), port)
r.Run(":" + port) // listen and serve on 0.0.0.0:8080
return nil
}
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "port, p",
Value: "8050",
Usage: "port to use to listen",
},
cli.StringFlag{
Name: "db",
Value: path.Join(".", "dbs"),
Usage: "path to use for storing databases",
},
cli.BoolFlag{
Name: "compress, c",
Usage: "turn on compression",
},
cli.BoolFlag{
Name: "debug",
Usage: "turn on debug mode",
},
}
app.Run(os.Args)
}
func handleHasKeys(c *gin.Context) {
dbname := c.Param("dbname")
type QueryJSON struct {
Keys []string `json:"keys"`
Buckets []string `json:"buckets"`
}
var json QueryJSON
if c.BindJSON(&json) != nil {
c.String(http.StatusBadRequest, "Problem binding keys")
return
}
doesHaveKeyMap, err := hasKeys(dbname, json.Buckets, json.Keys)
if err != nil {
c.String(http.StatusInternalServerError, err.Error())
return
}
c.JSON(http.StatusOK, doesHaveKeyMap)
}
func handleCreateDB(c *gin.Context) {
dbname := c.Param("dbname")
var json []string
if c.BindJSON(&json) != nil {
c.String(http.StatusBadRequest, "Problem binding keys")
return
}
err := createDatabase(dbname, json)
if err != nil {
c.String(http.StatusInternalServerError, err.Error())
return
}
c.JSON(http.StatusOK, "Created db")
}
func handleHasKey(c *gin.Context) {
dbname := c.Param("dbname")
bucket := c.Param("bucket")
key := c.Param("key")
doesHaveKey, err := hasKey(dbname, bucket, key)
if err != nil {
c.String(http.StatusInternalServerError, err.Error())
return
}
c.JSON(http.StatusOK, doesHaveKey)
}
func handleGetDBStats(c *gin.Context) {
dbname := c.Param("dbname")
bucketNames, err := getBucketNames(dbname)
if err != nil {
c.String(http.StatusInternalServerError, err.Error())
return
}
stats := make(map[string]int)
for _, bucket := range bucketNames {
stats[bucket], err = getNumberKeysInBucket(dbname, bucket)
if err != nil {
c.String(http.StatusInternalServerError, err.Error())
return
}
}
c.JSON(http.StatusOK, stats)
}
func handleGetNumKeys(c *gin.Context) {
dbname := c.Param("dbname")
bucket := c.Param("bucket")
n, err := getNumberKeysInBucket(dbname, bucket)
if err != nil {
c.String(http.StatusInternalServerError, err.Error())
return
}
c.JSON(http.StatusOK, n)
}
func handleGetBuckets(c *gin.Context) {
dbname := c.Param("dbname")
bucketNames, err := getBucketNames(dbname)
if err != nil {
c.String(http.StatusInternalServerError, err.Error())
return
}
c.JSON(http.StatusOK, bucketNames)
}
func handleDeleteDatabase(c *gin.Context) {
dbname := c.Param("dbname")
err := deleteDatabase(dbname)
if err != nil {
c.String(http.StatusInternalServerError, err.Error())
return
}
c.String(http.StatusOK, "Deleted database")
}
func handleDeleteBucket(c *gin.Context) {
dbname := c.Param("dbname")
bucket := c.Param("bucket")
err := deleteBucket(dbname, bucket)
if err != nil {
c.String(http.StatusInternalServerError, err.Error())
return
}
c.String(http.StatusOK, "Deleted bucket")
}
func handleDeleteKeys(c *gin.Context) {
dbname := c.Param("dbname")
bucket := c.Param("bucket")
var keys []string
if c.BindJSON(&keys) != nil {
c.String(http.StatusBadRequest, "Problem binding keys")
return
}
err := deleteKeys(dbname, bucket, keys)
if err != nil {
c.String(http.StatusInternalServerError, err.Error())
return
}
c.String(http.StatusOK, "Deleted keys")
}
func handleUpdate(c *gin.Context) {
dbname := c.Param("dbname")
bucket := c.Param("bucket")
var json map[string]string
if c.BindJSON(&json) != nil {
c.String(http.StatusBadRequest, "Problem binding keystore")
return
}
err := updateDatabase(dbname, bucket, json)
if err != nil {
c.String(http.StatusInternalServerError, err.Error())
return
}
c.String(http.StatusOK, fmt.Sprintf("Inserted %d things into %s", len(json), bucket))
}
func handleGetKeys(c *gin.Context) {
dbname := c.Param("dbname")
bucket := c.Param("bucket")
keystore, err := getKeysFromDatabase(dbname, bucket)
if err != nil {
c.String(http.StatusInternalServerError, err.Error())
return
}
c.JSON(http.StatusOK, keystore)
}
func handlePop(c *gin.Context) {
dbname := c.Param("dbname")
bucket := c.Param("bucket")
nQuery := c.DefaultQuery("n", "0")
num, err := strconv.Atoi(nQuery)
if err != nil {
c.String(http.StatusInternalServerError, err.Error())
return
}
if num <= 0 {
c.String(http.StatusBadRequest, "Must specify n > 0")
return
}
keystore, err := pop(dbname, bucket, num)
if err != nil {
c.String(http.StatusInternalServerError, err.Error())
return
}
c.JSON(http.StatusOK, keystore)
}
func handleGet(c *gin.Context) {
dbname := c.Param("dbname")
bucket := c.Param("bucket")
keysQuery := c.DefaultQuery("keys", "")
json := []string{}
if c.BindJSON(&json) != nil && keysQuery != "" {
json = strings.Split(keysQuery, ",")
}
// If requested some without providing keys, throw error
if len(json) == 0 && strings.Contains(c.Request.RequestURI, "some") {
c.String(http.StatusBadRequest, "Must provide keys")
return
}
// Get keys and values
keystore, err := getFromDatabase(dbname, bucket, json)
if err != nil {
c.String(http.StatusInternalServerError, err.Error())
return
}
c.JSON(http.StatusOK, keystore)
return
}
func handleMove(c *gin.Context) {
dbname := c.Param("dbname")
type QueryJSON struct {
FromBucket string `json:"from_bucket"`
ToBucket string `json:"to_bucket"`
Keys []string `json:"keys"`
}
var json QueryJSON
if c.BindJSON(&json) != nil {
c.String(http.StatusBadRequest, "Must provide keys, from_bucket and to_bucket")
return
}
// Get keys and values
err := moveBuckets(dbname, json.FromBucket, json.ToBucket, json.Keys)
if err != nil {
log.Error("Could not move %v from %s to %s", json.Keys, json.FromBucket, json.ToBucket)
c.String(http.StatusInternalServerError, err.Error())
return
}
log.Trace("Moved %v from %s to %s", json.Keys, json.FromBucket, json.ToBucket)
c.JSON(http.StatusOK, fmt.Sprintf("Moved keys"))
}