diff --git a/db/db.go b/db/db.go index 1f85c428..ea36c4fc 100644 --- a/db/db.go +++ b/db/db.go @@ -43,7 +43,7 @@ func Setup(opts *cnf.Options) (err error) { log.WithPrefix("db").Infof("Starting database at %s", opts.DB.Path) - db, err = kvs.New(opts.DB.Path) + db, err = kvs.New(opts) return diff --git a/kvs/boltdb/main.go b/kvs/boltdb/main.go index 365ccb5d..cad6692a 100644 --- a/kvs/boltdb/main.go +++ b/kvs/boltdb/main.go @@ -19,6 +19,7 @@ import ( "github.com/boltdb/bolt" + "github.com/abcum/surreal/cnf" "github.com/abcum/surreal/kvs" ) @@ -28,11 +29,11 @@ func init() { kvs.Register("boltdb", New) } -func New(path string) (ds kvs.DS, err error) { +func New(opts *cnf.Options) (ds kvs.DS, err error) { var db *bolt.DB - path = strings.TrimLeft(path, "boltdb://") + path := strings.TrimLeft(opts.DB.Path, "boltdb://") db, err = bolt.Open(path, 0666, nil) diff --git a/kvs/db.go b/kvs/db.go index 82d29473..e1a98f7f 100644 --- a/kvs/db.go +++ b/kvs/db.go @@ -16,30 +16,32 @@ package kvs import ( "strings" + + "github.com/abcum/surreal/cnf" ) -var stores = make(map[string]func(string) (DS, error)) +var stores = make(map[string]func(*cnf.Options) (DS, error)) // DB is a database handle to a single Surreal cluster. type DB struct { ds DS } -func New(path string) (db *DB, err error) { // New sets up the underlying key-value store +func New(opts *cnf.Options) (db *DB, err error) { var ds DS - if strings.HasPrefix(path, "boltdb://") { - ds, err = stores["boltdb"](path) + if strings.HasPrefix(opts.DB.Path, "boltdb://") { + ds, err = stores["boltdb"](opts) } - if strings.HasPrefix(path, "mysql://") { - ds, err = stores["mysql"](path) + if strings.HasPrefix(opts.DB.Path, "mysql://") { + ds, err = stores["mysql"](opts) } - if strings.HasPrefix(path, "pgsql://") { - ds, err = stores["pgsql"](path) + if strings.HasPrefix(opts.DB.Path, "pgsql://") { + ds, err = stores["pgsql"](opts) } return &DB{ds: ds}, err @@ -240,7 +242,7 @@ func (db *DB) Close() (err error) { } -func Register(name string, constructor func(string) (DS, error)) { +func Register(name string, constructor func(*cnf.Options) (DS, error)) { stores[name] = constructor diff --git a/kvs/mysql/main.go b/kvs/mysql/main.go index e3c01866..d7c44c5a 100644 --- a/kvs/mysql/main.go +++ b/kvs/mysql/main.go @@ -20,6 +20,7 @@ import ( "database/sql" _ "github.com/go-sql-driver/mysql" + "github.com/abcum/surreal/cnf" "github.com/abcum/surreal/kvs" ) @@ -27,11 +28,11 @@ func init() { kvs.Register("mysql", New) } -func New(path string) (ds kvs.DS, err error) { +func New(opts *cnf.Options) (ds kvs.DS, err error) { var db *sql.DB - path = strings.TrimLeft(path, "mysql://") + path := strings.TrimLeft(opts.DB.Path, "mysql://") db, err = sql.Open("mysql", path) diff --git a/kvs/pgsql/main.go b/kvs/pgsql/main.go index 0ed1e94e..faf8202e 100644 --- a/kvs/pgsql/main.go +++ b/kvs/pgsql/main.go @@ -20,6 +20,7 @@ import ( "database/sql" _ "github.com/lib/pq" + "github.com/abcum/surreal/cnf" "github.com/abcum/surreal/kvs" ) @@ -27,11 +28,11 @@ func init() { kvs.Register("pgsql", New) } -func New(path string) (ds kvs.DS, err error) { +func New(opts *cnf.Options) (ds kvs.DS, err error) { var db *sql.DB - path = strings.TrimLeft(path, "pgsql://") + path := strings.TrimLeft(opts.DB.Path, "pgsql://") db, err = sql.Open("postgres", path)