Implement connstring handling #247

Merged
lash merged 28 commits from lash/purify-more into master 2025-01-09 13:03:29 +01:00
12 changed files with 98 additions and 125 deletions
Showing only changes of commit 450dfa02cc - Show all commits

View File

@ -8,7 +8,6 @@ import (
"os" "os"
"os/signal" "os/signal"
"path" "path"
"path/filepath"
"strconv" "strconv"
"syscall" "syscall"
@ -55,12 +54,13 @@ func main() {
flag.UintVar(&port, "p", initializers.GetEnvUint("PORT", 7123), "http port") flag.UintVar(&port, "p", initializers.GetEnvUint("PORT", 7123), "http port")
flag.Parse() flag.Parse()
if connStr == ".state" { if connStr != "" {
connStr, err = filepath.Abs(connStr) connStr = config.DbConn
if err != nil { }
fmt.Fprintf(os.Stderr, "auto connstr generate error: %v", err) connData, err := storage.ToConnData(config.DbConn)
os.Exit(1) if err != nil {
} fmt.Fprintf(os.Stderr, "connstr err: %v", err)
os.Exit(1)
} }
logg.Infof("start command", "build", build, "dbdir", connStr, "resourcedir", resourceDir, "outputsize", size) logg.Infof("start command", "build", build, "dbdir", connStr, "resourcedir", resourceDir, "outputsize", size)
@ -81,7 +81,7 @@ func main() {
} }
menuStorageService := storage.NewMenuStorageService(resourceDir) menuStorageService := storage.NewMenuStorageService(resourceDir)
err = menuStorageService.SetConn(connStr) menuStorageService = menuStorageService.WithConn(connData)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, err.Error()) fmt.Fprintf(os.Stderr, err.Error())
os.Exit(1) os.Exit(1)

View File

@ -7,7 +7,6 @@ import (
"os" "os"
"os/signal" "os/signal"
"path" "path"
"path/filepath"
"syscall" "syscall"
"git.defalsify.org/vise.git/engine" "git.defalsify.org/vise.git/engine"
@ -47,8 +46,8 @@ func (p *asyncRequestParser) GetInput(r any) ([]byte, error) {
func main() { func main() {
config.LoadConfig() config.LoadConfig()
var sessionId string
var connStr string var connStr string
var sessionId string
var resourceDir string var resourceDir string
var size uint var size uint
var database string var database string
@ -59,19 +58,20 @@ func main() {
flag.StringVar(&sessionId, "session-id", "075xx2123", "session id") flag.StringVar(&sessionId, "session-id", "075xx2123", "session id")
flag.StringVar(&resourceDir, "resourcedir", path.Join("services", "registration"), "resource dir") flag.StringVar(&resourceDir, "resourcedir", path.Join("services", "registration"), "resource dir")
flag.StringVar(&connStr, "c", ".state", "connection string") flag.StringVar(&connStr, "c", "", "connection string")
flag.BoolVar(&engineDebug, "d", false, "use engine debug output") flag.BoolVar(&engineDebug, "d", false, "use engine debug output")
flag.UintVar(&size, "s", 160, "max size of output") flag.UintVar(&size, "s", 160, "max size of output")
flag.StringVar(&host, "h", initializers.GetEnv("HOST", "127.0.0.1"), "http host") flag.StringVar(&host, "h", initializers.GetEnv("HOST", "127.0.0.1"), "http host")
flag.UintVar(&port, "p", initializers.GetEnvUint("PORT", 7123), "http port") flag.UintVar(&port, "p", initializers.GetEnvUint("PORT", 7123), "http port")
flag.Parse() flag.Parse()
if connStr == ".state" { if connStr != "" {
connStr, err = filepath.Abs(connStr) connStr = config.DbConn
if err != nil { }
fmt.Fprintf(os.Stderr, "auto connstr generate error: %v", err) connData, err := storage.ToConnData(config.DbConn)
os.Exit(1) if err != nil {
} fmt.Fprintf(os.Stderr, "connstr err: %v", err)
os.Exit(1)
} }
logg.Infof("start command", "connstr", connStr, "resourcedir", resourceDir, "outputsize", size, "sessionId", sessionId) logg.Infof("start command", "connstr", connStr, "resourcedir", resourceDir, "outputsize", size, "sessionId", sessionId)
@ -92,7 +92,7 @@ func main() {
} }
menuStorageService := storage.NewMenuStorageService(resourceDir) menuStorageService := storage.NewMenuStorageService(resourceDir)
err = menuStorageService.SetConn(connStr) menuStorageService = menuStorageService.WithConn(connData)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, err.Error()) fmt.Fprintf(os.Stderr, err.Error())
os.Exit(1) os.Exit(1)

View File

@ -8,7 +8,6 @@ import (
"os" "os"
"os/signal" "os/signal"
"path" "path"
"path/filepath"
"strconv" "strconv"
"syscall" "syscall"
@ -47,19 +46,20 @@ func main() {
var err error var err error
flag.StringVar(&resourceDir, "resourcedir", path.Join("services", "registration"), "resource dir") flag.StringVar(&resourceDir, "resourcedir", path.Join("services", "registration"), "resource dir")
flag.StringVar(&connStr, "c", ".state", "connection string") flag.StringVar(&connStr, "c", "", "connection string")
flag.BoolVar(&engineDebug, "d", false, "use engine debug output") flag.BoolVar(&engineDebug, "d", false, "use engine debug output")
flag.UintVar(&size, "s", 160, "max size of output") flag.UintVar(&size, "s", 160, "max size of output")
flag.StringVar(&host, "h", initializers.GetEnv("HOST", "127.0.0.1"), "http host") flag.StringVar(&host, "h", initializers.GetEnv("HOST", "127.0.0.1"), "http host")
flag.UintVar(&port, "p", initializers.GetEnvUint("PORT", 7123), "http port") flag.UintVar(&port, "p", initializers.GetEnvUint("PORT", 7123), "http port")
flag.Parse() flag.Parse()
if connStr == ".state" { if connStr != "" {
connStr, err = filepath.Abs(connStr) connStr = config.DbConn
if err != nil { }
fmt.Fprintf(os.Stderr, "auto connstr generate error: %v", err) connData, err := storage.ToConnData(config.DbConn)
os.Exit(1) if err != nil {
} fmt.Fprintf(os.Stderr, "connstr err: %v", err)
os.Exit(1)
} }
logg.Infof("start command", "connstr", connStr, "resourcedir", resourceDir, "outputsize", size) logg.Infof("start command", "connstr", connStr, "resourcedir", resourceDir, "outputsize", size)
@ -80,12 +80,8 @@ func main() {
} }
menuStorageService := storage.NewMenuStorageService(resourceDir) menuStorageService := storage.NewMenuStorageService(resourceDir)
err = menuStorageService.SetConn(connStr) menuStorageService = menuStorageService.WithConn(connData)
if err != nil {
fmt.Fprintf(os.Stderr, err.Error())
os.Exit(1)
}
rs, err := menuStorageService.GetResource(ctx) rs, err := menuStorageService.GetResource(ctx)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, err.Error()) fmt.Fprintf(os.Stderr, err.Error())

View File

@ -6,7 +6,6 @@ import (
"fmt" "fmt"
"os" "os"
"path" "path"
"path/filepath"
"git.defalsify.org/vise.git/engine" "git.defalsify.org/vise.git/engine"
"git.defalsify.org/vise.git/logging" "git.defalsify.org/vise.git/logging"
@ -39,17 +38,18 @@ func main() {
var err error var err error
flag.StringVar(&sessionId, "session-id", "075xx2123", "session id") flag.StringVar(&sessionId, "session-id", "075xx2123", "session id")
flag.StringVar(&connStr, "c", ".state", "connection string") flag.StringVar(&connStr, "c", "", "connection string")
flag.BoolVar(&engineDebug, "d", false, "use engine debug output") flag.BoolVar(&engineDebug, "d", false, "use engine debug output")
flag.UintVar(&size, "s", 160, "max size of output") flag.UintVar(&size, "s", 160, "max size of output")
flag.Parse() flag.Parse()
if connStr == ".state" { if connStr != "" {
connStr, err = filepath.Abs(connStr) connStr = config.DbConn
if err != nil { }
fmt.Fprintf(os.Stderr, "auto connstr generate error: %v", err) connData, err := storage.ToConnData(config.DbConn)
os.Exit(1) if err != nil {
} fmt.Fprintf(os.Stderr, "connstr err: %v", err)
os.Exit(1)
} }
logg.Infof("start command", "connstr", connStr, "outputsize", size) logg.Infof("start command", "connstr", connStr, "outputsize", size)
@ -69,12 +69,7 @@ func main() {
resourceDir := scriptDir resourceDir := scriptDir
menuStorageService := storage.NewMenuStorageService(resourceDir) menuStorageService := storage.NewMenuStorageService(resourceDir)
menuStorageService = menuStorageService.WithConn(connData)
err = menuStorageService.SetConn(connStr)
if err != nil {
fmt.Fprintf(os.Stderr, err.Error())
os.Exit(1)
}
rs, err := menuStorageService.GetResource(ctx) rs, err := menuStorageService.GetResource(ctx)
if err != nil { if err != nil {

View File

@ -23,19 +23,19 @@ type StorageServices interface {
GetPersister(ctx context.Context) (*persist.Persister, error) GetPersister(ctx context.Context) (*persist.Persister, error)
GetUserdataDb(ctx context.Context) (db.Db, error) GetUserdataDb(ctx context.Context) (db.Db, error)
GetResource(ctx context.Context) (resource.Resource, error) GetResource(ctx context.Context) (resource.Resource, error)
SetConn(connStr string) error SetConn(conn storage.ConnData)
} }
type StorageService struct { type StorageService struct {
svc *storage.MenuStorageService svc *storage.MenuStorageService
} }
func NewStorageService(connStr string) (*StorageService, error) { func NewStorageService(conn storage.ConnData) (*StorageService, error) {
svc := &StorageService{ svc := &StorageService{
svc: storage.NewMenuStorageService(""), svc: storage.NewMenuStorageService(""),
} }
err := svc.SetConn(connStr) svc.SetConn(conn)
return svc, err return svc, nil
} }
func(ss *StorageService) GetPersister(ctx context.Context) (*persist.Persister, error) { func(ss *StorageService) GetPersister(ctx context.Context) (*persist.Persister, error) {
@ -50,6 +50,6 @@ func(ss *StorageService) GetResource(ctx context.Context) (resource.Resource, er
return nil, errors.New("not implemented") return nil, errors.New("not implemented")
} }
func(ss *StorageService) SetConn(connStr string) error { func(ss *StorageService) SetConn(conn storage.ConnData) {
return ss.svc.SetConn(connStr) ss.svc = ss.svc.WithConn(conn)
} }

View File

@ -34,6 +34,7 @@ var (
VoucherTransfersURL string VoucherTransfersURL string
VoucherDataURL string VoucherDataURL string
CheckAliasURL string CheckAliasURL string
DbConn string
) )
func setBase() error { func setBase() error {
@ -43,14 +44,20 @@ func setBase() error {
dataURLBase = initializers.GetEnv("DATA_URL_BASE", "http://localhost:5006") dataURLBase = initializers.GetEnv("DATA_URL_BASE", "http://localhost:5006")
BearerToken = initializers.GetEnv("BEARER_TOKEN", "") BearerToken = initializers.GetEnv("BEARER_TOKEN", "")
_, err = url.JoinPath(custodialURLBase, "/foo") _, err = url.Parse(custodialURLBase)
if err != nil { if err != nil {
return err return err
} }
_, err = url.JoinPath(dataURLBase, "/bar") _, err = url.Parse(dataURLBase)
if err != nil { if err != nil {
return err return err
} }
return nil
}
func setConn() error {
DbConn = initializers.GetEnv("DB_CONN", "")
return nil return nil
} }
@ -60,6 +67,10 @@ func LoadConfig() error {
if err != nil { if err != nil {
return err return err
} }
err = setConn()
if err != nil {
return err
}
CreateAccountURL, _ = url.JoinPath(custodialURLBase, createAccountPath) CreateAccountURL, _ = url.JoinPath(custodialURLBase, createAccountPath)
TrackStatusURL, _ = url.JoinPath(custodialURLBase, trackStatusPath) TrackStatusURL, _ = url.JoinPath(custodialURLBase, trackStatusPath)
BalanceURL, _ = url.JoinPath(custodialURLBase, balancePathPrefix) BalanceURL, _ = url.JoinPath(custodialURLBase, balancePathPrefix)

View File

@ -7,7 +7,6 @@ import (
"fmt" "fmt"
"os" "os"
"path" "path"
"path/filepath"
"git.defalsify.org/vise.git/logging" "git.defalsify.org/vise.git/logging"
"git.grassecon.net/urdt/ussd/config" "git.grassecon.net/urdt/ussd/config"
@ -36,19 +35,20 @@ func main() {
var err error var err error
flag.StringVar(&sessionId, "session-id", "075xx2123", "session id") flag.StringVar(&sessionId, "session-id", "075xx2123", "session id")
flag.StringVar(&connStr, "c", ".", "connection string") flag.StringVar(&connStr, "c", "", "connection string")
flag.BoolVar(&engineDebug, "d", false, "use engine debug output") flag.BoolVar(&engineDebug, "d", false, "use engine debug output")
flag.Parse() flag.Parse()
if connStr == "." { if connStr != "" {
connStr, err = filepath.Abs(".state/state.gdbm") connStr = config.DbConn
if err != nil { }
fmt.Fprintf(os.Stderr, "auto connstr generate error: %v", err) connData, err := storage.ToConnData(config.DbConn)
os.Exit(1) if err != nil {
} fmt.Fprintf(os.Stderr, "connstr err: %v", err)
os.Exit(1)
} }
logg.Infof("start command", "connstr", connStr) logg.Infof("start command", "conn", connData)
ctx := context.Background() ctx := context.Background()
ctx = context.WithValue(ctx, "SessionId", sessionId) ctx = context.WithValue(ctx, "SessionId", sessionId)
@ -56,12 +56,8 @@ func main() {
resourceDir := scriptDir resourceDir := scriptDir
menuStorageService := storage.NewMenuStorageService(resourceDir) menuStorageService := storage.NewMenuStorageService(resourceDir)
err = menuStorageService.SetConn(connStr) menuStorageService = menuStorageService.WithConn(connData)
if err != nil {
fmt.Fprintf(os.Stderr, "connection string error: %v", err)
os.Exit(1)
}
store, err := menuStorageService.GetUserdataDb(ctx) store, err := menuStorageService.GetUserdataDb(ctx)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, err.Error()) fmt.Fprintf(os.Stderr, err.Error())

View File

@ -6,7 +6,6 @@ import (
"fmt" "fmt"
"os" "os"
"path" "path"
"path/filepath"
"git.grassecon.net/urdt/ussd/config" "git.grassecon.net/urdt/ussd/config"
"git.grassecon.net/urdt/ussd/initializers" "git.grassecon.net/urdt/ussd/initializers"
@ -40,12 +39,13 @@ func main() {
flag.BoolVar(&engineDebug, "d", false, "use engine debug output") flag.BoolVar(&engineDebug, "d", false, "use engine debug output")
flag.Parse() flag.Parse()
if connStr == "." { if connStr != "" {
connStr, err = filepath.Abs(".state/state.gdbm") connStr = config.DbConn
if err != nil { }
fmt.Fprintf(os.Stderr, "auto connstr generate error: %v", err) connData, err := storage.ToConnData(config.DbConn)
os.Exit(1) if err != nil {
} fmt.Fprintf(os.Stderr, "connstr err: %v", err)
os.Exit(1)
} }
ctx := context.Background() ctx := context.Background()
@ -54,7 +54,7 @@ func main() {
resourceDir := scriptDir resourceDir := scriptDir
menuStorageService := storage.NewMenuStorageService(resourceDir) menuStorageService := storage.NewMenuStorageService(resourceDir)
err = menuStorageService.SetConn(connStr) menuStorageService = menuStorageService.WithConn(connData)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "connection string error: %v", err) fmt.Fprintf(os.Stderr, "connection string error: %v", err)
os.Exit(1) os.Exit(1)

View File

@ -12,16 +12,16 @@ const (
DBTYPE_POSTGRES DBTYPE_POSTGRES
) )
type connData struct { type ConnData struct {
typ int typ int
str string str string
} }
func (cd *connData) DbType() int { func (cd *ConnData) DbType() int {
return cd.typ return cd.typ
} }
func (cd *connData) String() string { func (cd *ConnData) String() string {
return cd.str return cd.str
} }
@ -44,8 +44,8 @@ func probeGdbm(s string) (string, bool) {
return s, true return s, true
} }
func toConnData(connStr string) (connData, error) { func ToConnData(connStr string) (ConnData, error) {
var o connData var o ConnData
if connStr == "" { if connStr == "" {
return o, nil return o, nil

View File

@ -5,32 +5,23 @@ import (
) )
func TestParseConnStr(t *testing.T) { func TestParseConnStr(t *testing.T) {
svc := NewMenuStorageService("") _, err := ToConnData("postgres://foo:bar@localhost:5432/baz")
err := svc.SetConn("postgres://foo:bar@localhost:5432/baz")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
err = svc.SetConn("/foo/bar/baz.gdbm") _, err = ToConnData("/foo/bar")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
err = svc.SetConn("foo/bar/baz.gdbm") _, err = ToConnData("/foo/bar/")
if err != nil {
t.Fatal(err)
}
_, err = ToConnData("foo/bar")
if err == nil { if err == nil {
t.Fatalf("expected error") t.Fatalf("expected error")
} }
err = svc.SetConn("http://foo/bar") _, err = ToConnData("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 { if err == nil {
t.Fatalf("expected error") t.Fatalf("expected error")
} }

View File

@ -12,7 +12,6 @@ import (
"git.defalsify.org/vise.git/logging" "git.defalsify.org/vise.git/logging"
"git.defalsify.org/vise.git/persist" "git.defalsify.org/vise.git/persist"
"git.defalsify.org/vise.git/resource" "git.defalsify.org/vise.git/resource"
"git.grassecon.net/urdt/ussd/initializers"
gdbmstorage "git.grassecon.net/urdt/ussd/internal/storage/db/gdbm" gdbmstorage "git.grassecon.net/urdt/ussd/internal/storage/db/gdbm"
) )
@ -28,42 +27,22 @@ type StorageService interface {
} }
type MenuStorageService struct { type MenuStorageService struct {
conn connData conn ConnData
resourceDir string resourceDir string
resourceStore db.Db resourceStore db.Db
stateStore db.Db stateStore db.Db
userDataStore db.Db userDataStore db.Db
} }
func buildConnStr() string {
host := initializers.GetEnv("DB_HOST", "localhost")
user := initializers.GetEnv("DB_USER", "postgres")
password := initializers.GetEnv("DB_PASSWORD", "")
dbName := initializers.GetEnv("DB_NAME", "")
port := initializers.GetEnv("DB_PORT", "5432")
connString := fmt.Sprintf(
"postgres://%s:%s@%s:%s/%s",
user, password, host, port, dbName,
)
logg.Debugf("pg conn string", "conn", connString)
return connString
}
func NewMenuStorageService(resourceDir string) *MenuStorageService { func NewMenuStorageService(resourceDir string) *MenuStorageService {
return &MenuStorageService{ return &MenuStorageService{
resourceDir: resourceDir, resourceDir: resourceDir,
} }
} }
func (ms *MenuStorageService) SetConn(connStr string) error { func (ms *MenuStorageService) WithConn(conn ConnData) *MenuStorageService {
o, err := toConnData(connStr) ms.conn = conn
if err != nil { return ms
return err
}
ms.conn = o
return nil
} }
func (ms *MenuStorageService) getOrCreateDb(ctx context.Context, existingDb db.Db, section string) (db.Db, error) { func (ms *MenuStorageService) getOrCreateDb(ctx context.Context, existingDb db.Db, section string) (db.Db, error) {

View File

@ -44,10 +44,15 @@ func TestEngine(sessionId string) (engine.Engine, func(), chan bool) {
fmt.Fprintf(os.Stderr, "connstr err: %v", err) fmt.Fprintf(os.Stderr, "connstr err: %v", err)
os.Exit(1) os.Exit(1)
} }
conn, err := storage.ToConnData(connStr)
if err != nil {
fmt.Fprintf(os.Stderr, "connstr parse err: %v", err)
os.Exit(1)
}
resourceDir := scriptDir resourceDir := scriptDir
menuStorageService := storage.NewMenuStorageService(resourceDir) menuStorageService := storage.NewMenuStorageService(resourceDir)
err = menuStorageService.SetConn(connStr) menuStorageService.WithConn(conn)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "conn error: %v", err) fmt.Fprintf(os.Stderr, "conn error: %v", err)
os.Exit(1) os.Exit(1)