Pass full configuration data into data store packages
This commit is contained in:
parent
010f90ceec
commit
a556096cee
5 changed files with 21 additions and 16 deletions
2
db/db.go
2
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)
|
log.WithPrefix("db").Infof("Starting database at %s", opts.DB.Path)
|
||||||
|
|
||||||
db, err = kvs.New(opts.DB.Path)
|
db, err = kvs.New(opts)
|
||||||
|
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
|
@ -19,6 +19,7 @@ import (
|
||||||
|
|
||||||
"github.com/boltdb/bolt"
|
"github.com/boltdb/bolt"
|
||||||
|
|
||||||
|
"github.com/abcum/surreal/cnf"
|
||||||
"github.com/abcum/surreal/kvs"
|
"github.com/abcum/surreal/kvs"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -28,11 +29,11 @@ func init() {
|
||||||
kvs.Register("boltdb", New)
|
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
|
var db *bolt.DB
|
||||||
|
|
||||||
path = strings.TrimLeft(path, "boltdb://")
|
path := strings.TrimLeft(opts.DB.Path, "boltdb://")
|
||||||
|
|
||||||
db, err = bolt.Open(path, 0666, nil)
|
db, err = bolt.Open(path, 0666, nil)
|
||||||
|
|
||||||
|
|
20
kvs/db.go
20
kvs/db.go
|
@ -16,30 +16,32 @@ package kvs
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"strings"
|
"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.
|
// DB is a database handle to a single Surreal cluster.
|
||||||
type DB struct {
|
type DB struct {
|
||||||
ds DS
|
ds DS
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(path string) (db *DB, err error) {
|
|
||||||
// New sets up the underlying key-value store
|
// New sets up the underlying key-value store
|
||||||
|
func New(opts *cnf.Options) (db *DB, err error) {
|
||||||
|
|
||||||
var ds DS
|
var ds DS
|
||||||
|
|
||||||
if strings.HasPrefix(path, "boltdb://") {
|
if strings.HasPrefix(opts.DB.Path, "boltdb://") {
|
||||||
ds, err = stores["boltdb"](path)
|
ds, err = stores["boltdb"](opts)
|
||||||
}
|
}
|
||||||
|
|
||||||
if strings.HasPrefix(path, "mysql://") {
|
if strings.HasPrefix(opts.DB.Path, "mysql://") {
|
||||||
ds, err = stores["mysql"](path)
|
ds, err = stores["mysql"](opts)
|
||||||
}
|
}
|
||||||
|
|
||||||
if strings.HasPrefix(path, "pgsql://") {
|
if strings.HasPrefix(opts.DB.Path, "pgsql://") {
|
||||||
ds, err = stores["pgsql"](path)
|
ds, err = stores["pgsql"](opts)
|
||||||
}
|
}
|
||||||
|
|
||||||
return &DB{ds: ds}, err
|
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
|
stores[name] = constructor
|
||||||
|
|
||||||
|
|
|
@ -20,6 +20,7 @@ import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
|
|
||||||
|
"github.com/abcum/surreal/cnf"
|
||||||
"github.com/abcum/surreal/kvs"
|
"github.com/abcum/surreal/kvs"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -27,11 +28,11 @@ func init() {
|
||||||
kvs.Register("mysql", New)
|
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
|
var db *sql.DB
|
||||||
|
|
||||||
path = strings.TrimLeft(path, "mysql://")
|
path := strings.TrimLeft(opts.DB.Path, "mysql://")
|
||||||
|
|
||||||
db, err = sql.Open("mysql", path)
|
db, err = sql.Open("mysql", path)
|
||||||
|
|
||||||
|
|
|
@ -20,6 +20,7 @@ import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
_ "github.com/lib/pq"
|
_ "github.com/lib/pq"
|
||||||
|
|
||||||
|
"github.com/abcum/surreal/cnf"
|
||||||
"github.com/abcum/surreal/kvs"
|
"github.com/abcum/surreal/kvs"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -27,11 +28,11 @@ func init() {
|
||||||
kvs.Register("pgsql", New)
|
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
|
var db *sql.DB
|
||||||
|
|
||||||
path = strings.TrimLeft(path, "pgsql://")
|
path := strings.TrimLeft(opts.DB.Path, "pgsql://")
|
||||||
|
|
||||||
db, err = sql.Open("postgres", path)
|
db, err = sql.Open("postgres", path)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue