moved webhook initialization down

This commit is contained in:
2026-01-11 15:42:03 -07:00
parent 388902a1a0
commit 23f527b769
2 changed files with 40 additions and 41 deletions
+22
View File
@@ -19,6 +19,8 @@ import (
"ruben/inventory2/internal/domains/raw_events" "ruben/inventory2/internal/domains/raw_events"
"ruben/inventory2/internal/server/middleware" "ruben/inventory2/internal/server/middleware"
"ruben/inventory2/internal/server/response" "ruben/inventory2/internal/server/response"
"ruben/inventory2/internal/server/webhooks"
etsy_webhooks "ruben/inventory2/internal/server/webhooks/etsy"
) )
type Server struct { type Server struct {
@@ -143,10 +145,12 @@ func NewServer(
// login // login
// TODO: consider a separate '/login or /auth' router
mux.Handle("GET /login", s.loginPage) mux.Handle("GET /login", s.loginPage)
mux.Handle("GET /login/callback", s.loginCallback) mux.Handle("GET /login/callback", s.loginCallback)
mux.Handle("GET /logout", s.logoutPage) mux.Handle("GET /logout", s.logoutPage)
// TODO: consider a separate '/accounts' router
// /accounts // /accounts
mux.Handle("POST /accounts", withAuth(s.createAccount)) 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("DELETE /accounts/{acctID}/inventory/sync-groups/draft/listings/{orderIndex}", withAuth(s.deleteSyncGroupListingDraft))
mux.Handle("POST /accounts/{acctID}/inventory/sync-groups", withAuth(s.saveNewSyncGroup)) 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 // webpage content
// non-html content: scripts, styles, images, etc // non-html content: scripts, styles, images, etc
+17 -40
View File
@@ -23,8 +23,6 @@ import (
etsy_platform "ruben/inventory2/internal/domains/platforms/etsy" etsy_platform "ruben/inventory2/internal/domains/platforms/etsy"
"ruben/inventory2/internal/domains/raw_events" "ruben/inventory2/internal/domains/raw_events"
"ruben/inventory2/internal/server" "ruben/inventory2/internal/server"
"ruben/inventory2/internal/server/webhooks"
etsy_webhooks "ruben/inventory2/internal/server/webhooks/etsy"
) )
const ( const (
@@ -142,7 +140,23 @@ func runAuthProcesses(ctx context.Context, auth *authentication.Authenticator) <
func runServer(ctx context.Context, logger *slog.Logger, connPool *pgxpool.Pool, auth *authentication.Authenticator) <-chan error { func runServer(ctx context.Context, logger *slog.Logger, connPool *pgxpool.Pool, auth *authentication.Authenticator) <-chan error {
srv := &http.Server{ srv := &http.Server{
Addr: ":8082", // local Addr: ":8082", // local
Handler: buildHTTPHandler(logger, connPool, auth), 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) ctx, cancel := context.WithCancel(ctx)
@@ -192,40 +206,3 @@ func runServer(ctx context.Context, logger *slog.Logger, connPool *pgxpool.Pool,
return errCh 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
}