sse support
This commit is contained in:
@@ -15,23 +15,29 @@ import (
|
||||
"ruben/inventory2/internal/logging"
|
||||
accounts_api "ruben/inventory2/internal/server/api/accounts"
|
||||
auth_api "ruben/inventory2/internal/server/api/auth"
|
||||
sse_api "ruben/inventory2/internal/server/api/sse"
|
||||
templates_api "ruben/inventory2/internal/server/api/templates"
|
||||
"ruben/inventory2/internal/server/api/webhooks"
|
||||
etsy_webhooks "ruben/inventory2/internal/server/api/webhooks/etsy"
|
||||
"ruben/inventory2/internal/server/middleware"
|
||||
"ruben/inventory2/internal/server/sse"
|
||||
)
|
||||
|
||||
// r gin.IRouter, // TODO: use this everywhere
|
||||
|
||||
func Router(
|
||||
ctx context.Context,
|
||||
type Router struct {
|
||||
*gin.Engine
|
||||
sse *sse.Queue
|
||||
}
|
||||
|
||||
func NewRouter(
|
||||
logger *logging.Logger,
|
||||
contentDir string,
|
||||
rawEvents *raw_events.Store,
|
||||
accts *accounts.Store,
|
||||
etsy *etsy_platform.Platform,
|
||||
auth *authentication.Authenticator,
|
||||
) *gin.Engine {
|
||||
) *Router {
|
||||
r := gin.Default()
|
||||
|
||||
// TODO: shouldn't this ACTUALLY be a middleware?
|
||||
@@ -74,21 +80,37 @@ func Router(
|
||||
authMiddleware,
|
||||
)
|
||||
|
||||
// sse setup
|
||||
|
||||
sq := sse.NewQueue()
|
||||
unp := middleware.NewUpdateNotificationPublisher(
|
||||
logger.WithGroup("update.notification.publisher"),
|
||||
sq,
|
||||
)
|
||||
|
||||
// api endpoints
|
||||
|
||||
api := r.Group("/api")
|
||||
apiLogger := logger.WithGroup("/api")
|
||||
unp = unp.Trim("/api")
|
||||
|
||||
auth_api.Routes(
|
||||
api.Group("/auth"),
|
||||
logger.WithGroup("/auth"),
|
||||
apiLogger.WithGroup("/auth"),
|
||||
auth,
|
||||
)
|
||||
accounts_api.Routes(
|
||||
api.Group("/accounts"),
|
||||
logger.WithGroup("/accounts"),
|
||||
accts,
|
||||
sse_api.Routes(
|
||||
api.Group("/events"),
|
||||
apiLogger.WithGroup("/events"),
|
||||
sq,
|
||||
authMiddleware,
|
||||
)
|
||||
accounts_api.Routes(
|
||||
api.Group("/accounts", authMiddleware.AuthenticateAndAddIdentityGin()),
|
||||
apiLogger.WithGroup("/accounts"),
|
||||
accts,
|
||||
unp.Group("/accounts"),
|
||||
)
|
||||
|
||||
// api webhooks (TODO: make a router for these)
|
||||
|
||||
@@ -104,7 +126,14 @@ func Router(
|
||||
},
|
||||
)
|
||||
|
||||
return r
|
||||
return &Router{
|
||||
Engine: r,
|
||||
sse: sq,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Router) RunSSE(ctx context.Context) error {
|
||||
return r.sse.Start(ctx)
|
||||
}
|
||||
|
||||
func fileServer(urlPrefix, dir string, beforeServe func(c *gin.Context)) gin.HandlerFunc {
|
||||
|
||||
Reference in New Issue
Block a user