Pass full configuration data into data store packages

This commit is contained in:
Tobie Morgan Hitchcock 2016-07-18 13:33:26 +01:00
parent 010f90ceec
commit a556096cee
5 changed files with 21 additions and 16 deletions

View file

@ -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

View file

@ -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)

View file

@ -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

View file

@ -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)

View file

@ -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)