Enable specifying database file path

This commit is contained in:
Tobie Morgan Hitchcock 2016-06-15 13:38:37 +01:00
parent 4706de4413
commit 51a2cb3415
2 changed files with 28 additions and 10 deletions

View file

@ -37,9 +37,27 @@ var db *kvs.DB
// Setup sets up the connection with the data layer
func Setup(opts *cnf.Options) (err error) {
log.WithPrefix("db").Infof("Connecting to database at %s", opts.Store)
log.WithPrefix("db").Infof("Starting database at %s", opts.DB.Path)
db, err = kvs.New()
db, err = kvs.New(opts.DB.Path)
/*ticker := time.NewTicker(5 * time.Second)
quit := make(chan struct{})
go func() {
for {
select {
case <-ticker.C:
t := time.Now()
n := fmt.Sprintf("%d-%02d-%02dT%02d-%02d-%02d-%d", t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), t.Nanosecond())
p := fmt.Sprintf("dev/%s.backup.db", n)
db.Save(p)
case <-quit:
ticker.Stop()
return
}
}
}()*/
return
@ -48,7 +66,7 @@ func Setup(opts *cnf.Options) (err error) {
// Exit shuts down the connection with the data layer
func Exit() {
log.WithPrefix("db").Infof("Disconnecting from database")
log.WithPrefix("db").Infof("Gracefully shutting down database")
db.Close()
@ -84,6 +102,8 @@ func status(e error) interface{} {
switch e.(type) {
default:
return "OK"
case error:
return "ERR"
case *kvs.DBError:
return "ERR_DB"
case *kvs.TXError:
@ -96,9 +116,11 @@ func status(e error) interface{} {
}
func detail(e error) interface{} {
switch e.(type) {
switch err := e.(type) {
default:
return nil
case error:
return err.Error()
case *kvs.DBError:
return "A database error occured"
case *kvs.TXError:

View file

@ -15,8 +15,6 @@
package kvs
import (
"os"
"github.com/boltdb/bolt"
)
@ -25,13 +23,11 @@ type DB struct {
db *bolt.DB
}
func New() (db *DB, err error) {
func New(path string) (db *DB, err error) {
var bo *bolt.DB
os.Remove("dev/bolt.db") // TODO remove this code!!!
bo, err = bolt.Open("dev/bolt.db", 0666, nil)
bo, err = bolt.Open(path, 0666, nil)
bo.Update(func(tx *bolt.Tx) error {
tx.CreateBucketIfNotExists(bucket)