amazon events published as server side event
This commit is contained in:
@@ -29,6 +29,7 @@ import (
|
||||
"ruben/inventory2/domains/reports"
|
||||
"ruben/inventory2/logging"
|
||||
"ruben/inventory2/server"
|
||||
"ruben/inventory2/server/sse"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -74,24 +75,40 @@ func runApp(ctx context.Context, logger *logging.Logger) error {
|
||||
return fmt.Errorf("failed to initialize database connection pool: %w", err)
|
||||
}
|
||||
|
||||
// start background processes
|
||||
|
||||
auth, err := authentication.New(ctx, connPool, logger.WithGroup("authenticator"))
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to construct authenticator: %w", err)
|
||||
}
|
||||
|
||||
accts := accounts.NewStore(logger.WithGroup("accounts"), connPool)
|
||||
|
||||
// start http server
|
||||
|
||||
sseQueue, srvErrCh := runServer(ctx, logger, connPool, auth, accts)
|
||||
|
||||
// start background processes
|
||||
|
||||
authErrCh := runAuthProcesses(ctx, auth)
|
||||
|
||||
eventErrCh := runEventProcessing(
|
||||
ctx,
|
||||
amazon.NewMocks(logger.WithGroup("amazon"), connPool),
|
||||
amazon.NewMocks(logger.WithGroup("amazon"), connPool).
|
||||
SetListener(sseQueue.NewDBEventPublisher(
|
||||
func(ctx context.Context, e raw_events.Event) (acctID int64, err error) {
|
||||
p, err := accounts.NewPlatform(e.Platform)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return accts.GetAccountIDByMockPlatformAndShopID(ctx, p, e.StoreID)
|
||||
},
|
||||
func(acctID int64, e raw_events.Event) (eventType string, err error) {
|
||||
// TODO: fine tune the event type later (don't want a referesh on EVERY event)
|
||||
return fmt.Sprintf("accounts_%d_simulations", acctID), nil
|
||||
},
|
||||
)),
|
||||
)
|
||||
|
||||
// start http server
|
||||
|
||||
srvErrCh := runServer(ctx, logger, connPool, auth)
|
||||
|
||||
// wait for interrupt signal or unrecoverable failure, then shutdown
|
||||
|
||||
osSignalCh := make(chan os.Signal, 1)
|
||||
@@ -184,9 +201,13 @@ func runEventProcessing(ctx context.Context, amz *amazon.Mocks) <-chan error {
|
||||
return errCh
|
||||
}
|
||||
|
||||
func runServer(ctx context.Context, logger *logging.Logger, connPool *pgxpool.Pool, auth *authentication.Authenticator) <-chan error {
|
||||
accts := accounts.NewStore(logger.WithGroup("accounts"), connPool)
|
||||
|
||||
func runServer(
|
||||
ctx context.Context,
|
||||
logger *logging.Logger,
|
||||
connPool *pgxpool.Pool,
|
||||
auth *authentication.Authenticator,
|
||||
accts *accounts.Store,
|
||||
) (*sse.Queue, <-chan error) {
|
||||
r := server.NewRouter(
|
||||
logger.WithGroup("server"),
|
||||
"./",
|
||||
@@ -268,5 +289,5 @@ func runServer(ctx context.Context, logger *logging.Logger, connPool *pgxpool.Po
|
||||
}
|
||||
}()
|
||||
|
||||
return errCh
|
||||
return r.GetSSEQueue(), errCh
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user