moved middleware event publisher to sse package
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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,
|
||||
) {
|
||||
|
||||
@@ -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(
|
||||
|
||||
@@ -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)),
|
||||
Reference in New Issue
Block a user