Implement storageservice in nats subscription object

This commit is contained in:
lash 2024-11-03 23:08:50 +00:00
parent 7c7eff2aff
commit f441047fa1
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
5 changed files with 66 additions and 16 deletions

View File

@ -2,13 +2,14 @@ package main
import ( import (
"context" "context"
"flag"
"fmt" "fmt"
"os" "os"
"os/signal" "os/signal"
"syscall" "syscall"
"git.defalsify.org/vise.git/db/mem"
"git.grassecon.net/urdt/ussd/initializers" "git.grassecon.net/urdt/ussd/initializers"
"git.grassecon.net/urdt/ussd/common"
"git.grassecon.net/term/config" "git.grassecon.net/term/config"
"git.grassecon.net/term/event/nats" "git.grassecon.net/term/event/nats"
) )
@ -18,14 +19,28 @@ func init() {
} }
func main() { func main() {
config.LoadConfig()
var dbDir string
flag.StringVar(&dbDir, "dbdir", ".state", "database dir to read from")
flag.Parse()
ctx := context.Background() ctx := context.Background()
db := mem.NewMemDb() // db := mem.NewMemDb()
err := db.Connect(ctx, "") // 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 { if err != nil {
fmt.Fprintf(os.Stderr, "Db connect err: %v", err) fmt.Fprintf(os.Stderr, err.Error())
os.Exit(1) os.Exit(1)
} }
n := nats.NewNatsSubscription(db)
n := nats.NewNatsSubscription(menuStorageService)
err = n.Connect(ctx, config.JetstreamURL) err = n.Connect(ctx, config.JetstreamURL)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "Stream connect err: %v", err) fmt.Fprintf(os.Stderr, "Stream connect err: %v", err)

View File

@ -9,7 +9,6 @@ import (
"github.com/nats-io/nats.go/jetstream" "github.com/nats-io/nats.go/jetstream"
geEvent "github.com/grassrootseconomics/eth-tracker/pkg/event" geEvent "github.com/grassrootseconomics/eth-tracker/pkg/event"
"git.defalsify.org/vise.git/logging" "git.defalsify.org/vise.git/logging"
"git.defalsify.org/vise.git/db"
"git.grassecon.net/urdt/ussd/common" "git.grassecon.net/urdt/ussd/common"
"git.grassecon.net/term/event" "git.grassecon.net/term/event"
"git.grassecon.net/term/config" "git.grassecon.net/term/config"
@ -32,12 +31,10 @@ type NatsSubscription struct {
} }
// NewNatsSubscription creates a new NatsSubscription with the given user store. // NewNatsSubscription creates a new NatsSubscription with the given user store.
func NewNatsSubscription(store db.Db) *NatsSubscription { func NewNatsSubscription(store common.StorageServices) *NatsSubscription {
return &NatsSubscription{ return &NatsSubscription{
Router: event.Router{ Router: event.Router{
Store: &common.UserDataStore{ Store: store,
Db: store,
},
}, },
} }
} }

View File

@ -141,8 +141,10 @@ func TestHandleMsg(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
storageService := &testutil.TestStorageService{
sub := NewNatsSubscription(userDb) Store: userDb,
}
sub := NewNatsSubscription(storageService)
data := fmt.Sprintf(`{ data := fmt.Sprintf(`{
"block": %d, "block": %d,

View File

@ -16,8 +16,7 @@ var (
// Router is responsible for invoking handlers corresponding to events. // Router is responsible for invoking handlers corresponding to events.
type Router struct { type Router struct {
// User data store abstraction over application data. Store common.StorageServices
Store *common.UserDataStore
} }
// Route parses an event from the event stream, and resolves the handler // Route parses an event from the event stream, and resolves the handler
@ -27,13 +26,20 @@ type Router struct {
// handler fails to successfully execute. // handler fails to successfully execute.
func(r *Router) Route(ctx context.Context, gev *geEvent.Event) error { func(r *Router) Route(ctx context.Context, gev *geEvent.Event) error {
logg.DebugCtxf(ctx, "have event", "ev", gev) 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) evCC, ok := asCustodialRegistrationEvent(gev)
if ok { if ok {
return handleCustodialRegistration(ctx, r.Store, evCC) return handleCustodialRegistration(ctx, userStore, evCC)
} }
evTT, ok := asTokenTransferEvent(gev) evTT, ok := asTokenTransferEvent(gev)
if ok { if ok {
return handleTokenTransfer(ctx, r.Store, evTT) return handleTokenTransfer(ctx, userStore, evTT)
} }
return fmt.Errorf("unexpected message") return fmt.Errorf("unexpected message")

View File

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