@@ -4,24 +4,46 @@ import (
44 "net/http"
55
66 "github.com/gin-gonic/gin"
7- "github.com/hookdeck/EventKit/internal/config"
87 "github.com/hookdeck/EventKit/internal/destination"
9- "github.com/hookdeck/EventKit/internal/redis"
10- "github.com/uptrace/opentelemetry-go-extra/otelzap"
8+ "github.com/hookdeck/EventKit/internal/tenant"
119 "go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin"
1210)
1311
14- func NewRouter (cfg * config.Config , logger * otelzap.Logger , redisClient * redis.Client ) http.Handler {
12+ type RouterConfig struct {
13+ Hostname string
14+ APIKey string
15+ JWTSecret string
16+ }
17+
18+ func NewRouter (
19+ cfg RouterConfig ,
20+ tenantHandlers * tenant.TenantHandlers ,
21+ destinationHandlers * destination.DestinationHandlers ,
22+ ) http.Handler {
1523 r := gin .Default ()
1624 r .Use (otelgin .Middleware (cfg .Hostname ))
17- r .Use (apiKeyAuthMiddleware (cfg .APIKey ))
1825
1926 r .GET ("/healthz" , func (c * gin.Context ) {
20- logger .Ctx (c .Request .Context ()).Info ("health check" )
2127 c .Status (http .StatusOK )
2228 })
2329
24- destinationHandlers := destination .NewHandlers (redisClient )
30+ // Admin router is a router group with the API key auth mechanism.
31+ adminRouter := r .Group ("/" , apiKeyAuthMiddleware (cfg .APIKey ))
32+
33+ adminRouter .PUT ("/:tenantID" , tenantHandlers .Upsert )
34+ adminRouter .GET ("/:tenantID/portal" , tenantHandlers .RetrievePortal )
35+
36+ // Tenant router is a router group that accepts either
37+ // - a tenant's JWT token OR
38+ // - the preconfigured API key
39+ //
40+ // If the EventKit service deployment isn't configured with an API key, then
41+ // it's assumed that the service runs in a secure environment
42+ // and the JWT check is NOT necessary either.
43+ tenantRouter := r .Group ("/" , apiKeyOrTenantJWTAuthMiddleware (cfg .APIKey , cfg .JWTSecret ))
44+
45+ tenantRouter .GET ("/:tenantID" , tenantHandlers .Retrieve )
46+ tenantRouter .DELETE ("/:tenantID" , tenantHandlers .Delete )
2547
2648 r .GET ("/destinations" , destinationHandlers .List )
2749 r .POST ("/destinations" , destinationHandlers .Create )
0 commit comments