2020-06-09 13:04:44 +00:00
|
|
|
package database
|
|
|
|
|
|
|
|
import (
|
|
|
|
"go.etcd.io/bbolt"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
firedBucket = []byte("fired")
|
2021-02-22 18:27:49 +00:00
|
|
|
unbanBucket = []byte("unban")
|
2020-06-09 13:04:44 +00:00
|
|
|
|
|
|
|
buckets = [][]byte{
|
|
|
|
firedBucket,
|
2021-02-22 18:27:49 +00:00
|
|
|
unbanBucket,
|
2020-06-09 13:04:44 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
type Database struct {
|
|
|
|
db *bbolt.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
func Load(dbPath string) (*Database, error) {
|
2022-01-07 20:47:35 +00:00
|
|
|
db, err := bbolt.Open(dbPath, 0o600, bbolt.DefaultOptions)
|
2020-06-09 13:04:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, b := range buckets {
|
|
|
|
if err := db.Update(func(tx *bbolt.Tx) error {
|
|
|
|
_, err := tx.CreateBucketIfNotExists(b)
|
|
|
|
return err
|
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return &Database{
|
|
|
|
db: db,
|
|
|
|
}, nil
|
|
|
|
}
|