From dc1674ec551c9ba24a16886d7076a3a91eefdd64 Mon Sep 17 00:00:00 2001 From: lash Date: Sat, 4 Jan 2025 08:40:43 +0000 Subject: [PATCH 01/17] WIP add connection string parser --- internal/storage/parse.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 internal/storage/parse.go diff --git a/internal/storage/parse.go b/internal/storage/parse.go new file mode 100644 index 0000000..0745a72 --- /dev/null +++ b/internal/storage/parse.go @@ -0,0 +1,21 @@ +package storage + +const ( + DBTYPE_MEM = iota + DBTYPE_GDBM + DBTYPE_POSTGRES +) + +type connData struct { + typ int + str string +} + +func toConnData(s string) connData { + var o connData + + if s == "" { + return o + } + return o +} -- 2.45.2 From 2e30739ec9223d2028c558c4b56e1bf03394bec3 Mon Sep 17 00:00:00 2001 From: lash Date: Sat, 4 Jan 2025 09:37:12 +0000 Subject: [PATCH 02/17] Implement connstr --- cmd/africastalking/main.go | 3 +- cmd/async/main.go | 31 +++++++++++----- cmd/http/main.go | 23 ++++++++---- cmd/main.go | 22 ++++++++--- common/storage.go | 14 ++++--- devtools/gen/main.go | 24 ++++++++++-- devtools/store/main.go | 22 +++++++++-- internal/storage/parse.go | 59 ++++++++++++++++++++++++++++-- internal/storage/parse_test.go | 37 +++++++++++++++++++ internal/storage/storageservice.go | 51 ++++++++++++++++---------- internal/testutil/TestEngine.go | 6 ++- 11 files changed, 227 insertions(+), 65 deletions(-) create mode 100644 internal/storage/parse_test.go diff --git a/cmd/africastalking/main.go b/cmd/africastalking/main.go index 2696b51..11c6512 100644 --- a/cmd/africastalking/main.go +++ b/cmd/africastalking/main.go @@ -15,11 +15,10 @@ import ( "git.defalsify.org/vise.git/logging" "git.defalsify.org/vise.git/resource" - "git.grassecon.net/urdt/ussd/common" "git.grassecon.net/urdt/ussd/config" "git.grassecon.net/urdt/ussd/initializers" "git.grassecon.net/urdt/ussd/internal/handlers" - httpserver "git.grassecon.net/urdt/ussd/internal/http/at" + at "git.grassecon.net/urdt/ussd/internal/http/at" "git.grassecon.net/urdt/ussd/internal/storage" "git.grassecon.net/urdt/ussd/remote" ) diff --git a/cmd/async/main.go b/cmd/async/main.go index 9cd04b3..a9a0b1e 100644 --- a/cmd/async/main.go +++ b/cmd/async/main.go @@ -7,6 +7,7 @@ import ( "os" "os/signal" "path" + "path/filepath" "syscall" "git.defalsify.org/vise.git/engine" @@ -47,24 +48,33 @@ func main() { config.LoadConfig() var sessionId string - var dbDir string + var connStr string var resourceDir string var size uint var database string var engineDebug bool var host string var port uint + var err error + flag.StringVar(&sessionId, "session-id", "075xx2123", "session id") - flag.StringVar(&dbDir, "dbdir", ".state", "database dir to read from") flag.StringVar(&resourceDir, "resourcedir", path.Join("services", "registration"), "resource dir") - flag.StringVar(&database, "db", "gdbm", "database to be used") + flag.StringVar(&connStr, "c", ".", "connection string") flag.BoolVar(&engineDebug, "d", false, "use engine debug output") flag.UintVar(&size, "s", 160, "max size of output") flag.StringVar(&host, "h", initializers.GetEnv("HOST", "127.0.0.1"), "http host") flag.UintVar(&port, "p", initializers.GetEnvUint("PORT", 7123), "http port") flag.Parse() - logg.Infof("start command", "dbdir", dbDir, "resourcedir", resourceDir, "outputsize", size, "sessionId", sessionId) + if connStr == "." { + connStr, err = filepath.Abs(".state/state.gdbm") + if err != nil { + fmt.Fprintf(os.Stderr, "auto connstr generate error: %v", err) + os.Exit(1) + } + } + + logg.Infof("start command", "connstr", connStr, "resourcedir", resourceDir, "outputsize", size, "sessionId", sessionId) ctx := context.Background() ctx = context.WithValue(ctx, "Database", database) @@ -81,18 +91,19 @@ func main() { cfg.EngineDebug = true } - menuStorageService := storage.NewMenuStorageService(dbDir, resourceDir) + menuStorageService := storage.NewMenuStorageService(resourceDir) + err = menuStorageService.SetConn(connStr) + if err != nil { + fmt.Fprintf(os.Stderr, err.Error()) + os.Exit(1) + } + rs, err := menuStorageService.GetResource(ctx) if err != nil { fmt.Fprintf(os.Stderr, err.Error()) os.Exit(1) } - err = menuStorageService.EnsureDbDir() - if err != nil { - fmt.Fprintf(os.Stderr, err.Error()) - os.Exit(1) - } userdataStore, err := menuStorageService.GetUserdataDb(ctx) if err != nil { diff --git a/cmd/http/main.go b/cmd/http/main.go index 6ddfded..dec4e64 100644 --- a/cmd/http/main.go +++ b/cmd/http/main.go @@ -8,6 +8,7 @@ import ( "os" "os/signal" "path" + "path/filepath" "strconv" "syscall" @@ -36,23 +37,31 @@ func init() { func main() { config.LoadConfig() - var dbDir string + var connStr string var resourceDir string var size uint var database string var engineDebug bool var host string var port uint - flag.StringVar(&dbDir, "dbdir", ".state", "database dir to read from") + var err error + flag.StringVar(&resourceDir, "resourcedir", path.Join("services", "registration"), "resource dir") - flag.StringVar(&database, "db", "gdbm", "database to be used") flag.BoolVar(&engineDebug, "d", false, "use engine debug output") flag.UintVar(&size, "s", 160, "max size of output") flag.StringVar(&host, "h", initializers.GetEnv("HOST", "127.0.0.1"), "http host") flag.UintVar(&port, "p", initializers.GetEnvUint("PORT", 7123), "http port") flag.Parse() - logg.Infof("start command", "dbdir", dbDir, "resourcedir", resourceDir, "outputsize", size) + if connStr == "." { + connStr, err = filepath.Abs(".state/state.gdbm") + if err != nil { + fmt.Fprintf(os.Stderr, "auto connstr generate error: %v", err) + os.Exit(1) + } + } + + logg.Infof("start command", "connstr", connStr, "resourcedir", resourceDir, "outputsize", size) ctx := context.Background() ctx = context.WithValue(ctx, "Database", database) @@ -69,14 +78,14 @@ func main() { cfg.EngineDebug = true } - menuStorageService := storage.NewMenuStorageService(dbDir, resourceDir) - rs, err := menuStorageService.GetResource(ctx) + menuStorageService := storage.NewMenuStorageService(resourceDir) + err = menuStorageService.SetConn(connStr) if err != nil { fmt.Fprintf(os.Stderr, err.Error()) os.Exit(1) } - err = menuStorageService.EnsureDbDir() + rs, err := menuStorageService.GetResource(ctx) if err != nil { fmt.Fprintf(os.Stderr, err.Error()) os.Exit(1) diff --git a/cmd/main.go b/cmd/main.go index 4fd084f..c45615b 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -6,6 +6,7 @@ import ( "fmt" "os" "path" + "path/filepath" "git.defalsify.org/vise.git/engine" "git.defalsify.org/vise.git/logging" @@ -30,19 +31,28 @@ func init() { func main() { config.LoadConfig() - var dbDir string + var connStr string var size uint var sessionId string var database string var engineDebug bool + var err error + flag.StringVar(&sessionId, "session-id", "075xx2123", "session id") - flag.StringVar(&database, "db", "gdbm", "database to be used") - flag.StringVar(&dbDir, "dbdir", ".state", "database dir to read from") + flag.StringVar(&connStr, "c", ".", "connection string") flag.BoolVar(&engineDebug, "d", false, "use engine debug output") flag.UintVar(&size, "s", 160, "max size of output") flag.Parse() - logg.Infof("start command", "dbdir", dbDir, "outputsize", size) + if connStr == "." { + connStr, err = filepath.Abs(".state/state.gdbm") + if err != nil { + fmt.Fprintf(os.Stderr, "auto connstr generate error: %v", err) + os.Exit(1) + } + } + + logg.Infof("start command", "connstr", connStr, "outputsize", size) ctx := context.Background() ctx = context.WithValue(ctx, "SessionId", sessionId) @@ -58,9 +68,9 @@ func main() { } resourceDir := scriptDir - menuStorageService := storage.NewMenuStorageService(dbDir, resourceDir) + menuStorageService := storage.NewMenuStorageService(resourceDir) - err := menuStorageService.EnsureDbDir() + err = menuStorageService.SetConn(connStr) if err != nil { fmt.Fprintf(os.Stderr, err.Error()) os.Exit(1) diff --git a/common/storage.go b/common/storage.go index d37bce3..c8a4ff2 100644 --- a/common/storage.go +++ b/common/storage.go @@ -23,17 +23,19 @@ type StorageServices interface { GetPersister(ctx context.Context) (*persist.Persister, error) GetUserdataDb(ctx context.Context) (db.Db, error) GetResource(ctx context.Context) (resource.Resource, error) - EnsureDbDir() error + SetConn(connStr string) error } type StorageService struct { svc *storage.MenuStorageService } -func NewStorageService(dbDir string) *StorageService { - return &StorageService{ - svc: storage.NewMenuStorageService(dbDir, ""), +func NewStorageService(connStr string) (*StorageService, error) { + svc := &StorageService{ + svc: storage.NewMenuStorageService(""), } + err := svc.SetConn(connStr) + return svc, err } func(ss *StorageService) GetPersister(ctx context.Context) (*persist.Persister, error) { @@ -48,6 +50,6 @@ func(ss *StorageService) GetResource(ctx context.Context) (resource.Resource, er return nil, errors.New("not implemented") } -func(ss *StorageService) EnsureDbDir() error { - return ss.svc.EnsureDbDir() +func(ss *StorageService) SetConn(connStr string) error { + return ss.svc.SetConn(connStr) } diff --git a/devtools/gen/main.go b/devtools/gen/main.go index b9e2aed..448cc16 100644 --- a/devtools/gen/main.go +++ b/devtools/gen/main.go @@ -7,6 +7,7 @@ import ( "fmt" "os" "path" + "path/filepath" "git.defalsify.org/vise.git/logging" "git.grassecon.net/urdt/ussd/config" @@ -28,23 +29,38 @@ func init() { func main() { config.LoadConfig() - var dbDir string + var connStr string var sessionId string var database string var engineDebug bool + var err error flag.StringVar(&sessionId, "session-id", "075xx2123", "session id") - flag.StringVar(&database, "db", "gdbm", "database to be used") - flag.StringVar(&dbDir, "dbdir", ".state", "database dir to read from") + flag.StringVar(&connStr, "c", ".", "connection string") flag.BoolVar(&engineDebug, "d", false, "use engine debug output") flag.Parse() + if connStr == "." { + connStr, err = filepath.Abs(".state/state.gdbm") + if err != nil { + fmt.Fprintf(os.Stderr, "auto connstr generate error: %v", err) + os.Exit(1) + } + } + + logg.Infof("start command", "connstr", connStr) + ctx := context.Background() ctx = context.WithValue(ctx, "SessionId", sessionId) ctx = context.WithValue(ctx, "Database", database) resourceDir := scriptDir - menuStorageService := storage.NewMenuStorageService(dbDir, resourceDir) + menuStorageService := storage.NewMenuStorageService(resourceDir) + err = menuStorageService.SetConn(connStr) + if err != nil { + fmt.Fprintf(os.Stderr, "connection string error: %v", err) + os.Exit(1) + } store, err := menuStorageService.GetUserdataDb(ctx) if err != nil { diff --git a/devtools/store/main.go b/devtools/store/main.go index 8bd4d16..ed54ba3 100644 --- a/devtools/store/main.go +++ b/devtools/store/main.go @@ -6,6 +6,7 @@ import ( "fmt" "os" "path" + "path/filepath" "git.grassecon.net/urdt/ussd/config" "git.grassecon.net/urdt/ussd/initializers" @@ -28,23 +29,36 @@ func init() { func main() { config.LoadConfig() - var dbDir string + var connStr string var sessionId string var database string var engineDebug bool + var err error flag.StringVar(&sessionId, "session-id", "075xx2123", "session id") - flag.StringVar(&database, "db", "gdbm", "database to be used") - flag.StringVar(&dbDir, "dbdir", ".state", "database dir to read from") + flag.StringVar(&connStr, "c", ".state", "connection string") flag.BoolVar(&engineDebug, "d", false, "use engine debug output") flag.Parse() + if connStr == "." { + connStr, err = filepath.Abs(".state/state.gdbm") + if err != nil { + fmt.Fprintf(os.Stderr, "auto connstr generate error: %v", err) + os.Exit(1) + } + } + ctx := context.Background() ctx = context.WithValue(ctx, "SessionId", sessionId) ctx = context.WithValue(ctx, "Database", database) resourceDir := scriptDir - menuStorageService := storage.NewMenuStorageService(dbDir, resourceDir) + menuStorageService := storage.NewMenuStorageService(resourceDir) + err = menuStorageService.SetConn(connStr) + if err != nil { + fmt.Fprintf(os.Stderr, "connection string error: %v", err) + os.Exit(1) + } store, err := menuStorageService.GetUserdataDb(ctx) if err != nil { diff --git a/internal/storage/parse.go b/internal/storage/parse.go index 0745a72..d4bc564 100644 --- a/internal/storage/parse.go +++ b/internal/storage/parse.go @@ -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) } diff --git a/internal/storage/parse_test.go b/internal/storage/parse_test.go new file mode 100644 index 0000000..d4b8ecb --- /dev/null +++ b/internal/storage/parse_test.go @@ -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") + } +} diff --git a/internal/storage/storageservice.go b/internal/storage/storageservice.go index 04e75ce..6a42bac 100644 --- a/internal/storage/storageservice.go +++ b/internal/storage/storageservice.go @@ -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) } diff --git a/internal/testutil/TestEngine.go b/internal/testutil/TestEngine.go index 3fcb307..63e9e84 100644 --- a/internal/testutil/TestEngine.go +++ b/internal/testutil/TestEngine.go @@ -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) -- 2.45.2 From c3cbe1cd927df9c94faa73d8a6417a71d18986bb Mon Sep 17 00:00:00 2001 From: lash Date: Sat, 4 Jan 2025 09:41:17 +0000 Subject: [PATCH 03/17] Add connstr to last executable --- cmd/africastalking/main.go | 24 +++++++++++++++++------- cmd/http/main.go | 1 + 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/cmd/africastalking/main.go b/cmd/africastalking/main.go index cb83a3a..e1035e9 100644 --- a/cmd/africastalking/main.go +++ b/cmd/africastalking/main.go @@ -8,6 +8,7 @@ import ( "os" "os/signal" "path" + "path/filepath" "strconv" "syscall" @@ -36,23 +37,32 @@ func init() { func main() { config.LoadConfig() - var dbDir string + var connStr string var resourceDir string var size uint var database string var engineDebug bool var host string var port uint - flag.StringVar(&dbDir, "dbdir", ".state", "database dir to read from") + var err error + flag.StringVar(&resourceDir, "resourcedir", path.Join("services", "registration"), "resource dir") - flag.StringVar(&database, "db", "gdbm", "database to be used") + flag.StringVar(&connStr, "c", ".", "connection string") flag.BoolVar(&engineDebug, "d", false, "use engine debug output") flag.UintVar(&size, "s", 160, "max size of output") flag.StringVar(&host, "h", initializers.GetEnv("HOST", "127.0.0.1"), "http host") flag.UintVar(&port, "p", initializers.GetEnvUint("PORT", 7123), "http port") flag.Parse() - logg.Infof("start command", "build", build, "dbdir", dbDir, "resourcedir", resourceDir, "outputsize", size) + if connStr == "." { + connStr, err = filepath.Abs(".state/state.gdbm") + if err != nil { + fmt.Fprintf(os.Stderr, "auto connstr generate error: %v", err) + os.Exit(1) + } + } + + logg.Infof("start command", "build", build, "dbdir", connStr, "resourcedir", resourceDir, "outputsize", size) ctx := context.Background() ctx = context.WithValue(ctx, "Database", database) @@ -69,14 +79,14 @@ func main() { cfg.EngineDebug = true } - menuStorageService := storage.NewMenuStorageService(dbDir, resourceDir) - rs, err := menuStorageService.GetResource(ctx) + menuStorageService := storage.NewMenuStorageService(resourceDir) + err = menuStorageService.SetConn(connStr) if err != nil { fmt.Fprintf(os.Stderr, err.Error()) os.Exit(1) } - err = menuStorageService.EnsureDbDir() + rs, err := menuStorageService.GetResource(ctx) if err != nil { fmt.Fprintf(os.Stderr, err.Error()) os.Exit(1) diff --git a/cmd/http/main.go b/cmd/http/main.go index dec4e64..070cf79 100644 --- a/cmd/http/main.go +++ b/cmd/http/main.go @@ -47,6 +47,7 @@ func main() { var err error flag.StringVar(&resourceDir, "resourcedir", path.Join("services", "registration"), "resource dir") + flag.StringVar(&connStr, "c", ".", "connection string") flag.BoolVar(&engineDebug, "d", false, "use engine debug output") flag.UintVar(&size, "s", 160, "max size of output") flag.StringVar(&host, "h", initializers.GetEnv("HOST", "127.0.0.1"), "http host") -- 2.45.2 From dc61d055844fffa71cab6c96893436f14360fa68 Mon Sep 17 00:00:00 2001 From: lash Date: Sat, 4 Jan 2025 12:16:28 +0000 Subject: [PATCH 04/17] WIP revert connstr gdbm to dir only, add file as table spec --- internal/storage/storageservice.go | 8 +++++-- .../testutil/{TestEngine.go => engine.go} | 22 +++++++++++-------- internal/testutil/engine_test.go | 15 +++++++++++++ menutraversal_test/menu_traversal_test.go | 11 +++++++--- 4 files changed, 42 insertions(+), 14 deletions(-) rename internal/testutil/{TestEngine.go => engine.go} (82%) create mode 100644 internal/testutil/engine_test.go diff --git a/internal/storage/storageservice.go b/internal/storage/storageservice.go index 6a42bac..e14b7a4 100644 --- a/internal/storage/storageservice.go +++ b/internal/storage/storageservice.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "os" + "path" "git.defalsify.org/vise.git/db" fsdb "git.defalsify.org/vise.git/db/fs" @@ -66,7 +67,7 @@ func (ms *MenuStorageService) SetConn(connStr string) error { return nil } -func (ms *MenuStorageService) getOrCreateDb(ctx context.Context, existingDb db.Db, fileName string) (db.Db, error) { +func (ms *MenuStorageService) getOrCreateDb(ctx context.Context, existingDb db.Db, section string) (db.Db, error) { var newDb db.Db var err error // database, ok := ctx.Value("Database").(string) @@ -79,6 +80,7 @@ func (ms *MenuStorageService) getOrCreateDb(ctx context.Context, existingDb db.D } + connStr := ms.conn.String() dbTyp := ms.conn.DbType() if dbTyp == DBTYPE_POSTGRES { newDb = postgres.NewPgDb() @@ -87,11 +89,13 @@ func (ms *MenuStorageService) getOrCreateDb(ctx context.Context, existingDb db.D if err != nil { return nil, err } + connStr = path.Join(connStr, section) newDb = gdbmstorage.NewThreadGdbmDb() } else { return nil, fmt.Errorf("unsupported connection string: %s", ms.conn.String()) } - err = newDb.Connect(ctx, ms.conn.String()) + logg.DebugCtxf(ctx, "connecting to db", "conn", connStr) + err = newDb.Connect(ctx, connStr) if err != nil { return nil, err } diff --git a/internal/testutil/TestEngine.go b/internal/testutil/engine.go similarity index 82% rename from internal/testutil/TestEngine.go rename to internal/testutil/engine.go index 63e9e84..4b02e32 100644 --- a/internal/testutil/TestEngine.go +++ b/internal/testutil/engine.go @@ -5,6 +5,7 @@ import ( "fmt" "os" "path" + "path/filepath" "time" "git.defalsify.org/vise.git/engine" @@ -27,7 +28,6 @@ var ( func TestEngine(sessionId string) (engine.Engine, func(), chan bool) { ctx := context.Background() ctx = context.WithValue(ctx, "SessionId", sessionId) - ctx = context.WithValue(ctx, "Database", "gdbm") pfp := path.Join(scriptDir, "pp.csv") var eventChannel = make(chan bool) @@ -39,39 +39,43 @@ func TestEngine(sessionId string) (engine.Engine, func(), chan bool) { FlagCount: uint32(128), } - dbDir := ".test_state" + connStr, err := filepath.Abs(".test_state/state.gdbm") + if err != nil { + fmt.Fprintf(os.Stderr, "connstr err: %v", err) + os.Exit(1) + } resourceDir := scriptDir - //menuStorageService := storage.NewMenuStorageService(dbDir, resourceDir) + //menuStorageService := storage.NewMenuStorageService(connStr, resourceDir) menuStorageService := storage.NewMenuStorageService(resourceDir) //err := menuStorageService.EnsureDbDir() - err := menuStorageService.SetConn(dbDir) + err = menuStorageService.SetConn(connStr) if err != nil { - fmt.Fprintf(os.Stderr, err.Error()) + fmt.Fprintf(os.Stderr, "conn error: %v", err) os.Exit(1) } rs, err := menuStorageService.GetResource(ctx) if err != nil { - fmt.Fprintf(os.Stderr, err.Error()) + fmt.Fprintf(os.Stderr, "resource error: %v", err) os.Exit(1) } pe, err := menuStorageService.GetPersister(ctx) if err != nil { - fmt.Fprintf(os.Stderr, err.Error()) + fmt.Fprintf(os.Stderr, "persister error: %v", err) os.Exit(1) } userDataStore, err := menuStorageService.GetUserdataDb(ctx) if err != nil { - fmt.Fprintf(os.Stderr, err.Error()) + fmt.Fprintf(os.Stderr, "userdb error: %v", err) os.Exit(1) } dbResource, ok := rs.(*resource.DbResource) if !ok { - fmt.Fprintf(os.Stderr, err.Error()) + fmt.Fprintf(os.Stderr, "dbresource cast error") os.Exit(1) } diff --git a/internal/testutil/engine_test.go b/internal/testutil/engine_test.go new file mode 100644 index 0000000..f747468 --- /dev/null +++ b/internal/testutil/engine_test.go @@ -0,0 +1,15 @@ +package testutil + +import ( + "testing" +) + +func TestCreateEngine(t *testing.T) { + o, clean, eventC := TestEngine("foo") + defer clean() + defer func() { + <-eventC + close(eventC) + }() + _ = o +} diff --git a/menutraversal_test/menu_traversal_test.go b/menutraversal_test/menu_traversal_test.go index 6b6b3da..52e2273 100644 --- a/menutraversal_test/menu_traversal_test.go +++ b/menutraversal_test/menu_traversal_test.go @@ -7,6 +7,7 @@ import ( "log" "math/rand" "os" + "path/filepath" "regexp" "testing" @@ -17,7 +18,6 @@ import ( var ( testData = driver.ReadData() - testStore = ".test_state" sessionID string src = rand.NewSource(42) g = rand.New(src) @@ -25,6 +25,11 @@ var ( var groupTestFile = flag.String("test-file", "group_test.json", "The test file to use for running the group tests") +func testStore() string { + v, _ := filepath.Abs(".test_state/state.gdbm") + return v +} + func GenerateSessionId() string { uu := uuid.NewGenWithOptions(uuid.WithRandomReader(g)) v, err := uu.NewV4() @@ -81,8 +86,8 @@ func extractSendAmount(response []byte) string { func TestMain(m *testing.M) { sessionID = GenerateSessionId() defer func() { - if err := os.RemoveAll(testStore); err != nil { - log.Fatalf("Failed to delete state store %s: %v", testStore, err) + if err := os.RemoveAll(testStore()); err != nil { + log.Fatalf("Failed to delete state store %s: %v", testStore(), err) } }() m.Run() -- 2.45.2 From 2992f7ae8e9983c51aeb1328197270db395b5b1a Mon Sep 17 00:00:00 2001 From: lash Date: Sat, 4 Jan 2025 13:19:30 +0000 Subject: [PATCH 05/17] Update executables with new conn str --- cmd/africastalking/main.go | 7 ++++--- cmd/async/main.go | 6 +++--- cmd/http/main.go | 6 +++--- cmd/main.go | 6 +++--- internal/storage/parse.go | 3 --- internal/storage/storageservice.go | 1 - 6 files changed, 13 insertions(+), 16 deletions(-) diff --git a/cmd/africastalking/main.go b/cmd/africastalking/main.go index e1035e9..8820215 100644 --- a/cmd/africastalking/main.go +++ b/cmd/africastalking/main.go @@ -34,6 +34,7 @@ var ( func init() { initializers.LoadEnvVariables() } + func main() { config.LoadConfig() @@ -47,15 +48,15 @@ func main() { var err error flag.StringVar(&resourceDir, "resourcedir", path.Join("services", "registration"), "resource dir") - flag.StringVar(&connStr, "c", ".", "connection string") + flag.StringVar(&connStr, "c", ".state", "connection string") flag.BoolVar(&engineDebug, "d", false, "use engine debug output") flag.UintVar(&size, "s", 160, "max size of output") flag.StringVar(&host, "h", initializers.GetEnv("HOST", "127.0.0.1"), "http host") flag.UintVar(&port, "p", initializers.GetEnvUint("PORT", 7123), "http port") flag.Parse() - if connStr == "." { - connStr, err = filepath.Abs(".state/state.gdbm") + if connStr == ".state" { + connStr, err = filepath.Abs(connStr) if err != nil { fmt.Fprintf(os.Stderr, "auto connstr generate error: %v", err) os.Exit(1) diff --git a/cmd/async/main.go b/cmd/async/main.go index a9a0b1e..8b15031 100644 --- a/cmd/async/main.go +++ b/cmd/async/main.go @@ -59,15 +59,15 @@ func main() { flag.StringVar(&sessionId, "session-id", "075xx2123", "session id") flag.StringVar(&resourceDir, "resourcedir", path.Join("services", "registration"), "resource dir") - flag.StringVar(&connStr, "c", ".", "connection string") + flag.StringVar(&connStr, "c", ".state", "connection string") flag.BoolVar(&engineDebug, "d", false, "use engine debug output") flag.UintVar(&size, "s", 160, "max size of output") flag.StringVar(&host, "h", initializers.GetEnv("HOST", "127.0.0.1"), "http host") flag.UintVar(&port, "p", initializers.GetEnvUint("PORT", 7123), "http port") flag.Parse() - if connStr == "." { - connStr, err = filepath.Abs(".state/state.gdbm") + if connStr == ".state" { + connStr, err = filepath.Abs(connStr) if err != nil { fmt.Fprintf(os.Stderr, "auto connstr generate error: %v", err) os.Exit(1) diff --git a/cmd/http/main.go b/cmd/http/main.go index 070cf79..f42d4d9 100644 --- a/cmd/http/main.go +++ b/cmd/http/main.go @@ -47,15 +47,15 @@ func main() { var err error flag.StringVar(&resourceDir, "resourcedir", path.Join("services", "registration"), "resource dir") - flag.StringVar(&connStr, "c", ".", "connection string") + flag.StringVar(&connStr, "c", ".state", "connection string") flag.BoolVar(&engineDebug, "d", false, "use engine debug output") flag.UintVar(&size, "s", 160, "max size of output") flag.StringVar(&host, "h", initializers.GetEnv("HOST", "127.0.0.1"), "http host") flag.UintVar(&port, "p", initializers.GetEnvUint("PORT", 7123), "http port") flag.Parse() - if connStr == "." { - connStr, err = filepath.Abs(".state/state.gdbm") + if connStr == ".state" { + connStr, err = filepath.Abs(connStr) if err != nil { fmt.Fprintf(os.Stderr, "auto connstr generate error: %v", err) os.Exit(1) diff --git a/cmd/main.go b/cmd/main.go index c45615b..2506012 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -39,13 +39,13 @@ func main() { var err error flag.StringVar(&sessionId, "session-id", "075xx2123", "session id") - flag.StringVar(&connStr, "c", ".", "connection string") + flag.StringVar(&connStr, "c", ".state", "connection string") flag.BoolVar(&engineDebug, "d", false, "use engine debug output") flag.UintVar(&size, "s", 160, "max size of output") flag.Parse() - if connStr == "." { - connStr, err = filepath.Abs(".state/state.gdbm") + if connStr == ".state" { + connStr, err = filepath.Abs(connStr) if err != nil { fmt.Fprintf(os.Stderr, "auto connstr generate error: %v", err) os.Exit(1) diff --git a/internal/storage/parse.go b/internal/storage/parse.go index d4bc564..a97b938 100644 --- a/internal/storage/parse.go +++ b/internal/storage/parse.go @@ -40,9 +40,6 @@ 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 } diff --git a/internal/storage/storageservice.go b/internal/storage/storageservice.go index e14b7a4..68e6916 100644 --- a/internal/storage/storageservice.go +++ b/internal/storage/storageservice.go @@ -28,7 +28,6 @@ type StorageService interface { } type MenuStorageService struct { - //dbDir string conn connData resourceDir string resourceStore db.Db -- 2.45.2 From a4d6cef9c0fa3e077b6691d4ced7085a7e7f52c0 Mon Sep 17 00:00:00 2001 From: lash Date: Sat, 4 Jan 2025 13:21:31 +0000 Subject: [PATCH 06/17] Remove commented code --- internal/storage/storageservice.go | 4 ---- internal/testutil/engine.go | 2 -- 2 files changed, 6 deletions(-) diff --git a/internal/storage/storageservice.go b/internal/storage/storageservice.go index 68e6916..c9c138f 100644 --- a/internal/storage/storageservice.go +++ b/internal/storage/storageservice.go @@ -69,10 +69,6 @@ func (ms *MenuStorageService) SetConn(connStr string) error { func (ms *MenuStorageService) getOrCreateDb(ctx context.Context, existingDb db.Db, section 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 diff --git a/internal/testutil/engine.go b/internal/testutil/engine.go index 4b02e32..4234773 100644 --- a/internal/testutil/engine.go +++ b/internal/testutil/engine.go @@ -45,10 +45,8 @@ func TestEngine(sessionId string) (engine.Engine, func(), chan bool) { os.Exit(1) } resourceDir := scriptDir - //menuStorageService := storage.NewMenuStorageService(connStr, resourceDir) menuStorageService := storage.NewMenuStorageService(resourceDir) - //err := menuStorageService.EnsureDbDir() err = menuStorageService.SetConn(connStr) if err != nil { fmt.Fprintf(os.Stderr, "conn error: %v", err) -- 2.45.2 From f61e65f4fe38380978fc7961a86ece26c50c2eac Mon Sep 17 00:00:00 2001 From: lash Date: Sat, 4 Jan 2025 13:24:14 +0000 Subject: [PATCH 07/17] Rename accountservice testservice source file --- .../testservice/{TestAccountService.go => accountservice.go} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename internal/testutil/testservice/{TestAccountService.go => accountservice.go} (100%) diff --git a/internal/testutil/testservice/TestAccountService.go b/internal/testutil/testservice/accountservice.go similarity index 100% rename from internal/testutil/testservice/TestAccountService.go rename to internal/testutil/testservice/accountservice.go -- 2.45.2 From 450dfa02cc016b525d743a864530581e0372acda Mon Sep 17 00:00:00 2001 From: lash Date: Sat, 4 Jan 2025 20:36:18 +0000 Subject: [PATCH 08/17] Refactor to use conndata as menustorageservice conn arg --- cmd/africastalking/main.go | 16 ++++++++-------- cmd/async/main.go | 20 ++++++++++---------- cmd/http/main.go | 24 ++++++++++-------------- cmd/main.go | 23 +++++++++-------------- common/storage.go | 12 ++++++------ config/config.go | 15 +++++++++++++-- devtools/gen/main.go | 26 +++++++++++--------------- devtools/store/main.go | 16 ++++++++-------- internal/storage/parse.go | 10 +++++----- internal/storage/parse_test.go | 25 ++++++++----------------- internal/storage/storageservice.go | 29 ++++------------------------- internal/testutil/engine.go | 7 ++++++- 12 files changed, 98 insertions(+), 125 deletions(-) diff --git a/cmd/africastalking/main.go b/cmd/africastalking/main.go index 8820215..6035fc6 100644 --- a/cmd/africastalking/main.go +++ b/cmd/africastalking/main.go @@ -8,7 +8,6 @@ import ( "os" "os/signal" "path" - "path/filepath" "strconv" "syscall" @@ -55,12 +54,13 @@ func main() { flag.UintVar(&port, "p", initializers.GetEnvUint("PORT", 7123), "http port") flag.Parse() - if connStr == ".state" { - connStr, err = filepath.Abs(connStr) - if err != nil { - fmt.Fprintf(os.Stderr, "auto connstr generate error: %v", err) - os.Exit(1) - } + if connStr != "" { + connStr = config.DbConn + } + connData, err := storage.ToConnData(config.DbConn) + 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) @@ -81,7 +81,7 @@ func main() { } menuStorageService := storage.NewMenuStorageService(resourceDir) - err = menuStorageService.SetConn(connStr) + menuStorageService = menuStorageService.WithConn(connData) if err != nil { fmt.Fprintf(os.Stderr, err.Error()) os.Exit(1) diff --git a/cmd/async/main.go b/cmd/async/main.go index 8b15031..17ea2a1 100644 --- a/cmd/async/main.go +++ b/cmd/async/main.go @@ -7,7 +7,6 @@ import ( "os" "os/signal" "path" - "path/filepath" "syscall" "git.defalsify.org/vise.git/engine" @@ -47,8 +46,8 @@ func (p *asyncRequestParser) GetInput(r any) ([]byte, error) { func main() { config.LoadConfig() - var sessionId string var connStr string + var sessionId string var resourceDir string var size uint var database string @@ -59,19 +58,20 @@ func main() { flag.StringVar(&sessionId, "session-id", "075xx2123", "session id") 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.UintVar(&size, "s", 160, "max size of output") flag.StringVar(&host, "h", initializers.GetEnv("HOST", "127.0.0.1"), "http host") flag.UintVar(&port, "p", initializers.GetEnvUint("PORT", 7123), "http port") flag.Parse() - if connStr == ".state" { - connStr, err = filepath.Abs(connStr) - if err != nil { - fmt.Fprintf(os.Stderr, "auto connstr generate error: %v", err) - os.Exit(1) - } + if connStr != "" { + connStr = config.DbConn + } + connData, err := storage.ToConnData(config.DbConn) + 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) @@ -92,7 +92,7 @@ func main() { } menuStorageService := storage.NewMenuStorageService(resourceDir) - err = menuStorageService.SetConn(connStr) + menuStorageService = menuStorageService.WithConn(connData) if err != nil { fmt.Fprintf(os.Stderr, err.Error()) os.Exit(1) diff --git a/cmd/http/main.go b/cmd/http/main.go index f42d4d9..69a9c90 100644 --- a/cmd/http/main.go +++ b/cmd/http/main.go @@ -8,7 +8,6 @@ import ( "os" "os/signal" "path" - "path/filepath" "strconv" "syscall" @@ -47,19 +46,20 @@ func main() { var err error 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.UintVar(&size, "s", 160, "max size of output") flag.StringVar(&host, "h", initializers.GetEnv("HOST", "127.0.0.1"), "http host") flag.UintVar(&port, "p", initializers.GetEnvUint("PORT", 7123), "http port") flag.Parse() - if connStr == ".state" { - connStr, err = filepath.Abs(connStr) - if err != nil { - fmt.Fprintf(os.Stderr, "auto connstr generate error: %v", err) - os.Exit(1) - } + if connStr != "" { + connStr = config.DbConn + } + connData, err := storage.ToConnData(config.DbConn) + if err != nil { + fmt.Fprintf(os.Stderr, "connstr err: %v", err) + os.Exit(1) } logg.Infof("start command", "connstr", connStr, "resourcedir", resourceDir, "outputsize", size) @@ -80,12 +80,8 @@ func main() { } menuStorageService := storage.NewMenuStorageService(resourceDir) - err = menuStorageService.SetConn(connStr) - if err != nil { - fmt.Fprintf(os.Stderr, err.Error()) - os.Exit(1) - } - + menuStorageService = menuStorageService.WithConn(connData) + rs, err := menuStorageService.GetResource(ctx) if err != nil { fmt.Fprintf(os.Stderr, err.Error()) diff --git a/cmd/main.go b/cmd/main.go index 2506012..a475dbe 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -6,7 +6,6 @@ import ( "fmt" "os" "path" - "path/filepath" "git.defalsify.org/vise.git/engine" "git.defalsify.org/vise.git/logging" @@ -39,17 +38,18 @@ func main() { var err error 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.UintVar(&size, "s", 160, "max size of output") flag.Parse() - if connStr == ".state" { - connStr, err = filepath.Abs(connStr) - if err != nil { - fmt.Fprintf(os.Stderr, "auto connstr generate error: %v", err) - os.Exit(1) - } + if connStr != "" { + connStr = config.DbConn + } + connData, err := storage.ToConnData(config.DbConn) + if err != nil { + fmt.Fprintf(os.Stderr, "connstr err: %v", err) + os.Exit(1) } logg.Infof("start command", "connstr", connStr, "outputsize", size) @@ -69,12 +69,7 @@ func main() { resourceDir := scriptDir menuStorageService := storage.NewMenuStorageService(resourceDir) - - err = menuStorageService.SetConn(connStr) - if err != nil { - fmt.Fprintf(os.Stderr, err.Error()) - os.Exit(1) - } + menuStorageService = menuStorageService.WithConn(connData) rs, err := menuStorageService.GetResource(ctx) if err != nil { diff --git a/common/storage.go b/common/storage.go index c8a4ff2..6ab5211 100644 --- a/common/storage.go +++ b/common/storage.go @@ -23,19 +23,19 @@ type StorageServices interface { GetPersister(ctx context.Context) (*persist.Persister, error) GetUserdataDb(ctx context.Context) (db.Db, error) GetResource(ctx context.Context) (resource.Resource, error) - SetConn(connStr string) error + SetConn(conn storage.ConnData) } type StorageService struct { svc *storage.MenuStorageService } -func NewStorageService(connStr string) (*StorageService, error) { +func NewStorageService(conn storage.ConnData) (*StorageService, error) { svc := &StorageService{ svc: storage.NewMenuStorageService(""), } - err := svc.SetConn(connStr) - return svc, err + svc.SetConn(conn) + return svc, nil } 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") } -func(ss *StorageService) SetConn(connStr string) error { - return ss.svc.SetConn(connStr) +func(ss *StorageService) SetConn(conn storage.ConnData) { + ss.svc = ss.svc.WithConn(conn) } diff --git a/config/config.go b/config/config.go index 3a8e8ed..6f2b225 100644 --- a/config/config.go +++ b/config/config.go @@ -34,6 +34,7 @@ var ( VoucherTransfersURL string VoucherDataURL string CheckAliasURL string + DbConn string ) func setBase() error { @@ -43,14 +44,20 @@ func setBase() error { dataURLBase = initializers.GetEnv("DATA_URL_BASE", "http://localhost:5006") BearerToken = initializers.GetEnv("BEARER_TOKEN", "") - _, err = url.JoinPath(custodialURLBase, "/foo") + _, err = url.Parse(custodialURLBase) if err != nil { return err } - _, err = url.JoinPath(dataURLBase, "/bar") + _, err = url.Parse(dataURLBase) if err != nil { return err } + + return nil +} + +func setConn() error { + DbConn = initializers.GetEnv("DB_CONN", "") return nil } @@ -60,6 +67,10 @@ func LoadConfig() error { if err != nil { return err } + err = setConn() + if err != nil { + return err + } CreateAccountURL, _ = url.JoinPath(custodialURLBase, createAccountPath) TrackStatusURL, _ = url.JoinPath(custodialURLBase, trackStatusPath) BalanceURL, _ = url.JoinPath(custodialURLBase, balancePathPrefix) diff --git a/devtools/gen/main.go b/devtools/gen/main.go index 448cc16..272fd9c 100644 --- a/devtools/gen/main.go +++ b/devtools/gen/main.go @@ -7,7 +7,6 @@ import ( "fmt" "os" "path" - "path/filepath" "git.defalsify.org/vise.git/logging" "git.grassecon.net/urdt/ussd/config" @@ -36,19 +35,20 @@ func main() { var err error 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.Parse() - if connStr == "." { - connStr, err = filepath.Abs(".state/state.gdbm") - if err != nil { - fmt.Fprintf(os.Stderr, "auto connstr generate error: %v", err) - os.Exit(1) - } + if connStr != "" { + connStr = config.DbConn + } + connData, err := storage.ToConnData(config.DbConn) + 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.WithValue(ctx, "SessionId", sessionId) @@ -56,12 +56,8 @@ func main() { resourceDir := scriptDir menuStorageService := storage.NewMenuStorageService(resourceDir) - err = menuStorageService.SetConn(connStr) - if err != nil { - fmt.Fprintf(os.Stderr, "connection string error: %v", err) - os.Exit(1) - } - + menuStorageService = menuStorageService.WithConn(connData) + store, err := menuStorageService.GetUserdataDb(ctx) if err != nil { fmt.Fprintf(os.Stderr, err.Error()) diff --git a/devtools/store/main.go b/devtools/store/main.go index ed54ba3..da988ad 100644 --- a/devtools/store/main.go +++ b/devtools/store/main.go @@ -6,7 +6,6 @@ import ( "fmt" "os" "path" - "path/filepath" "git.grassecon.net/urdt/ussd/config" "git.grassecon.net/urdt/ussd/initializers" @@ -40,12 +39,13 @@ func main() { flag.BoolVar(&engineDebug, "d", false, "use engine debug output") flag.Parse() - if connStr == "." { - connStr, err = filepath.Abs(".state/state.gdbm") - if err != nil { - fmt.Fprintf(os.Stderr, "auto connstr generate error: %v", err) - os.Exit(1) - } + if connStr != "" { + connStr = config.DbConn + } + connData, err := storage.ToConnData(config.DbConn) + if err != nil { + fmt.Fprintf(os.Stderr, "connstr err: %v", err) + os.Exit(1) } ctx := context.Background() @@ -54,7 +54,7 @@ func main() { resourceDir := scriptDir 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) diff --git a/internal/storage/parse.go b/internal/storage/parse.go index a97b938..bb25627 100644 --- a/internal/storage/parse.go +++ b/internal/storage/parse.go @@ -12,16 +12,16 @@ const ( DBTYPE_POSTGRES ) -type connData struct { +type ConnData struct { typ int str string } -func (cd *connData) DbType() int { +func (cd *ConnData) DbType() int { return cd.typ } -func (cd *connData) String() string { +func (cd *ConnData) String() string { return cd.str } @@ -44,8 +44,8 @@ func probeGdbm(s string) (string, bool) { return s, true } -func toConnData(connStr string) (connData, error) { - var o connData +func ToConnData(connStr string) (ConnData, error) { + var o ConnData if connStr == "" { return o, nil diff --git a/internal/storage/parse_test.go b/internal/storage/parse_test.go index d4b8ecb..e18e57c 100644 --- a/internal/storage/parse_test.go +++ b/internal/storage/parse_test.go @@ -5,32 +5,23 @@ import ( ) func TestParseConnStr(t *testing.T) { - svc := NewMenuStorageService("") - err := svc.SetConn("postgres://foo:bar@localhost:5432/baz") + _, err := ToConnData("postgres://foo:bar@localhost:5432/baz") if err != nil { t.Fatal(err) } - err = svc.SetConn("/foo/bar/baz.gdbm") + _, err = ToConnData("/foo/bar") if err != nil { 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 { 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") + _, err = ToConnData("http://foo/bar") if err == nil { t.Fatalf("expected error") } diff --git a/internal/storage/storageservice.go b/internal/storage/storageservice.go index c9c138f..e3429bc 100644 --- a/internal/storage/storageservice.go +++ b/internal/storage/storageservice.go @@ -12,7 +12,6 @@ import ( "git.defalsify.org/vise.git/logging" "git.defalsify.org/vise.git/persist" "git.defalsify.org/vise.git/resource" - "git.grassecon.net/urdt/ussd/initializers" gdbmstorage "git.grassecon.net/urdt/ussd/internal/storage/db/gdbm" ) @@ -28,42 +27,22 @@ type StorageService interface { } type MenuStorageService struct { - conn connData + conn ConnData resourceDir string resourceStore db.Db stateStore 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 { return &MenuStorageService{ resourceDir: resourceDir, } } -func (ms *MenuStorageService) SetConn(connStr string) error { - o, err := toConnData(connStr) - if err != nil { - return err - } - ms.conn = o - return nil +func (ms *MenuStorageService) WithConn(conn ConnData) *MenuStorageService { + ms.conn = conn + return ms } func (ms *MenuStorageService) getOrCreateDb(ctx context.Context, existingDb db.Db, section string) (db.Db, error) { diff --git a/internal/testutil/engine.go b/internal/testutil/engine.go index 4234773..3d804ae 100644 --- a/internal/testutil/engine.go +++ b/internal/testutil/engine.go @@ -44,10 +44,15 @@ func TestEngine(sessionId string) (engine.Engine, func(), chan bool) { fmt.Fprintf(os.Stderr, "connstr err: %v", err) 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 menuStorageService := storage.NewMenuStorageService(resourceDir) - err = menuStorageService.SetConn(connStr) + menuStorageService.WithConn(conn) if err != nil { fmt.Fprintf(os.Stderr, "conn error: %v", err) os.Exit(1) -- 2.45.2 From 89c21847b959d304c364f657249dee2c01c4b281 Mon Sep 17 00:00:00 2001 From: lash Date: Sat, 4 Jan 2025 20:52:49 +0000 Subject: [PATCH 09/17] Improve error message --- internal/storage/storageservice.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/storage/storageservice.go b/internal/storage/storageservice.go index e3429bc..24d5eff 100644 --- a/internal/storage/storageservice.go +++ b/internal/storage/storageservice.go @@ -66,7 +66,7 @@ func (ms *MenuStorageService) getOrCreateDb(ctx context.Context, existingDb db.D connStr = path.Join(connStr, section) newDb = gdbmstorage.NewThreadGdbmDb() } else { - return nil, fmt.Errorf("unsupported connection string: %s", ms.conn.String()) + return nil, fmt.Errorf("unsupported connection string: '%s'\n", ms.conn.String()) } logg.DebugCtxf(ctx, "connecting to db", "conn", connStr) err = newDb.Connect(ctx, connStr) -- 2.45.2 From 3a9f3fa373c7feb4295ad3e52896d1ba7389269e Mon Sep 17 00:00:00 2001 From: lash Date: Sat, 4 Jan 2025 20:54:51 +0000 Subject: [PATCH 10/17] Update env example --- .env.example | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/.env.example b/.env.example index c636fa8..0a43945 100644 --- a/.env.example +++ b/.env.example @@ -6,13 +6,8 @@ HOST=127.0.0.1 AT_ENDPOINT=/ussd/africastalking #PostgreSQL -DB_HOST=localhost -DB_USER=postgres -DB_PASSWORD=strongpass -DB_NAME=urdt_ussd -DB_PORT=5432 -DB_SSLMODE=disable -DB_TIMEZONE=Africa/Nairobi +DB_CONN=postgres://postgres:strongpass@localhost:5432/urdt_ussd +#DB_TIMEZONE=Africa/Nairobi #External API Calls CUSTODIAL_URL_BASE=http://localhost:5003 -- 2.45.2 From cc9760125aa9e47f1939fc90a6b50b0a2e8e1376 Mon Sep 17 00:00:00 2001 From: lash Date: Sat, 4 Jan 2025 21:02:08 +0000 Subject: [PATCH 11/17] Remove unnecessary connection chain step --- .env.example | 1 + cmd/africastalking/main.go | 3 +-- cmd/async/main.go | 3 +-- cmd/http/main.go | 3 +-- cmd/main.go | 3 +-- common/storage.go | 7 +------ devtools/gen/main.go | 3 +-- devtools/store/main.go | 7 +------ internal/storage/storageservice.go | 8 ++------ internal/testutil/engine.go | 8 +------- 10 files changed, 11 insertions(+), 35 deletions(-) diff --git a/.env.example b/.env.example index 0a43945..dae37bc 100644 --- a/.env.example +++ b/.env.example @@ -8,6 +8,7 @@ AT_ENDPOINT=/ussd/africastalking #PostgreSQL DB_CONN=postgres://postgres:strongpass@localhost:5432/urdt_ussd #DB_TIMEZONE=Africa/Nairobi +#DB_SCHEMA=vise #External API Calls CUSTODIAL_URL_BASE=http://localhost:5003 diff --git a/cmd/africastalking/main.go b/cmd/africastalking/main.go index 6035fc6..0d2b8ec 100644 --- a/cmd/africastalking/main.go +++ b/cmd/africastalking/main.go @@ -80,8 +80,7 @@ func main() { cfg.EngineDebug = true } - menuStorageService := storage.NewMenuStorageService(resourceDir) - menuStorageService = menuStorageService.WithConn(connData) + menuStorageService := storage.NewMenuStorageService(connData, resourceDir) if err != nil { fmt.Fprintf(os.Stderr, err.Error()) os.Exit(1) diff --git a/cmd/async/main.go b/cmd/async/main.go index 17ea2a1..65e681d 100644 --- a/cmd/async/main.go +++ b/cmd/async/main.go @@ -91,8 +91,7 @@ func main() { cfg.EngineDebug = true } - menuStorageService := storage.NewMenuStorageService(resourceDir) - menuStorageService = menuStorageService.WithConn(connData) + menuStorageService := storage.NewMenuStorageService(connData, resourceDir) if err != nil { fmt.Fprintf(os.Stderr, err.Error()) os.Exit(1) diff --git a/cmd/http/main.go b/cmd/http/main.go index 69a9c90..2f043e6 100644 --- a/cmd/http/main.go +++ b/cmd/http/main.go @@ -79,8 +79,7 @@ func main() { cfg.EngineDebug = true } - menuStorageService := storage.NewMenuStorageService(resourceDir) - menuStorageService = menuStorageService.WithConn(connData) + menuStorageService := storage.NewMenuStorageService(connData, resourceDir) rs, err := menuStorageService.GetResource(ctx) if err != nil { diff --git a/cmd/main.go b/cmd/main.go index a475dbe..ad4535b 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -68,8 +68,7 @@ func main() { } resourceDir := scriptDir - menuStorageService := storage.NewMenuStorageService(resourceDir) - menuStorageService = menuStorageService.WithConn(connData) + menuStorageService := storage.NewMenuStorageService(connData, resourceDir) rs, err := menuStorageService.GetResource(ctx) if err != nil { diff --git a/common/storage.go b/common/storage.go index 6ab5211..c7e478b 100644 --- a/common/storage.go +++ b/common/storage.go @@ -32,9 +32,8 @@ type StorageService struct { func NewStorageService(conn storage.ConnData) (*StorageService, error) { svc := &StorageService{ - svc: storage.NewMenuStorageService(""), + svc: storage.NewMenuStorageService(conn, ""), } - svc.SetConn(conn) return svc, nil } @@ -49,7 +48,3 @@ func(ss *StorageService) GetUserdataDb(ctx context.Context) (db.Db, error) { func(ss *StorageService) GetResource(ctx context.Context) (resource.Resource, error) { return nil, errors.New("not implemented") } - -func(ss *StorageService) SetConn(conn storage.ConnData) { - ss.svc = ss.svc.WithConn(conn) -} diff --git a/devtools/gen/main.go b/devtools/gen/main.go index 272fd9c..749f340 100644 --- a/devtools/gen/main.go +++ b/devtools/gen/main.go @@ -55,8 +55,7 @@ func main() { ctx = context.WithValue(ctx, "Database", database) resourceDir := scriptDir - menuStorageService := storage.NewMenuStorageService(resourceDir) - menuStorageService = menuStorageService.WithConn(connData) + menuStorageService := storage.NewMenuStorageService(connData, resourceDir) store, err := menuStorageService.GetUserdataDb(ctx) if err != nil { diff --git a/devtools/store/main.go b/devtools/store/main.go index da988ad..f7f76c6 100644 --- a/devtools/store/main.go +++ b/devtools/store/main.go @@ -53,12 +53,7 @@ func main() { ctx = context.WithValue(ctx, "Database", database) resourceDir := scriptDir - menuStorageService := storage.NewMenuStorageService(resourceDir) - menuStorageService = menuStorageService.WithConn(connData) - if err != nil { - fmt.Fprintf(os.Stderr, "connection string error: %v", err) - os.Exit(1) - } + menuStorageService := storage.NewMenuStorageService(connData, resourceDir) store, err := menuStorageService.GetUserdataDb(ctx) if err != nil { diff --git a/internal/storage/storageservice.go b/internal/storage/storageservice.go index 24d5eff..9148d7c 100644 --- a/internal/storage/storageservice.go +++ b/internal/storage/storageservice.go @@ -34,17 +34,13 @@ type MenuStorageService struct { userDataStore db.Db } -func NewMenuStorageService(resourceDir string) *MenuStorageService { +func NewMenuStorageService(conn ConnData, resourceDir string) *MenuStorageService { return &MenuStorageService{ + conn: conn, resourceDir: resourceDir, } } -func (ms *MenuStorageService) WithConn(conn ConnData) *MenuStorageService { - ms.conn = conn - return ms -} - func (ms *MenuStorageService) getOrCreateDb(ctx context.Context, existingDb db.Db, section string) (db.Db, error) { var newDb db.Db var err error diff --git a/internal/testutil/engine.go b/internal/testutil/engine.go index 3d804ae..2372ce9 100644 --- a/internal/testutil/engine.go +++ b/internal/testutil/engine.go @@ -50,13 +50,7 @@ func TestEngine(sessionId string) (engine.Engine, func(), chan bool) { os.Exit(1) } resourceDir := scriptDir - menuStorageService := storage.NewMenuStorageService(resourceDir) - - menuStorageService.WithConn(conn) - if err != nil { - fmt.Fprintf(os.Stderr, "conn error: %v", err) - os.Exit(1) - } + menuStorageService := storage.NewMenuStorageService(conn, resourceDir) rs, err := menuStorageService.GetResource(ctx) if err != nil { -- 2.45.2 From 51b6fc0dde431b6489e5ceb26a6e0cc2c66c6b51 Mon Sep 17 00:00:00 2001 From: lash Date: Sat, 4 Jan 2025 21:13:39 +0000 Subject: [PATCH 12/17] Remove unused methods in storage interfaces, improve logs --- cmd/africastalking/main.go | 4 ++-- cmd/async/main.go | 2 +- cmd/http/main.go | 2 +- cmd/main.go | 2 +- common/storage.go | 1 - devtools/store/main.go | 2 ++ internal/storage/storageservice.go | 1 - 7 files changed, 7 insertions(+), 7 deletions(-) diff --git a/cmd/africastalking/main.go b/cmd/africastalking/main.go index 0d2b8ec..40c3609 100644 --- a/cmd/africastalking/main.go +++ b/cmd/africastalking/main.go @@ -47,7 +47,7 @@ func main() { var err error 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.UintVar(&size, "s", 160, "max size of output") flag.StringVar(&host, "h", initializers.GetEnv("HOST", "127.0.0.1"), "http host") @@ -63,7 +63,7 @@ func main() { os.Exit(1) } - logg.Infof("start command", "build", build, "dbdir", connStr, "resourcedir", resourceDir, "outputsize", size) + logg.Infof("start command", "build", build, "conn", connData, "resourcedir", resourceDir, "outputsize", size) ctx := context.Background() ctx = context.WithValue(ctx, "Database", database) diff --git a/cmd/async/main.go b/cmd/async/main.go index 65e681d..b0c7caa 100644 --- a/cmd/async/main.go +++ b/cmd/async/main.go @@ -74,7 +74,7 @@ func main() { os.Exit(1) } - logg.Infof("start command", "connstr", connStr, "resourcedir", resourceDir, "outputsize", size, "sessionId", sessionId) + logg.Infof("start command", "conn", connData, "resourcedir", resourceDir, "outputsize", size, "sessionId", sessionId) ctx := context.Background() ctx = context.WithValue(ctx, "Database", database) diff --git a/cmd/http/main.go b/cmd/http/main.go index 2f043e6..d744afc 100644 --- a/cmd/http/main.go +++ b/cmd/http/main.go @@ -62,7 +62,7 @@ func main() { os.Exit(1) } - logg.Infof("start command", "connstr", connStr, "resourcedir", resourceDir, "outputsize", size) + logg.Infof("start command", "conn", connData, "resourcedir", resourceDir, "outputsize", size) ctx := context.Background() ctx = context.WithValue(ctx, "Database", database) diff --git a/cmd/main.go b/cmd/main.go index ad4535b..ac680fc 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -52,7 +52,7 @@ func main() { os.Exit(1) } - logg.Infof("start command", "connstr", connStr, "outputsize", size) + logg.Infof("start command", "conn", connData, "outputsize", size) ctx := context.Background() ctx = context.WithValue(ctx, "SessionId", sessionId) diff --git a/common/storage.go b/common/storage.go index c7e478b..2960578 100644 --- a/common/storage.go +++ b/common/storage.go @@ -23,7 +23,6 @@ type StorageServices interface { GetPersister(ctx context.Context) (*persist.Persister, error) GetUserdataDb(ctx context.Context) (db.Db, error) GetResource(ctx context.Context) (resource.Resource, error) - SetConn(conn storage.ConnData) } type StorageService struct { diff --git a/devtools/store/main.go b/devtools/store/main.go index f7f76c6..6915f74 100644 --- a/devtools/store/main.go +++ b/devtools/store/main.go @@ -48,6 +48,8 @@ func main() { os.Exit(1) } + logg.Infof("start command", "conn", connData) + ctx := context.Background() ctx = context.WithValue(ctx, "SessionId", sessionId) ctx = context.WithValue(ctx, "Database", database) diff --git a/internal/storage/storageservice.go b/internal/storage/storageservice.go index 9148d7c..83ce051 100644 --- a/internal/storage/storageservice.go +++ b/internal/storage/storageservice.go @@ -23,7 +23,6 @@ type StorageService interface { GetPersister(ctx context.Context) (*persist.Persister, error) GetUserdataDb(ctx context.Context) db.Db GetResource(ctx context.Context) (resource.Resource, error) - SetConn(connStr string) error } type MenuStorageService struct { -- 2.45.2 From d39740a09abd7dc8a7ba092d946cfcebcb7e0c9f Mon Sep 17 00:00:00 2001 From: lash Date: Mon, 6 Jan 2025 07:41:24 +0000 Subject: [PATCH 13/17] Edit ssh cli help text --- cmd/ssh/main.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/ssh/main.go b/cmd/ssh/main.go index 0227616..0b22216 100644 --- a/cmd/ssh/main.go +++ b/cmd/ssh/main.go @@ -37,8 +37,8 @@ func main() { flag.BoolVar(&engineDebug, "engine-debug", false, "use engine debug output") flag.BoolVar(&stateDebug, "state-debug", false, "use engine debug output") flag.UintVar(&size, "s", 160, "max size of output") - flag.StringVar(&host, "h", "127.0.0.1", "http host") - flag.UintVar(&port, "p", 7122, "http port") + flag.StringVar(&host, "h", "127.0.0.1", "socket host") + flag.UintVar(&port, "p", 7122, "socket port") flag.Parse() sshKeyFile := flag.Arg(0) -- 2.45.2 From 52fd1eced24fc499e5b9bf04151df7a78973f62f Mon Sep 17 00:00:00 2001 From: lash Date: Mon, 6 Jan 2025 08:11:37 +0000 Subject: [PATCH 14/17] Enable env, config, db in ssh --- cmd/main.go | 3 ++- cmd/ssh/main.go | 28 +++++++++++++++++++++++----- internal/ssh/ssh.go | 35 +++++++++++++++-------------------- 3 files changed, 40 insertions(+), 26 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index ac680fc..01a352c 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -35,8 +35,10 @@ func main() { var sessionId string var database string var engineDebug bool + var resourceDir string var err error + flag.StringVar(&resourceDir, "resourcedir", scriptDir, "resource dir") flag.StringVar(&sessionId, "session-id", "075xx2123", "session id") flag.StringVar(&connStr, "c", "", "connection string") flag.BoolVar(&engineDebug, "d", false, "use engine debug output") @@ -67,7 +69,6 @@ func main() { MenuSeparator: menuSeparator, } - resourceDir := scriptDir menuStorageService := storage.NewMenuStorageService(connData, resourceDir) rs, err := menuStorageService.GetResource(ctx) diff --git a/cmd/ssh/main.go b/cmd/ssh/main.go index 0b22216..58f2b0b 100644 --- a/cmd/ssh/main.go +++ b/cmd/ssh/main.go @@ -14,7 +14,10 @@ import ( "git.defalsify.org/vise.git/engine" "git.defalsify.org/vise.git/logging" + "git.grassecon.net/urdt/ussd/config" + "git.grassecon.net/urdt/ussd/initializers" "git.grassecon.net/urdt/ussd/internal/ssh" + "git.grassecon.net/urdt/ussd/internal/storage" ) var ( @@ -24,7 +27,14 @@ var ( scriptDir = path.Join("services", "registration") ) +func init() { + initializers.LoadEnvVariables() +} + func main() { + config.LoadConfig() + + var connStr string var dbDir string var resourceDir string var size uint @@ -32,17 +42,25 @@ func main() { var stateDebug bool var host string var port uint - flag.StringVar(&dbDir, "dbdir", ".state", "database dir to read from") + flag.StringVar(&connStr, "c", "", "connection string") flag.StringVar(&resourceDir, "resourcedir", path.Join("services", "registration"), "resource dir") - flag.BoolVar(&engineDebug, "engine-debug", false, "use engine debug output") - flag.BoolVar(&stateDebug, "state-debug", false, "use engine debug output") + flag.BoolVar(&engineDebug, "d", false, "use engine debug output") flag.UintVar(&size, "s", 160, "max size of output") flag.StringVar(&host, "h", "127.0.0.1", "socket host") flag.UintVar(&port, "p", 7122, "socket port") flag.Parse() + if connStr != "" { + connStr = config.DbConn + } + connData, err := storage.ToConnData(config.DbConn) + if err != nil { + fmt.Fprintf(os.Stderr, "connstr err: %v", err) + os.Exit(1) + } + sshKeyFile := flag.Arg(0) - _, err := os.Stat(sshKeyFile) + _, err = os.Stat(sshKeyFile) if err != nil { fmt.Fprintf(os.Stderr, "cannot open ssh server private key file: %v\n", err) os.Exit(1) @@ -93,7 +111,7 @@ func main() { Cfg: cfg, Debug: engineDebug, FlagFile: pfp, - DbDir: dbDir, + Conn: connData, ResourceDir: resourceDir, SrvKeyFile: sshKeyFile, Host: host, diff --git a/internal/ssh/ssh.go b/internal/ssh/ssh.go index 1831ebe..c3ec725 100644 --- a/internal/ssh/ssh.go +++ b/internal/ssh/ssh.go @@ -71,6 +71,20 @@ func(a *auther) Get(k []byte) (string, error) { return v, nil } +type SshRunner struct { + Ctx context.Context + Cfg engine.Config + FlagFile string + Conn storage.ConnData + ResourceDir string + Debug bool + SrvKeyFile string + Host string + Port uint + wg sync.WaitGroup + lst net.Listener +} + func(s *SshRunner) serve(ctx context.Context, sessionId string, ch ssh.NewChannel, en engine.Engine) error { if ch == nil { return errors.New("nil channel") @@ -128,32 +142,13 @@ func(s *SshRunner) serve(ctx context.Context, sessionId string, ch ssh.NewChanne return nil } -type SshRunner struct { - Ctx context.Context - Cfg engine.Config - FlagFile string - DbDir string - ResourceDir string - Debug bool - SrvKeyFile string - Host string - Port uint - wg sync.WaitGroup - lst net.Listener -} - func(s *SshRunner) Stop() error { return s.lst.Close() } func(s *SshRunner) GetEngine(sessionId string) (engine.Engine, func(), error) { ctx := s.Ctx - menuStorageService := storage.NewMenuStorageService(s.DbDir, s.ResourceDir) - - err := menuStorageService.EnsureDbDir() - if err != nil { - return nil, nil, err - } + menuStorageService := storage.NewMenuStorageService(s.Conn, s.ResourceDir) rs, err := menuStorageService.GetResource(ctx) if err != nil { -- 2.45.2 From 599815c343e958234b88a2075670e368ad89a597 Mon Sep 17 00:00:00 2001 From: lash Date: Mon, 6 Jan 2025 09:00:41 +0000 Subject: [PATCH 15/17] Fix remaining conflict in cmd cli --- cmd/main.go | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index 8547994..4bf134b 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -88,22 +88,12 @@ func main() { MenuSeparator: menuSeparator, } -<<<<<<< HEAD menuStorageService := storage.NewMenuStorageService(connData, resourceDir) -======= - resourceDir := scriptDir - menuStorageService := storage.NewMenuStorageService(dbDir, resourceDir) + if gettextDir != "" { menuStorageService = menuStorageService.WithGettext(gettextDir, langs.Langs()) } - err = menuStorageService.EnsureDbDir() - if err != nil { - fmt.Fprintf(os.Stderr, err.Error()) - os.Exit(1) - } ->>>>>>> master - rs, err := menuStorageService.GetResource(ctx) if err != nil { fmt.Fprintf(os.Stderr, err.Error()) -- 2.45.2 From 1fa830f2866d9b39b96d4ea593b7aad99653bab4 Mon Sep 17 00:00:00 2001 From: lash Date: Tue, 7 Jan 2025 13:51:26 +0000 Subject: [PATCH 16/17] Add auth conn string to ssh, use connstr for execs --- cmd/africastalking/main.go | 2 +- cmd/async/main.go | 2 +- cmd/http/main.go | 2 +- cmd/main.go | 2 +- cmd/ssh/main.go | 19 ++++++++++++++----- internal/ssh/ssh.go | 1 + 6 files changed, 19 insertions(+), 9 deletions(-) diff --git a/cmd/africastalking/main.go b/cmd/africastalking/main.go index e0d05f6..72d3944 100644 --- a/cmd/africastalking/main.go +++ b/cmd/africastalking/main.go @@ -63,7 +63,7 @@ func main() { if connStr != "" { connStr = config.DbConn } - connData, err := storage.ToConnData(config.DbConn) + connData, err := storage.ToConnData(connStr) if err != nil { fmt.Fprintf(os.Stderr, "connstr err: %v", err) os.Exit(1) diff --git a/cmd/async/main.go b/cmd/async/main.go index e63c469..dc293e6 100644 --- a/cmd/async/main.go +++ b/cmd/async/main.go @@ -74,7 +74,7 @@ func main() { if connStr != "" { connStr = config.DbConn } - connData, err := storage.ToConnData(config.DbConn) + connData, err := storage.ToConnData(connStr) if err != nil { fmt.Fprintf(os.Stderr, "connstr err: %v", err) os.Exit(1) diff --git a/cmd/http/main.go b/cmd/http/main.go index c61d68d..8e65232 100644 --- a/cmd/http/main.go +++ b/cmd/http/main.go @@ -62,7 +62,7 @@ func main() { if connStr != "" { connStr = config.DbConn } - connData, err := storage.ToConnData(config.DbConn) + connData, err := storage.ToConnData(connStr) if err != nil { fmt.Fprintf(os.Stderr, "connstr err: %v", err) os.Exit(1) diff --git a/cmd/main.go b/cmd/main.go index 4bf134b..3939c9d 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -55,7 +55,7 @@ func main() { if connStr != "" { connStr = config.DbConn } - connData, err := storage.ToConnData(config.DbConn) + connData, err := storage.ToConnData(connStr) if err != nil { fmt.Fprintf(os.Stderr, "connstr err: %v", err) os.Exit(1) diff --git a/cmd/ssh/main.go b/cmd/ssh/main.go index e55d793..51023e5 100644 --- a/cmd/ssh/main.go +++ b/cmd/ssh/main.go @@ -37,7 +37,7 @@ func main() { config.LoadConfig() var connStr string - var dbDir string + var authConnStr string var resourceDir string var size uint var engineDebug bool @@ -45,6 +45,7 @@ func main() { var host string var port uint flag.StringVar(&connStr, "c", "", "connection string") + flag.StringVar(&authConnStr, "authdb", "", "auth connection string") flag.StringVar(&resourceDir, "resourcedir", path.Join("services", "registration"), "resource dir") flag.BoolVar(&engineDebug, "d", false, "use engine debug output") flag.UintVar(&size, "s", 160, "max size of output") @@ -52,14 +53,22 @@ func main() { flag.UintVar(&port, "p", 7122, "socket port") flag.Parse() - if connStr != "" { + if connStr == "" { connStr = config.DbConn } - connData, err := storage.ToConnData(config.DbConn) + if authConnStr == "" { + authConnStr = connStr + } + connData, err := storage.ToConnData(connStr) if err != nil { fmt.Fprintf(os.Stderr, "connstr err: %v", err) os.Exit(1) } + authConnData, err := storage.ToConnData(authConnStr) + if err != nil { + fmt.Fprintf(os.Stderr, "auth connstr err: %v", err) + os.Exit(1) + } sshKeyFile := flag.Arg(0) _, err = os.Stat(sshKeyFile) @@ -75,7 +84,7 @@ func main() { logg.WarnCtxf(ctx, "!!!!! Do not expose to internet and only use with tunnel!") logg.WarnCtxf(ctx, "!!!!! (See ssh -L <...>)") - logg.Infof("start command", "dbdir", dbDir, "resourcedir", resourceDir, "outputsize", size, "keyfile", sshKeyFile, "host", host, "port", port) + logg.Infof("start command", "conn", connData, "authconn", authConnData, "resourcedir", resourceDir, "outputsize", size, "keyfile", sshKeyFile, "host", host, "port", port) pfp := path.Join(scriptDir, "pp.csv") @@ -91,7 +100,7 @@ func main() { cfg.EngineDebug = true } - authKeyStore, err := ssh.NewSshKeyStore(ctx, dbDir) + authKeyStore, err := ssh.NewSshKeyStore(ctx, authConnData.String()) if err != nil { fmt.Fprintf(os.Stderr, "keystore file open error: %v", err) os.Exit(1) diff --git a/internal/ssh/ssh.go b/internal/ssh/ssh.go index c3ec725..4b744a8 100644 --- a/internal/ssh/ssh.go +++ b/internal/ssh/ssh.go @@ -41,6 +41,7 @@ func NewAuther(ctx context.Context, keyStore *SshKeyStore) *auther { } func(a *auther) Check(conn ssh.ConnMetadata, pubKey ssh.PublicKey) (*ssh.Permissions, error) { + logg.TraceCtxf(a.Ctx, "looking for publickey", "pubkey", fmt.Sprintf("%x", pubKey)) va, err := a.keyStore.Get(a.Ctx, pubKey) if err != nil { return nil, err -- 2.45.2 From 721f80d0f268e76ae4942de245804a5743886187 Mon Sep 17 00:00:00 2001 From: lash Date: Wed, 8 Jan 2025 07:34:15 +0000 Subject: [PATCH 17/17] Repalce missing context --- internal/ssh/ssh.go | 1 + 1 file changed, 1 insertion(+) diff --git a/internal/ssh/ssh.go b/internal/ssh/ssh.go index 4b744a8..8209187 100644 --- a/internal/ssh/ssh.go +++ b/internal/ssh/ssh.go @@ -204,6 +204,7 @@ func(s *SshRunner) GetEngine(sessionId string) (engine.Engine, func(), error) { // adapted example from crypto/ssh package, NewServerConn doc func(s *SshRunner) Run(ctx context.Context, keyStore *SshKeyStore) { + s.Ctx = ctx running := true // TODO: waitgroup should probably not be global -- 2.45.2