From a6267ac1893e1324e130bc068df4cffe38118f60 Mon Sep 17 00:00:00 2001 From: Angel Beltran Date: Sat, 24 Jan 2026 14:06:19 -0700 Subject: [PATCH] moved middleware event publisher to sse package --- internal/server/api/accounts/router.go | 3 +- internal/server/api/apis.go | 2 +- internal/server/server.go | 6 ++- .../events.go => sse/publisher.go} | 42 ++++++++++++------- internal/server/sse/{sse.go => queue.go} | 0 5 files changed, 33 insertions(+), 20 deletions(-) rename internal/server/{middleware/events.go => sse/publisher.go} (80%) rename internal/server/sse/{sse.go => queue.go} (100%) diff --git a/internal/server/api/accounts/router.go b/internal/server/api/accounts/router.go index f0a65a4..25b5a54 100644 --- a/internal/server/api/accounts/router.go +++ b/internal/server/api/accounts/router.go @@ -9,6 +9,7 @@ import ( "ruben/inventory2/internal/logging" "ruben/inventory2/internal/server/middleware" "ruben/inventory2/internal/server/response" + "ruben/inventory2/internal/server/sse" "strconv" "github.com/gin-gonic/gin" @@ -23,7 +24,7 @@ func Routes( r *gin.RouterGroup, logger *logging.Logger, accts *accounts.Store, - pub *middleware.UpdateNotificationPublisher, + pub *sse.UpdateNotificationPublisher, ) { as := &accountSubrouter{ log: logger, diff --git a/internal/server/api/apis.go b/internal/server/api/apis.go index b1cc193..eb1d4eb 100644 --- a/internal/server/api/apis.go +++ b/internal/server/api/apis.go @@ -22,7 +22,7 @@ func Routes( auth *middleware.Auth, sq *sse.Queue, accts *accounts.Store, - unp *middleware.UpdateNotificationPublisher, + unp *sse.UpdateNotificationPublisher, rawEvents *raw_events.Store, etsy *etsy_platform.Platform, ) { diff --git a/internal/server/server.go b/internal/server/server.go index aaf3d76..ef34312 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -79,9 +79,11 @@ func NewRouter( // sse setup sq := sse.NewQueue() - unp := middleware.NewUpdateNotificationPublisher( + unp := sq.NewUpdateNotificationPublisher( logger.WithGroup("update.notification.publisher"), - sq, + func(c *gin.Context) int64 { + return middleware.GetIdentity(c).Account.AccountID + }, ).Trim("/api") api.Routes( diff --git a/internal/server/middleware/events.go b/internal/server/sse/publisher.go similarity index 80% rename from internal/server/middleware/events.go rename to internal/server/sse/publisher.go index 1007e2f..7b3903d 100644 --- a/internal/server/middleware/events.go +++ b/internal/server/sse/publisher.go @@ -1,31 +1,40 @@ -package middleware +package sse import ( "context" "fmt" "path" - "ruben/inventory2/internal/logging" - "ruben/inventory2/internal/server/sse" "strings" "time" "github.com/gin-gonic/gin" + + "ruben/inventory2/internal/logging" ) -type UpdateNotificationPublisher struct { - log *logging.Logger - sse *sse.Queue - trimBasePath string - basePathPattern string -} +type ( + UpdateNotificationPublisher struct { + log *logging.Logger + queue sender + getAccountID func(*gin.Context) int64 + trimBasePath string + basePathPattern string + } -func NewUpdateNotificationPublisher( + // sender is satisfied by *Queue + sender interface { + Send(ctx context.Context, e Event) error + } +) + +func (q *Queue) NewUpdateNotificationPublisher( log *logging.Logger, - sse *sse.Queue, + getAccountID func(*gin.Context) int64, ) *UpdateNotificationPublisher { return &UpdateNotificationPublisher{ - log: log, - sse: sse, + log: log, + queue: q, + getAccountID: getAccountID, } } @@ -46,7 +55,8 @@ func (p *UpdateNotificationPublisher) Group(pathPattern string) *UpdateNotificat return &p2 } -// Publish must be applied AFTER a middleware puts in the Identity into the gin.Context. +// Publish constructs a gin middleware. +// It must used with and after middleware puts in the Identity into the gin.Context. func (p *UpdateNotificationPublisher) Publish(pathPattern string) gin.HandlerFunc { trimBasePathSegs := getPathSegments(p.trimBasePath) trimmedBasePathPattern := path.Join(p.basePathPattern, pathPattern) @@ -92,13 +102,13 @@ func (p *UpdateNotificationPublisher) Publish(pathPattern string) gin.HandlerFun parentEvent = e } - acctID := GetIdentity(c).Account.AccountID + acctID := p.getAccountID(c) for _, e := range events { go func() { ctx, cancel := context.WithTimeout(context.Background(), time.Minute) defer cancel() - if err := p.sse.Send(ctx, sse.Event{ + if err := p.queue.Send(ctx, Event{ AccountID: acctID, Type: e, Data: []byte(fmt.Sprintf(`{"eventType": %q}`, e)), diff --git a/internal/server/sse/sse.go b/internal/server/sse/queue.go similarity index 100% rename from internal/server/sse/sse.go rename to internal/server/sse/queue.go