-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.go
143 lines (123 loc) · 3.29 KB
/
db.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
package db
import (
"github.com/n0rdy/proteus/httpserver/models"
"github.com/n0rdy/proteus/httpserver/utils"
"github.com/n0rdy/proteus/logger"
commonUtils "github.com/n0rdy/proteus/utils"
bolt "go.etcd.io/bbolt"
"strings"
)
const (
restBucket = "REST"
)
var restBucketBytes = []byte(restBucket)
type EndpointsDb struct {
db *bolt.DB
}
func NewEndpointsDb() (*EndpointsDb, error) {
boltDb, err := bolt.Open(commonUtils.GetOsSpecificDbDir()+"proteus_endpoints.db", 0600, nil)
if err != nil {
return nil, err
}
err = boltDb.Update(func(tx *bolt.Tx) error {
_, err = tx.CreateBucketIfNotExists(restBucketBytes)
if err != nil {
logger.Error("error on creating BoltDB bucket: "+restBucket, err)
return err
}
return nil
})
if err != nil {
return nil, err
}
return &EndpointsDb{db: boltDb}, nil
}
func (edb *EndpointsDb) Close() error {
return edb.db.Close()
}
func (edb *EndpointsDb) GetAllRest() ([]models.RestEndpoint, error) {
var res []models.RestEndpoint
err := edb.db.View(func(tx *bolt.Tx) error {
b := tx.Bucket(restBucketBytes)
return b.ForEach(func(k, v []byte) error {
var endpoint models.RestEndpoint
err := utils.Deserialize(v, &endpoint)
if err != nil {
logger.Error("error on deserializing BoltDB value for key: "+string(k), err)
return err
}
res = append(res, endpoint)
return nil
})
})
if err != nil {
logger.Error("error on getting BoltDB values for bucket: "+restBucket, err)
return nil, err
}
if res == nil {
return []models.RestEndpoint{}, nil
}
return res, nil
}
func (edb *EndpointsDb) GetOneRest(method string, path string) (*models.RestEndpoint, error) {
var res models.RestEndpoint
err := edb.db.View(func(tx *bolt.Tx) error {
b := tx.Bucket(restBucketBytes)
v := b.Get([]byte(edb.toKey(method, path)))
if v == nil {
return nil
}
return utils.Deserialize(v, &res)
})
if err != nil {
logger.Error("error on getting BoltDB value for key: "+edb.toKey(method, path), err)
return nil, err
}
if res.Path == "" {
return nil, nil
}
return &res, nil
}
func (edb *EndpointsDb) InsertOneRest(endpoint models.RestEndpoint) error {
serialized, err := utils.Serialize(endpoint)
if err != nil {
logger.Error("error on serializing REST endpoint: "+endpoint.Path, err)
return err
}
err = edb.db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket(restBucketBytes)
return b.Put([]byte(edb.toKey(endpoint.Method, endpoint.Path)), serialized)
})
if err != nil {
logger.Error("error on inserting REST endpoint: "+endpoint.Path, err)
return err
}
return nil
}
func (edb *EndpointsDb) DeleteAllRest() error {
err := edb.db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket(restBucketBytes)
return b.ForEach(func(k, v []byte) error {
return b.Delete(k)
})
})
if err != nil {
logger.Error("error on deleting all REST endpoints", err)
return err
}
return nil
}
func (edb *EndpointsDb) DeleteOneRest(method string, path string) error {
err := edb.db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket(restBucketBytes)
return b.Delete([]byte(edb.toKey(method, path)))
})
if err != nil {
logger.Error("error on deleting REST endpoint: "+edb.toKey(method, path), err)
return err
}
return nil
}
func (edb *EndpointsDb) toKey(method string, path string) string {
return strings.ToLower(method + "_" + path)
}