From 23f527b7696420c4c32021df30c1dd44fd074160 Mon Sep 17 00:00:00 2001 From: Angel Beltran Date: Sun, 11 Jan 2026 15:42:03 -0700 Subject: [PATCH] moved webhook initialization down --- internal/server/server.go | 22 +++++++++++++++ main.go | 59 ++++++++++++--------------------------- 2 files changed, 40 insertions(+), 41 deletions(-) diff --git a/internal/server/server.go b/internal/server/server.go index ea7189e..afb0c2c 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -19,6 +19,8 @@ import ( "ruben/inventory2/internal/domains/raw_events" "ruben/inventory2/internal/server/middleware" "ruben/inventory2/internal/server/response" + "ruben/inventory2/internal/server/webhooks" + etsy_webhooks "ruben/inventory2/internal/server/webhooks/etsy" ) type Server struct { @@ -143,10 +145,12 @@ func NewServer( // login + // TODO: consider a separate '/login or /auth' router mux.Handle("GET /login", s.loginPage) mux.Handle("GET /login/callback", s.loginCallback) mux.Handle("GET /logout", s.logoutPage) + // TODO: consider a separate '/accounts' router // /accounts mux.Handle("POST /accounts", withAuth(s.createAccount)) @@ -156,6 +160,24 @@ func NewServer( mux.Handle("DELETE /accounts/{acctID}/inventory/sync-groups/draft/listings/{orderIndex}", withAuth(s.deleteSyncGroupListingDraft)) mux.Handle("POST /accounts/{acctID}/inventory/sync-groups", withAuth(s.saveNewSyncGroup)) + // TODO: consider a webhook separate router + // webhooks + webhookHandler := http.StripPrefix("/webhooks", webhooks.New( + logger.WithGroup("webhooks"), + rawEvents, + etsy, + webhooks.Config{ + Etsy: etsy_webhooks.Config{ + OAuthRedirectURIWithAcctIDParam: "/oauth/account/{acctID}/auth_code", + }, + }, + )) + mux.Mux.Handle("GET /webhooks/", webhookHandler) + mux.Mux.Handle("POST /webhooks/", webhookHandler) + mux.Mux.Handle("PUT /webhooks/", webhookHandler) + + // TODO: consider a separate webpage router + // webpage content // non-html content: scripts, styles, images, etc diff --git a/main.go b/main.go index 72001f5..545405e 100644 --- a/main.go +++ b/main.go @@ -23,8 +23,6 @@ import ( etsy_platform "ruben/inventory2/internal/domains/platforms/etsy" "ruben/inventory2/internal/domains/raw_events" "ruben/inventory2/internal/server" - "ruben/inventory2/internal/server/webhooks" - etsy_webhooks "ruben/inventory2/internal/server/webhooks/etsy" ) const ( @@ -141,8 +139,24 @@ func runAuthProcesses(ctx context.Context, auth *authentication.Authenticator) < func runServer(ctx context.Context, logger *slog.Logger, connPool *pgxpool.Pool, auth *authentication.Authenticator) <-chan error { srv := &http.Server{ - Addr: ":8082", // local - Handler: buildHTTPHandler(logger, connPool, auth), + Addr: ":8082", // local + Handler: server.NewServer( + logger.WithGroup("server"), + // TODO: consider moving the templates up to a higher level... + "./internal/server", + raw_events.NewStore(logger.WithGroup("raw-event-store"), connPool), + accounts.NewStore(logger, connPool), + etsy_platform.NewPlatform( + logger, + func(acctID int64) string { + return fmt.Sprintf("/oauth/account/%d/auth_code", acctID) + }, + etsyAPIKeystring, + etsyAPISharedSecret, + connPool, + ), + auth, + ), } ctx, cancel := context.WithCancel(ctx) @@ -192,40 +206,3 @@ func runServer(ctx context.Context, logger *slog.Logger, connPool *pgxpool.Pool, return errCh } - -func buildHTTPHandler(logger *slog.Logger, connPool *pgxpool.Pool, auth *authentication.Authenticator) http.Handler { - eventsDB := raw_events.NewStore(logger.WithGroup("raw-event-store"), connPool) - etsy := etsy_platform.NewPlatform( - logger, - func(acctID int64) string { - return fmt.Sprintf("/oauth/account/%d/auth_code", acctID) - }, - etsyAPIKeystring, - etsyAPISharedSecret, - connPool, - ) - accts := accounts.NewStore(logger, connPool) - - mux := http.NewServeMux() - - mux.Handle("/webhooks/", http.StripPrefix("/webhooks", webhooks.New( - logger.WithGroup("webhooks"), - eventsDB, - etsy, - webhooks.Config{ - Etsy: etsy_webhooks.Config{ - OAuthRedirectURIWithAcctIDParam: "/oauth/account/{acctID}/auth_code", - }, - }, - ))) - mux.Handle("/", server.NewServer( - logger.WithGroup("server"), - "./internal/server", - eventsDB, - accts, - etsy, - auth, - )) - - return mux -}