-
Notifications
You must be signed in to change notification settings - Fork 3
/
api_keys.go
79 lines (64 loc) · 1.42 KB
/
api_keys.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
package leveldb_admin
import (
"github.com/siddontang/go/hack"
"github.com/syndtr/goleveldb/leveldb"
"github.com/syndtr/goleveldb/leveldb/util"
"net/http"
"strconv"
)
type keyListRes struct {
Items []string
SearchText string
IsPart bool
}
func (l *LevelAdmin) apiKeys(writer http.ResponseWriter, request *http.Request) {
db := request.URL.Query().Get("db")
if db == "" {
http.NotFound(writer, request)
return
}
prefix := request.URL.Query().Get("prefix")
searchText := request.URL.Query().Get("searchText")
limitStr := request.URL.Query().Get("limit")
limit := 15
if limitStr != "" {
limitRe, err := strconv.Atoi(limitStr)
if err != nil {
l.writeError(writer, err)
}
limit = limitRe
}
if limit > 15 {
limit = 15
}
if limit < 0 {
limit = 15
}
res := &keyListRes{IsPart: false}
if load, ok := l.dbs.Load(db); ok {
db := load.(*leveldb.DB)
iter := db.NewIterator(util.BytesPrefix([]byte(prefix)), nil)
defer iter.Release()
if searchText != "" {
iter.Seek(hack.Slice(searchText))
}
for iter.Next() {
if len(res.Items) >= limit {
res.SearchText = string(iter.Key())
res.IsPart = true
l.writeJson(writer, res)
return
}
res.Items = append(res.Items, string(iter.Key()))
}
err := iter.Error()
if err != nil {
l.writeError(writer, err)
return
}
res.IsPart = false
l.writeJson(writer, res)
} else {
http.NotFound(writer, request)
}
}