Implement connstr
This commit is contained in:
@@ -1,5 +1,11 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"path"
|
||||
)
|
||||
|
||||
const (
|
||||
DBTYPE_MEM = iota
|
||||
DBTYPE_GDBM
|
||||
@@ -11,11 +17,56 @@ type connData struct {
|
||||
str string
|
||||
}
|
||||
|
||||
func toConnData(s string) connData {
|
||||
func (cd *connData) DbType() int {
|
||||
return cd.typ
|
||||
}
|
||||
|
||||
func (cd *connData) String() string {
|
||||
return cd.str
|
||||
}
|
||||
|
||||
func probePostgres(s string) (string, bool) {
|
||||
v, err := url.Parse(s)
|
||||
if err != nil {
|
||||
return "", false
|
||||
}
|
||||
if v.Scheme != "postgres" {
|
||||
return "", false
|
||||
}
|
||||
return s, true
|
||||
}
|
||||
|
||||
func probeGdbm(s string) (string, bool) {
|
||||
if !path.IsAbs(s) {
|
||||
return "", false
|
||||
}
|
||||
if path.Ext(s) != ".gdbm" {
|
||||
return "", false
|
||||
}
|
||||
s = path.Clean(s)
|
||||
return s, true
|
||||
}
|
||||
|
||||
func toConnData(connStr string) (connData, error) {
|
||||
var o connData
|
||||
|
||||
if s == "" {
|
||||
return o
|
||||
if connStr == "" {
|
||||
return o, nil
|
||||
}
|
||||
return o
|
||||
|
||||
v, ok := probePostgres(connStr)
|
||||
if ok {
|
||||
o.typ = DBTYPE_POSTGRES
|
||||
o.str = v
|
||||
return o, nil
|
||||
}
|
||||
|
||||
v, ok = probeGdbm(connStr)
|
||||
if ok {
|
||||
o.typ = DBTYPE_GDBM
|
||||
o.str = v
|
||||
return o, nil
|
||||
}
|
||||
|
||||
return o, fmt.Errorf("invalid connection string: %s", connStr)
|
||||
}
|
||||
|
||||
37
internal/storage/parse_test.go
Normal file
37
internal/storage/parse_test.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestParseConnStr(t *testing.T) {
|
||||
svc := NewMenuStorageService("")
|
||||
err := svc.SetConn("postgres://foo:bar@localhost:5432/baz")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err = svc.SetConn("/foo/bar/baz.gdbm")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err = svc.SetConn("foo/bar/baz.gdbm")
|
||||
if err == nil {
|
||||
t.Fatalf("expected error")
|
||||
}
|
||||
err = svc.SetConn("http://foo/bar")
|
||||
if err == nil {
|
||||
t.Fatalf("expected error")
|
||||
}
|
||||
err = svc.SetConn("foo/bar/baz.txt")
|
||||
if err == nil {
|
||||
t.Fatalf("expected error")
|
||||
}
|
||||
err = svc.SetConn("/foo/bar")
|
||||
if err == nil {
|
||||
t.Fatalf("expected error")
|
||||
}
|
||||
err = svc.SetConn("foo/bar")
|
||||
if err == nil {
|
||||
t.Fatalf("expected error")
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
|
||||
"git.defalsify.org/vise.git/db"
|
||||
fsdb "git.defalsify.org/vise.git/db/fs"
|
||||
@@ -24,11 +23,12 @@ type StorageService interface {
|
||||
GetPersister(ctx context.Context) (*persist.Persister, error)
|
||||
GetUserdataDb(ctx context.Context) db.Db
|
||||
GetResource(ctx context.Context) (resource.Resource, error)
|
||||
EnsureDbDir() error
|
||||
SetConn(connStr string) error
|
||||
}
|
||||
|
||||
type MenuStorageService struct {
|
||||
dbDir string
|
||||
//dbDir string
|
||||
conn connData
|
||||
resourceDir string
|
||||
resourceStore db.Db
|
||||
stateStore db.Db
|
||||
@@ -51,36 +51,47 @@ func buildConnStr() string {
|
||||
return connString
|
||||
}
|
||||
|
||||
func NewMenuStorageService(dbDir string, resourceDir string) *MenuStorageService {
|
||||
func NewMenuStorageService(resourceDir string) *MenuStorageService {
|
||||
return &MenuStorageService{
|
||||
dbDir: dbDir,
|
||||
resourceDir: resourceDir,
|
||||
}
|
||||
}
|
||||
|
||||
func (ms *MenuStorageService) getOrCreateDb(ctx context.Context, existingDb db.Db, fileName string) (db.Db, error) {
|
||||
database, ok := ctx.Value("Database").(string)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("failed to select the database")
|
||||
func (ms *MenuStorageService) SetConn(connStr string) error {
|
||||
o, err := toConnData(connStr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ms.conn = o
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ms *MenuStorageService) getOrCreateDb(ctx context.Context, existingDb db.Db, fileName string) (db.Db, error) {
|
||||
var newDb db.Db
|
||||
var err error
|
||||
// database, ok := ctx.Value("Database").(string)
|
||||
// if !ok {
|
||||
// return nil, fmt.Errorf("failed to select the database")
|
||||
// }
|
||||
|
||||
if existingDb != nil {
|
||||
return existingDb, nil
|
||||
}
|
||||
|
||||
var newDb db.Db
|
||||
var err error
|
||||
|
||||
if database == "postgres" {
|
||||
dbTyp := ms.conn.DbType()
|
||||
if dbTyp == DBTYPE_POSTGRES {
|
||||
newDb = postgres.NewPgDb()
|
||||
connStr := buildConnStr()
|
||||
err = newDb.Connect(ctx, connStr)
|
||||
} else {
|
||||
} else if dbTyp == DBTYPE_GDBM {
|
||||
err = ms.ensureDbDir()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
newDb = gdbmstorage.NewThreadGdbmDb()
|
||||
storeFile := path.Join(ms.dbDir, fileName)
|
||||
err = newDb.Connect(ctx, storeFile)
|
||||
} else {
|
||||
return nil, fmt.Errorf("unsupported connection string: %s", ms.conn.String())
|
||||
}
|
||||
|
||||
err = newDb.Connect(ctx, ms.conn.String())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -137,8 +148,8 @@ func (ms *MenuStorageService) GetStateStore(ctx context.Context) (db.Db, error)
|
||||
return ms.stateStore, nil
|
||||
}
|
||||
|
||||
func (ms *MenuStorageService) EnsureDbDir() error {
|
||||
err := os.MkdirAll(ms.dbDir, 0700)
|
||||
func (ms *MenuStorageService) ensureDbDir() error {
|
||||
err := os.MkdirAll(ms.conn.String(), 0700)
|
||||
if err != nil {
|
||||
return fmt.Errorf("state dir create exited with error: %v\n", err)
|
||||
}
|
||||
|
||||
@@ -41,9 +41,11 @@ func TestEngine(sessionId string) (engine.Engine, func(), chan bool) {
|
||||
|
||||
dbDir := ".test_state"
|
||||
resourceDir := scriptDir
|
||||
menuStorageService := storage.NewMenuStorageService(dbDir, resourceDir)
|
||||
//menuStorageService := storage.NewMenuStorageService(dbDir, resourceDir)
|
||||
menuStorageService := storage.NewMenuStorageService(resourceDir)
|
||||
|
||||
err := menuStorageService.EnsureDbDir()
|
||||
//err := menuStorageService.EnsureDbDir()
|
||||
err := menuStorageService.SetConn(dbDir)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, err.Error())
|
||||
os.Exit(1)
|
||||
|
||||
Reference in New Issue
Block a user