diff --git a/cmd/broker/main.go b/cmd/broker/main.go index 4fd2fa2ac4..9287c0ca3d 100644 --- a/cmd/broker/main.go +++ b/cmd/broker/main.go @@ -456,13 +456,10 @@ func createAPI(router *httputil.Router, servicesConfig broker.ServicesConfig, pl router.Use(middleware.AddRegionToContext(cfg.DefaultRequestRegion)) router.Use(middleware.AddProviderToContext()) - for _, prefix := range []string{ - "/oauth/", // oauth2 handled by Ory - "/oauth/{region}/", // oauth2 handled by Ory with region - } { - router.PathPrefix(prefix) - broker.AttachRoutes(router, kymaEnvBroker, logger, cfg.Broker.Binding.CreateBindingTimeout) - } + + // oauth2 handled by Ory + prefixes := []string{"/oauth", "/oauth/{region}"} + broker.AttachRoutes(router, kymaEnvBroker, logger, cfg.Broker.Binding.CreateBindingTimeout, prefixes) respWriter := httputil.NewResponseWriter(logs, cfg.DevelopmentMode) runtimesInfoHandler := appinfo.NewRuntimeInfoHandler(db.Instances(), db.Operations(), defaultPlansConfig, cfg.DefaultRequestRegion, respWriter) diff --git a/internal/broker/server.go b/internal/broker/server.go index 943fa5b6f0..e1bbcb224a 100644 --- a/internal/broker/server.go +++ b/internal/broker/server.go @@ -22,25 +22,18 @@ func (h CreateBindingHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) } // copied from github.com/pivotal-cf/brokerapi/api.go -func AttachRoutes(router *httputil.Router, serviceBroker domain.ServiceBroker, logger *slog.Logger, createBindingTimeout time.Duration) *httputil.Router { +func AttachRoutes(router *httputil.Router, serviceBroker domain.ServiceBroker, logger *slog.Logger, createBindingTimeout time.Duration, prefixes []string) *httputil.Router { apiHandler := handlers.NewApiHandler(serviceBroker, logger) + deprovision := func(w http.ResponseWriter, req *http.Request) { req2 := req.WithContext(context.WithValue(req.Context(), "User-Agent", req.Header.Get("User-Agent"))) apiHandler.Deprovision(w, req2) } - router.HandleFunc("GET /v2/catalog", apiHandler.Catalog) - - router.HandleFunc("GET /v2/service_instances/{instance_id}", apiHandler.GetInstance) - router.HandleFunc("PUT /v2/service_instances/{instance_id}", apiHandler.Provision) - router.HandleFunc("DELETE /v2/service_instances/{instance_id}", deprovision) - router.HandleFunc("GET /v2/service_instances/{instance_id}/last_operation", apiHandler.LastOperation) - router.HandleFunc("PATCH /v2/service_instances/{instance_id}", apiHandler.Update) - - router.HandleFunc("GET /v2/service_instances/{instance_id}/service_bindings/{binding_id}", apiHandler.GetBinding) - router.Handle("PUT /v2/service_instances/{instance_id}/service_bindings/{binding_id}", http.TimeoutHandler(CreateBindingHandler{apiHandler.Bind}, createBindingTimeout, fmt.Sprintf("request timeout: time exceeded %s", createBindingTimeout))) - router.HandleFunc("DELETE /v2/service_instances/{instance_id}/service_bindings/{binding_id}", apiHandler.Unbind) - router.HandleFunc("GET /v2/service_instances/{instance_id}/service_bindings/{binding_id}/last_operation", apiHandler.LastBindingOperation) + prefixes = append(prefixes, "") + for _, prefix := range prefixes { + registerRoutesAndHandlers(router, &apiHandler, deprovision, createBindingTimeout, prefix) + } router.Use(middlewares.AddCorrelationIDToContext) apiVersionMiddleware := middlewares.APIVersionMiddleware{Logger: logger} @@ -51,3 +44,23 @@ func AttachRoutes(router *httputil.Router, serviceBroker domain.ServiceBroker, l return router } + +func registerRoutesAndHandlers(router *httputil.Router, apiHandler *handlers.APIHandler, deprovisionFunc func(w http.ResponseWriter, req *http.Request), createBindingTimeout time.Duration, pathPrefix string) { + router.HandleFunc(buildPathPattern(http.MethodGet, pathPrefix, "/v2/catalog"), apiHandler.Catalog) + + router.HandleFunc(buildPathPattern(http.MethodGet, pathPrefix, "/v2/service_instances/{instance_id}"), apiHandler.GetInstance) + router.HandleFunc(buildPathPattern(http.MethodPut, pathPrefix, "/v2/service_instances/{instance_id}"), apiHandler.Provision) + router.HandleFunc(buildPathPattern(http.MethodDelete, pathPrefix, "/v2/service_instances/{instance_id}"), deprovisionFunc) + router.HandleFunc(buildPathPattern(http.MethodGet, pathPrefix, "/v2/service_instances/{instance_id}/last_operation"), apiHandler.LastOperation) + router.HandleFunc(buildPathPattern(http.MethodPatch, pathPrefix, "/v2/service_instances/{instance_id}"), apiHandler.Update) + + router.HandleFunc(buildPathPattern(http.MethodGet, pathPrefix, "/v2/service_instances/{instance_id}/service_bindings/{binding_id}"), apiHandler.GetBinding) + router.Handle(buildPathPattern(http.MethodPut, pathPrefix, "/v2/service_instances/{instance_id}/service_bindings/{binding_id}"), http.TimeoutHandler(CreateBindingHandler{apiHandler.Bind}, createBindingTimeout, fmt.Sprintf("request timeout: time exceeded %s", createBindingTimeout))) + router.HandleFunc(buildPathPattern(http.MethodDelete, pathPrefix, "/v2/service_instances/{instance_id}/service_bindings/{binding_id}"), apiHandler.Unbind) + + router.HandleFunc(buildPathPattern(http.MethodGet, pathPrefix, "/v2/service_instances/{instance_id}/service_bindings/{binding_id}/last_operation"), apiHandler.LastBindingOperation) +} + +func buildPathPattern(httpMethod, prefix, path string) string { + return fmt.Sprintf("%s %s%s", httpMethod, prefix, path) +} diff --git a/internal/httputil/router.go b/internal/httputil/router.go index 2e7e231b04..0e89bd5394 100644 --- a/internal/httputil/router.go +++ b/internal/httputil/router.go @@ -1,7 +1,6 @@ package httputil import ( - "fmt" "net/http" "github.com/kyma-project/kyma-environment-broker/internal/middleware" @@ -27,13 +26,6 @@ func (r *Router) Use(middlewares ...middleware.MiddlewareFunc) { } } -func (r *Router) PathPrefix(prefix string) { - subrouter := http.NewServeMux() - pattern := fmt.Sprintf("/%s/", prefix) - subrouter.Handle(pattern, r) - r.subrouters = append(r.subrouters, subrouter) -} - func (r *Router) Handle(pattern string, handler http.Handler) { for i := len(r.middlewares) - 1; i >= 0; i-- { handler = r.middlewares[i](handler)