Skip to content

Commit

Permalink
Register patterns and handlers in one router
Browse files Browse the repository at this point in the history
  • Loading branch information
szwedm committed Dec 3, 2024
1 parent bb18120 commit 2a481c5
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 28 deletions.
11 changes: 4 additions & 7 deletions cmd/broker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
39 changes: 26 additions & 13 deletions internal/broker/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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)
}
8 changes: 0 additions & 8 deletions internal/httputil/router.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package httputil

import (
"fmt"
"net/http"

"github.com/kyma-project/kyma-environment-broker/internal/middleware"
Expand All @@ -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)
Expand Down

0 comments on commit 2a481c5

Please sign in to comment.