Toggle services with build tags, cmd now runs with dev api

This commit is contained in:
lash 2025-01-15 00:16:03 +00:00
parent 960a3d9075
commit ac00e4619e
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
5 changed files with 76 additions and 57 deletions

View File

@ -13,13 +13,9 @@ import (
"git.defalsify.org/vise.git/lang" "git.defalsify.org/vise.git/lang"
"git.grassecon.net/grassrootseconomics/sarafu-vise/config" "git.grassecon.net/grassrootseconomics/sarafu-vise/config"
"git.grassecon.net/grassrootseconomics/visedriver/storage" "git.grassecon.net/grassrootseconomics/visedriver/storage"
httpremote "git.grassecon.net/grassrootseconomics/sarafu-api/remote/http" "git.grassecon.net/grassrootseconomics/sarafu-vise/services"
devremote "git.grassecon.net/grassrootseconomics/sarafu-api/dev"
"git.grassecon.net/grassrootseconomics/sarafu-api/remote"
apievent "git.grassecon.net/grassrootseconomics/sarafu-api/event"
"git.grassecon.net/grassrootseconomics/sarafu-vise/args" "git.grassecon.net/grassrootseconomics/sarafu-vise/args"
"git.grassecon.net/grassrootseconomics/sarafu-vise/handlers" "git.grassecon.net/grassrootseconomics/sarafu-vise/handlers"
"git.grassecon.net/grassrootseconomics/sarafu-vise/handlers/event"
) )
var ( var (
@ -28,37 +24,9 @@ var (
menuSeparator = ": " menuSeparator = ": "
) )
type devEmitter struct {
h *apievent.EventsHandler
}
func (d *devEmitter) emit(ctx context.Context, msg apievent.Msg) error {
var err error
if msg.Typ == apievent.EventTokenTransferTag {
tx, ok := msg.Item.(devremote.Tx)
if !ok {
return fmt.Errorf("not a valid tx")
}
logg.InfoCtxf(ctx, "tx emit", "tx", tx)
ev := tx.ToTransferEvent()
err = d.h.Handle(ctx, apievent.EventTokenTransferTag, &ev)
} else if msg.Typ == apievent.EventRegistrationTag {
acc, ok := msg.Item.(devremote.Account)
if !ok {
return fmt.Errorf("not a valid tx")
}
logg.InfoCtxf(ctx, "account emit", "account", acc)
ev := acc.ToRegistrationEvent()
err = d.h.Handle(ctx, apievent.EventRegistrationTag, &ev)
}
return err
}
func main() { func main() {
config.LoadConfig() config.LoadConfig()
var accountService remote.AccountService
var fakeDir string
var connStr string var connStr string
var size uint var size uint
var sessionId string var sessionId string
@ -71,7 +39,6 @@ func main() {
flag.StringVar(&resourceDir, "resourcedir", scriptDir, "resource dir") flag.StringVar(&resourceDir, "resourcedir", scriptDir, "resource dir")
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.StringVar(&fakeDir, "fakedir", "", "if valid path, enables fake api with fsdb backend")
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(&gettextDir, "gettext", "", "use gettext translations from given directory") flag.StringVar(&gettextDir, "gettext", "", "use gettext translations from given directory")
@ -155,20 +122,7 @@ func main() {
os.Exit(1) os.Exit(1)
} }
if fakeDir != "" { accountService := services.New(ctx, menuStorageService, connData)
svc := devremote.NewDevAccountService()
svc = svc.WithFs(ctx, fakeDir)
svc = svc.WithAutoVoucher(ctx, "FOO", 42)
eu := event.NewEventsUpdater(svc, menuStorageService)
emitter := &devEmitter{
h: eu.ToEventsHandler(),
}
svc = svc.WithEmitter(emitter.emit)
svc.AddVoucher(ctx, "BAR")
accountService = svc
} else {
accountService = &httpremote.HTTPAccountService{}
}
hl, err := lhs.GetHandler(accountService) hl, err := lhs.GetHandler(accountService)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "get accounts service handler: %v\n", err) fmt.Fprintf(os.Stderr, "get accounts service handler: %v\n", err)

6
go.mod
View File

@ -3,10 +3,10 @@ module git.grassecon.net/grassrootseconomics/sarafu-vise
go 1.23.4 go 1.23.4
require ( require (
git.defalsify.org/vise.git v0.2.3-0.20250103172917-3e190a44568d git.defalsify.org/vise.git v0.2.3-0.20250115000535-e2d329b3f739
git.grassecon.net/grassrootseconomics/common v0.0.0-20250113174703-6afccefd1f05 git.grassecon.net/grassrootseconomics/common v0.0.0-20250113174703-6afccefd1f05
git.grassecon.net/grassrootseconomics/sarafu-api v0.0.0-20250114142051-9645a2af7bef git.grassecon.net/grassrootseconomics/sarafu-api v0.0.0-20250115001238-7001d1f828aa
git.grassecon.net/grassrootseconomics/visedriver v0.8.0-beta.10.0.20250113213325-5228aef0889b git.grassecon.net/grassrootseconomics/visedriver v0.8.0-beta.10.0.20250115001111-24e729d27566
git.grassecon.net/grassrootseconomics/visedriver-africastalking v0.0.0-20250113103030-f0b2056fd87d git.grassecon.net/grassrootseconomics/visedriver-africastalking v0.0.0-20250113103030-f0b2056fd87d
github.com/alecthomas/assert/v2 v2.2.2 github.com/alecthomas/assert/v2 v2.2.2
github.com/gofrs/uuid v4.4.0+incompatible github.com/gofrs/uuid v4.4.0+incompatible

12
go.sum
View File

@ -1,11 +1,11 @@
git.defalsify.org/vise.git v0.2.3-0.20250103172917-3e190a44568d h1:bPAOVZOX4frSGhfOdcj7kc555f8dc9DmMd2YAyC2AMw= git.defalsify.org/vise.git v0.2.3-0.20250115000535-e2d329b3f739 h1:w7pj1oh7jXrfajahVYU7m7AfHst4C6jNVzDVoaqJ7e8=
git.defalsify.org/vise.git v0.2.3-0.20250103172917-3e190a44568d/go.mod h1:jyBMe1qTYUz3mmuoC9JQ/TvFeW0vTanCUcPu3H8p4Ck= git.defalsify.org/vise.git v0.2.3-0.20250115000535-e2d329b3f739/go.mod h1:jyBMe1qTYUz3mmuoC9JQ/TvFeW0vTanCUcPu3H8p4Ck=
git.grassecon.net/grassrootseconomics/common v0.0.0-20250113174703-6afccefd1f05 h1:BenzGx6aDHKDwE23/mWIFD2InYIXyzHroZWV3MF5WUk= git.grassecon.net/grassrootseconomics/common v0.0.0-20250113174703-6afccefd1f05 h1:BenzGx6aDHKDwE23/mWIFD2InYIXyzHroZWV3MF5WUk=
git.grassecon.net/grassrootseconomics/common v0.0.0-20250113174703-6afccefd1f05/go.mod h1:wgQJZGIS6QuNLHqDhcsvehsbn5PvgV7aziRebMnJi60= git.grassecon.net/grassrootseconomics/common v0.0.0-20250113174703-6afccefd1f05/go.mod h1:wgQJZGIS6QuNLHqDhcsvehsbn5PvgV7aziRebMnJi60=
git.grassecon.net/grassrootseconomics/sarafu-api v0.0.0-20250114142051-9645a2af7bef h1:ukxNTuI+MF6ic2SkRlc8ZjlnsTR8g6owuHkAvnQNY8A= git.grassecon.net/grassrootseconomics/sarafu-api v0.0.0-20250115001238-7001d1f828aa h1:V1K4CqA2lYJVKv5uPIZ8zy0pIWUM78+M8xQnum2C5aM=
git.grassecon.net/grassrootseconomics/sarafu-api v0.0.0-20250114142051-9645a2af7bef/go.mod h1:BRIbXv0aulFWqR2XV87haWB4oNRlVEu8KyldLQsAroQ= git.grassecon.net/grassrootseconomics/sarafu-api v0.0.0-20250115001238-7001d1f828aa/go.mod h1:yT/NvEiicBoWWRmOvBp1nT5YED3akTVdeZ2Ntq4hOFg=
git.grassecon.net/grassrootseconomics/visedriver v0.8.0-beta.10.0.20250113213325-5228aef0889b h1:6SieNUSEKbkjzquuwazs/lVG56zdEWF10zQQEMRJfMs= git.grassecon.net/grassrootseconomics/visedriver v0.8.0-beta.10.0.20250115001111-24e729d27566 h1:+Mrd6e7KnWpnzMTjwmPyXgF4+enAelmDbAqRzhq1aAM=
git.grassecon.net/grassrootseconomics/visedriver v0.8.0-beta.10.0.20250113213325-5228aef0889b/go.mod h1:E6W7ZOa7ZvVr0Bc5ot0LNSwpSPYq4hXlAIvEPy3AJ7U= git.grassecon.net/grassrootseconomics/visedriver v0.8.0-beta.10.0.20250115001111-24e729d27566/go.mod h1:Syw9TZyigPAM7t9FvicOm36iUnidt45f0SxzT2JniQ8=
git.grassecon.net/grassrootseconomics/visedriver-africastalking v0.0.0-20250113103030-f0b2056fd87d h1:q/NO1rEgK3pia32D/tCq5hXfEuJp84COZRwceFvy/MM= git.grassecon.net/grassrootseconomics/visedriver-africastalking v0.0.0-20250113103030-f0b2056fd87d h1:q/NO1rEgK3pia32D/tCq5hXfEuJp84COZRwceFvy/MM=
git.grassecon.net/grassrootseconomics/visedriver-africastalking v0.0.0-20250113103030-f0b2056fd87d/go.mod h1:AH15xABcvaJr1TCGlih3oGSuwWC0E5IdbHQwuu+E1KI= git.grassecon.net/grassrootseconomics/visedriver-africastalking v0.0.0-20250113103030-f0b2056fd87d/go.mod h1:AH15xABcvaJr1TCGlih3oGSuwWC0E5IdbHQwuu+E1KI=
github.com/alecthomas/assert/v2 v2.2.2 h1:Z/iVC0xZfWTaFNE6bA3z07T86hd45Xe2eLt6WVy2bbk= github.com/alecthomas/assert/v2 v2.2.2 h1:Z/iVC0xZfWTaFNE6bA3z07T86hd45Xe2eLt6WVy2bbk=

50
services/local.go Normal file
View File

@ -0,0 +1,50 @@
// +build !online
package services
import (
"fmt"
"context"
"git.grassecon.net/grassrootseconomics/visedriver/storage"
devremote "git.grassecon.net/grassrootseconomics/sarafu-api/dev"
"git.grassecon.net/grassrootseconomics/sarafu-api/remote"
apievent "git.grassecon.net/grassrootseconomics/sarafu-api/event"
"git.grassecon.net/grassrootseconomics/sarafu-vise/handlers/event"
)
type localEmitter struct {
h *apievent.EventsHandler
}
func (d *localEmitter) emit(ctx context.Context, msg apievent.Msg) error {
var err error
if msg.Typ == apievent.EventTokenTransferTag {
tx, ok := msg.Item.(devremote.Tx)
if !ok {
return fmt.Errorf("not a valid tx")
}
ev := tx.ToTransferEvent()
err = d.h.Handle(ctx, apievent.EventTokenTransferTag, &ev)
} else if msg.Typ == apievent.EventRegistrationTag {
acc, ok := msg.Item.(devremote.Account)
if !ok {
return fmt.Errorf("not a valid tx")
}
ev := acc.ToRegistrationEvent()
err = d.h.Handle(ctx, apievent.EventRegistrationTag, &ev)
}
return err
}
func New(ctx context.Context, storageService storage.StorageService, conn storage.ConnData) remote.AccountService {
svc := devremote.NewDevAccountService(ctx, storageService)
svc = svc.WithAutoVoucher(ctx, "FOO", 42)
eu := event.NewEventsUpdater(svc, storageService)
emitter := &localEmitter{
h: eu.ToEventsHandler(),
}
svc = svc.WithEmitter(emitter.emit)
svc.AddVoucher(ctx, "BAR")
return svc
}

15
services/remote.go Normal file
View File

@ -0,0 +1,15 @@
// +build online
package services
import (
"context"
"git.grassecon.net/grassrootseconomics/visedriver/storage"
"git.grassecon.net/grassrootseconomics/sarafu-api/remote"
httpremote "git.grassecon.net/grassrootseconomics/sarafu-api/remote/http"
)
func New(ctx context.Context, storageService storage.StorageService, conn storage.ConnData) remote.AccountService {
return &httpremote.HTTPAccountService{}
}