Skip to content

Commit

Permalink
backend: add middleware support to ConfigConnectRPC hook (#934)
Browse files Browse the repository at this point in the history
  • Loading branch information
weeco authored Nov 24, 2023
1 parent b00af36 commit 465a3e5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
4 changes: 4 additions & 0 deletions backend/pkg/api/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ type ConfigConnectRPCResponse struct {

// Instructs OSS to register these services in addition to the OSS ones
AdditionalServices []ConnectService

// HTTPMiddlewares are middlewares that shall be used for the router
// that serves all ConnectRPC requests.
HTTPMiddlewares []func(http.Handler) http.Handler
}

// ConnectService is a Connect handler along with its metadata
Expand Down
12 changes: 10 additions & 2 deletions backend/pkg/api/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,19 @@ func (api *API) setupConnectWithGRPCGateway(r chi.Router) {
},
}),
)
r.Mount("/v1alpha1", gwMux) // Dataplane API

// Call Hook
hookOutput := api.Hooks.Route.ConfigConnectRPC(ConfigConnectRPCRequest{
BaseInterceptors: baseInterceptors,
GRPCGatewayMux: gwMux,
})

// Use HTTP Middlewares that are configured by the Hook
if len(hookOutput.HTTPMiddlewares) > 0 {
r.Use(hookOutput.HTTPMiddlewares...)
}
r.Mount("/v1alpha1", gwMux) // Dataplane API

// Create OSS Connect handlers only after calling hook. We need the hook output's final list of interceptors.
userSvc := apiusersvc.NewService(api.Cfg, api.Logger.Named("user_service"), api.RedpandaSvc, api.ConsoleSvc, api.Hooks.Authorization.IsProtectedKafkaUser)
aclSvc := apiaclsvc.NewService(api.Cfg, api.Logger.Named("kafka_service"), api.ConsoleSvc)
Expand Down Expand Up @@ -152,7 +157,10 @@ func (api *API) routes() *chi.Mux {
MaxAge: 300, // Maximum value not ignored by any of major browsers
}))

api.setupConnectWithGRPCGateway(baseRouter)
// Fork a new router so that we can inject middlewares that are specific to the Connect API
baseRouter.Group(func(router chi.Router) {
api.setupConnectWithGRPCGateway(router)
})

baseRouter.Group(func(router chi.Router) {
// Init middlewares - Do set up of any shared/third-party middleware and handlers
Expand Down

0 comments on commit 465a3e5

Please sign in to comment.