-
Notifications
You must be signed in to change notification settings - Fork 3
/
api_keys_count.go
62 lines (51 loc) · 1.15 KB
/
api_keys_count.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
package leveldb_admin
import (
"github.com/siddontang/go/hack"
"github.com/syndtr/goleveldb/leveldb"
"github.com/syndtr/goleveldb/leveldb/util"
"net/http"
"time"
)
type apiKeysCountRes struct {
Count uint64
LastKey string
IsTrue bool
}
func (l *LevelAdmin) apiKeysCount(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")
res := &apiKeysCountRes{IsTrue: true, LastKey: searchText}
timeOut := time.After(time.Second * 1)
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() {
select {
case <-timeOut:
res.IsTrue = false
goto end
default:
res.LastKey = string(iter.Key())
res.Count++
}
}
end:
err := iter.Error()
if err != nil {
l.writeError(writer, err)
return
}
l.writeJson(writer, res)
} else {
http.NotFound(writer, request)
}
}