sarafu-vise-events/event/route.go

46 lines
1.1 KiB
Go
Raw Normal View History

2024-10-23 22:01:10 +02:00
package event
import (
"context"
2024-10-24 17:00:46 +02:00
"fmt"
2024-10-23 22:01:10 +02:00
geEvent "github.com/grassrootseconomics/eth-tracker/pkg/event"
2024-11-02 23:58:09 +01:00
2024-11-03 01:34:28 +01:00
"git.defalsify.org/vise.git/logging"
apievent "git.grassecon.net/grassrootseconomics/sarafu-api/event"
2024-10-23 22:01:10 +02:00
)
2024-10-24 17:00:46 +02:00
var (
2024-11-03 01:34:28 +01:00
logg = logging.NewVanilla().WithDomain("term-event")
2024-10-24 17:00:46 +02:00
)
2024-11-03 20:04:44 +01:00
// Router is responsible for invoking handlers corresponding to events.
2024-10-23 22:01:10 +02:00
type Router struct {
handler *apievent.EventsHandler
2025-01-13 18:47:21 +01:00
}
func NewRouter(handler *apievent.EventsHandler) *Router {
2025-01-13 18:47:21 +01:00
return &Router{
handler: handler,
}
2024-10-23 22:01:10 +02:00
}
2024-11-03 20:04:44 +01:00
// Route parses an event from the event stream, and resolves the handler
// corresponding to the event.
//
// An error will be returned if no handler can be found, or if the resolved
// handler fails to successfully execute.
func(r *Router) Route(ctx context.Context, gev *geEvent.Event) error {
2024-11-03 01:34:28 +01:00
logg.DebugCtxf(ctx, "have event", "ev", gev)
2024-10-24 17:00:46 +02:00
evCC, ok := asCustodialRegistrationEvent(gev)
2024-10-24 17:17:04 +02:00
if ok {
return r.handler.Handle(ctx, apievent.EventTokenTransferTag, evCC)
2024-10-24 17:00:46 +02:00
}
2024-11-03 01:34:28 +01:00
evTT, ok := asTokenTransferEvent(gev)
if ok {
return r.handler.Handle(ctx, apievent.EventRegistrationTag, evTT)
2024-11-03 01:34:28 +01:00
}
2024-10-24 17:00:46 +02:00
return fmt.Errorf("unexpected message")
2024-10-23 22:01:10 +02:00
}