From f441047fa1f3527dc16e7e9f64451fa6877129c5 Mon Sep 17 00:00:00 2001 From: lash Date: Sun, 3 Nov 2024 23:08:50 +0000 Subject: [PATCH] Implement storageservice in nats subscription object --- cmd/main.go | 25 ++++++++++++++++++++----- event/nats/nats.go | 7 ++----- event/nats/nats_test.go | 6 ++++-- event/route.go | 14 ++++++++++---- internal/testutil/service.go | 30 ++++++++++++++++++++++++++++++ 5 files changed, 66 insertions(+), 16 deletions(-) create mode 100644 internal/testutil/service.go diff --git a/cmd/main.go b/cmd/main.go index 850dc7d..242c112 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -2,13 +2,14 @@ package main import ( "context" + "flag" "fmt" "os" "os/signal" "syscall" - "git.defalsify.org/vise.git/db/mem" "git.grassecon.net/urdt/ussd/initializers" + "git.grassecon.net/urdt/ussd/common" "git.grassecon.net/term/config" "git.grassecon.net/term/event/nats" ) @@ -18,14 +19,28 @@ func init() { } func main() { + config.LoadConfig() + + var dbDir string + flag.StringVar(&dbDir, "dbdir", ".state", "database dir to read from") + flag.Parse() + ctx := context.Background() - db := mem.NewMemDb() - err := db.Connect(ctx, "") +// db := mem.NewMemDb() +// err := db.Connect(ctx, "") +// if err != nil { +// fmt.Fprintf(os.Stderr, "Db connect err: %v", err) +// os.Exit(1) +// } + + menuStorageService := common.NewStorageService(dbDir) + err := menuStorageService.EnsureDbDir() if err != nil { - fmt.Fprintf(os.Stderr, "Db connect err: %v", err) + fmt.Fprintf(os.Stderr, err.Error()) os.Exit(1) } - n := nats.NewNatsSubscription(db) + + n := nats.NewNatsSubscription(menuStorageService) err = n.Connect(ctx, config.JetstreamURL) if err != nil { fmt.Fprintf(os.Stderr, "Stream connect err: %v", err) diff --git a/event/nats/nats.go b/event/nats/nats.go index 223b8da..5ca9b91 100644 --- a/event/nats/nats.go +++ b/event/nats/nats.go @@ -9,7 +9,6 @@ import ( "github.com/nats-io/nats.go/jetstream" geEvent "github.com/grassrootseconomics/eth-tracker/pkg/event" "git.defalsify.org/vise.git/logging" - "git.defalsify.org/vise.git/db" "git.grassecon.net/urdt/ussd/common" "git.grassecon.net/term/event" "git.grassecon.net/term/config" @@ -32,12 +31,10 @@ type NatsSubscription struct { } // NewNatsSubscription creates a new NatsSubscription with the given user store. -func NewNatsSubscription(store db.Db) *NatsSubscription { +func NewNatsSubscription(store common.StorageServices) *NatsSubscription { return &NatsSubscription{ Router: event.Router{ - Store: &common.UserDataStore{ - Db: store, - }, + Store: store, }, } } diff --git a/event/nats/nats_test.go b/event/nats/nats_test.go index 766c45b..1629150 100644 --- a/event/nats/nats_test.go +++ b/event/nats/nats_test.go @@ -141,8 +141,10 @@ func TestHandleMsg(t *testing.T) { t.Fatal(err) } - - sub := NewNatsSubscription(userDb) + storageService := &testutil.TestStorageService{ + Store: userDb, + } + sub := NewNatsSubscription(storageService) data := fmt.Sprintf(`{ "block": %d, diff --git a/event/route.go b/event/route.go index 2266388..d99bdb2 100644 --- a/event/route.go +++ b/event/route.go @@ -16,8 +16,7 @@ var ( // Router is responsible for invoking handlers corresponding to events. type Router struct { - // User data store abstraction over application data. - Store *common.UserDataStore + Store common.StorageServices } // Route parses an event from the event stream, and resolves the handler @@ -27,13 +26,20 @@ type Router struct { // handler fails to successfully execute. func(r *Router) Route(ctx context.Context, gev *geEvent.Event) error { logg.DebugCtxf(ctx, "have event", "ev", gev) + store, err := r.Store.GetUserdataDb(ctx) + if err != nil { + return err + } + userStore := &common.UserDataStore{ + Db: store, + } evCC, ok := asCustodialRegistrationEvent(gev) if ok { - return handleCustodialRegistration(ctx, r.Store, evCC) + return handleCustodialRegistration(ctx, userStore, evCC) } evTT, ok := asTokenTransferEvent(gev) if ok { - return handleTokenTransfer(ctx, r.Store, evTT) + return handleTokenTransfer(ctx, userStore, evTT) } return fmt.Errorf("unexpected message") diff --git a/internal/testutil/service.go b/internal/testutil/service.go new file mode 100644 index 0000000..da2b71f --- /dev/null +++ b/internal/testutil/service.go @@ -0,0 +1,30 @@ +package testutil + +import ( + "context" + "errors" + + "git.defalsify.org/vise.git/persist" + "git.defalsify.org/vise.git/resource" + "git.defalsify.org/vise.git/db" +) + +type TestStorageService struct { + Store db.Db +} + +func(ss *TestStorageService) GetUserdataDb(ctx context.Context) (db.Db, error) { + return ss.Store, nil +} + +func(ts *TestStorageService) GetPersister(ctx context.Context) (*persist.Persister, error) { + return nil, errors.New("not implemented") +} + +func(ts *TestStorageService) GetResource(ctx context.Context) (resource.Resource, error) { + return nil, errors.New("not implemented") +} + +func(ss *TestStorageService) EnsureDbDir() error { + return nil +}