organized api packages into a unified constructor
This commit is contained in:
@@ -0,0 +1,56 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
|
||||||
|
"ruben/inventory2/internal/domains/accounts"
|
||||||
|
etsy_platform "ruben/inventory2/internal/domains/platforms/etsy"
|
||||||
|
"ruben/inventory2/internal/domains/raw_events"
|
||||||
|
"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"
|
||||||
|
"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"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Routes(
|
||||||
|
r *gin.RouterGroup,
|
||||||
|
logger *logging.Logger,
|
||||||
|
auth *middleware.Auth,
|
||||||
|
sq *sse.Queue,
|
||||||
|
accts *accounts.Store,
|
||||||
|
unp *middleware.UpdateNotificationPublisher,
|
||||||
|
rawEvents *raw_events.Store,
|
||||||
|
etsy *etsy_platform.Platform,
|
||||||
|
) {
|
||||||
|
auth_api.Routes(
|
||||||
|
r.Group("/auth"),
|
||||||
|
logger.WithGroup("/auth"),
|
||||||
|
auth.GetAuthenticator(),
|
||||||
|
)
|
||||||
|
sse_api.Routes(
|
||||||
|
r.Group("/events", auth.Authenticate()),
|
||||||
|
logger.WithGroup("/events"),
|
||||||
|
sq,
|
||||||
|
)
|
||||||
|
accounts_api.Routes(
|
||||||
|
r.Group("/accounts", auth.Authenticate()),
|
||||||
|
logger.WithGroup("/accounts"),
|
||||||
|
accts,
|
||||||
|
unp.Group("/accounts"),
|
||||||
|
)
|
||||||
|
webhooks.Routes(
|
||||||
|
r.Group("/webhooks"),
|
||||||
|
logger.WithGroup("/webhooks"),
|
||||||
|
rawEvents,
|
||||||
|
etsy,
|
||||||
|
webhooks.Config{
|
||||||
|
Etsy: etsy_webhooks.Config{
|
||||||
|
OAuthRedirectURIWithAcctIDParam: "/oauth/accounts/:acctID/auth_code",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -49,6 +49,10 @@ func NewAuth(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (a *Auth) GetAuthenticator() *authentication.Authenticator {
|
||||||
|
return a.auth
|
||||||
|
}
|
||||||
|
|
||||||
// Identify will add an Identity to the context that can then be retrieved via GetIdentity.
|
// Identify will add an Identity to the context that can then be retrieved via GetIdentity.
|
||||||
func (a *Auth) Identify(c *gin.Context) {
|
func (a *Auth) Identify(c *gin.Context) {
|
||||||
if err := a.addIdentity(c); err != nil {
|
if err := a.addIdentity(c); err != nil {
|
||||||
|
|||||||
@@ -29,12 +29,17 @@ func NewUpdateNotificationPublisher(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Trim produces an *UpdateNotificationPublisher with the pathPattern
|
||||||
|
// trimmed from events otherwise published by p.
|
||||||
func (p *UpdateNotificationPublisher) Trim(pathPattern string) *UpdateNotificationPublisher {
|
func (p *UpdateNotificationPublisher) Trim(pathPattern string) *UpdateNotificationPublisher {
|
||||||
p2 := *p
|
p2 := *p
|
||||||
p2.trimBasePath = path.Join(p2.trimBasePath, pathPattern)
|
p2.trimBasePath = path.Join(p2.trimBasePath, pathPattern)
|
||||||
return &p2
|
return &p2
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Group produces an *UpdateNotificationPublisher with the pathPattern
|
||||||
|
// appended to the base path used by p, if any, in publishing events.
|
||||||
|
// If no base path was set to p, then pathPattern becomes the base path.
|
||||||
func (p *UpdateNotificationPublisher) Group(pathPattern string) *UpdateNotificationPublisher {
|
func (p *UpdateNotificationPublisher) Group(pathPattern string) *UpdateNotificationPublisher {
|
||||||
p2 := *p
|
p2 := *p
|
||||||
p2.basePathPattern = path.Join(p2.basePathPattern, pathPattern)
|
p2.basePathPattern = path.Join(p2.basePathPattern, pathPattern)
|
||||||
|
|||||||
@@ -13,19 +13,13 @@ 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/logging"
|
"ruben/inventory2/internal/logging"
|
||||||
accounts_api "ruben/inventory2/internal/server/api/accounts"
|
"ruben/inventory2/internal/server/api"
|
||||||
auth_api "ruben/inventory2/internal/server/api/auth"
|
|
||||||
sse_api "ruben/inventory2/internal/server/api/sse"
|
|
||||||
templates_api "ruben/inventory2/internal/server/api/templates"
|
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/middleware"
|
||||||
"ruben/inventory2/internal/server/response"
|
"ruben/inventory2/internal/server/response"
|
||||||
"ruben/inventory2/internal/server/sse"
|
"ruben/inventory2/internal/server/sse"
|
||||||
)
|
)
|
||||||
|
|
||||||
// r gin.IRouter, // TODO: use this everywhere
|
|
||||||
|
|
||||||
type Router struct {
|
type Router struct {
|
||||||
*gin.Engine
|
*gin.Engine
|
||||||
sse *sse.Queue
|
sse *sse.Queue
|
||||||
@@ -84,9 +78,6 @@ func NewRouter(
|
|||||||
|
|
||||||
// api endpoints
|
// api endpoints
|
||||||
|
|
||||||
api := r.Group("/api")
|
|
||||||
apiLogger := logger.WithGroup("/api")
|
|
||||||
|
|
||||||
// sse setup
|
// sse setup
|
||||||
sq := sse.NewQueue()
|
sq := sse.NewQueue()
|
||||||
unp := middleware.NewUpdateNotificationPublisher(
|
unp := middleware.NewUpdateNotificationPublisher(
|
||||||
@@ -94,33 +85,15 @@ func NewRouter(
|
|||||||
sq,
|
sq,
|
||||||
).Trim("/api")
|
).Trim("/api")
|
||||||
|
|
||||||
// TODO: group these into a single unifieds 'apis' object/package?
|
api.Routes(
|
||||||
auth_api.Routes(
|
r.Group("/api"),
|
||||||
api.Group("/auth"),
|
logger.WithGroup("/api"),
|
||||||
apiLogger.WithGroup("/auth"),
|
authM,
|
||||||
auth,
|
|
||||||
)
|
|
||||||
sse_api.Routes(
|
|
||||||
api.Group("/events", authM.Authenticate()),
|
|
||||||
apiLogger.WithGroup("/events"),
|
|
||||||
sq,
|
sq,
|
||||||
)
|
|
||||||
accounts_api.Routes(
|
|
||||||
api.Group("/accounts", authM.Authenticate()),
|
|
||||||
apiLogger.WithGroup("/accounts"),
|
|
||||||
accts,
|
accts,
|
||||||
unp.Group("/accounts"),
|
unp,
|
||||||
)
|
|
||||||
webhooks.Routes(
|
|
||||||
api.Group("/webhooks"),
|
|
||||||
apiLogger.WithGroup("/webhooks"),
|
|
||||||
rawEvents,
|
rawEvents,
|
||||||
etsy,
|
etsy,
|
||||||
webhooks.Config{
|
|
||||||
Etsy: etsy_webhooks.Config{
|
|
||||||
OAuthRedirectURIWithAcctIDParam: "/oauth/accounts/:acctID/auth_code",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
)
|
)
|
||||||
|
|
||||||
return &Router{
|
return &Router{
|
||||||
|
|||||||
Reference in New Issue
Block a user