quintodrome/persistence/db_storm/storm.go

37 lines
652 B
Go
Raw Normal View History

2020-01-10 15:41:35 -09:00
package db_storm
2020-01-10 11:08:23 -09:00
import (
"fmt"
"os"
2020-01-10 17:51:57 -09:00
"path"
"path/filepath"
2020-01-10 11:08:23 -09:00
"sync"
"github.com/asdine/storm"
2020-01-10 17:51:57 -09:00
"github.com/cloudsonic/sonic-server/conf"
"github.com/cloudsonic/sonic-server/log"
2020-01-10 11:08:23 -09:00
)
var (
_dbInstance *storm.DB
once sync.Once
)
func Db() *storm.DB {
once.Do(func() {
err := os.MkdirAll(conf.Sonic.DbPath, 0700)
if err != nil {
panic(err)
}
2020-01-10 17:51:57 -09:00
dbPath := path.Join(conf.Sonic.DbPath, "storm.db")
dbPath = fmt.Sprintf(".%c%s", filepath.Separator, dbPath)
2020-01-10 17:51:57 -09:00
instance, err := storm.Open(dbPath)
log.Debug("Opening Storm DB from: " + dbPath)
2020-01-10 11:08:23 -09:00
if err != nil {
panic(err)
}
_dbInstance = instance
})
return _dbInstance
}