-
Notifications
You must be signed in to change notification settings - Fork 1
/
db.go
122 lines (105 loc) · 2.43 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
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"strconv"
"time"
"github.com/boltdb/bolt"
)
type store interface {
putAlert(a alertData) error
getAlert(id string) []byte
deleteAlert(id string) error
backup(w http.ResponseWriter)
getAlertsByPrefix(prefix string) ([]byte, int, error)
}
type boltStore struct {
db *bolt.DB
}
func newBoltStore() (*boltStore, error) {
db, err := bolt.Open("alertboard.db", 0600, nil)
if err != nil {
return nil, err
}
b := &boltStore{db}
//create the bucket
err = db.Update(func(tx *bolt.Tx) error {
_, err := tx.CreateBucketIfNotExists([]byte("alerts"))
if err != nil {
return fmt.Errorf("create bucket: %s", err)
}
return nil
})
return b, err
}
func (b *boltStore) putAlert(a alertData) error {
if a.Time.IsZero() {
a.Time = time.Now()
}
if a.Status == "" {
a.Status = "Open"
}
data, err := json.Marshal(a)
if err != nil {
return err
}
err = b.db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("alerts"))
err := b.Put([]byte(a.ID), data)
return err
})
return err
}
func (b *boltStore) backup(w http.ResponseWriter) {
err := b.db.View(func(tx *bolt.Tx) error {
w.Header().Set("Content-Type", "application/octet-stream")
w.Header().Set("Content-Disposition", `attachment; filename="alertboard.db"`)
w.Header().Set("Content-Length", strconv.Itoa(int(tx.Size())))
_, err := tx.WriteTo(w)
return err
})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func (b *boltStore) getAlert(id string) []byte {
var alert []byte
b.db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("alerts"))
alert = b.Get([]byte(id))
return nil
})
return alert
}
func (b *boltStore) deleteAlert(id string) error {
err := b.db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("alerts"))
err := b.Delete([]byte(id))
return err
})
return err
}
func (b *boltStore) getAlertsByPrefix(prefix string) ([]byte, int, error) {
alerts := make([]alertData, 0, 0)
var alert alertData
err := b.db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("alerts"))
c := b.Cursor()
p := []byte(prefix)
for k, v := c.Seek(p); bytes.HasPrefix(k, p) && k != nil; k, v = c.Next() {
err := json.Unmarshal(v, &alert)
if err != nil {
return err
}
alerts = append(alerts, alert)
}
return nil
})
data, _ := json.Marshal(alerts)
return data, len(alerts), err
}
func (b *boltStore) Close() {
b.db.Close()
}