@@ -19,40 +19,103 @@ import (
1919 "log/slog"
2020 "net/http"
2121
22- "github.com/go-chi/chi/v5"
22+ "github.com/pivotal-cf/brokerapi/v11/internal/middleware"
23+
24+ "github.com/pivotal-cf/brokerapi/v11/auth"
25+ "github.com/pivotal-cf/brokerapi/v11/domain"
2326 "github.com/pivotal-cf/brokerapi/v11/handlers"
27+ "github.com/pivotal-cf/brokerapi/v11/middlewares"
2428)
2529
2630type BrokerCredentials struct {
2731 Username string
2832 Password string
2933}
3034
31- func New (serviceBroker ServiceBroker , logger * slog.Logger , brokerCredentials BrokerCredentials ) http.Handler {
32- return NewWithOptions (serviceBroker , logger , WithBrokerCredentials (brokerCredentials ))
35+ func New (serviceBroker domain.ServiceBroker , logger * slog.Logger , brokerCredentials BrokerCredentials , opts ... Option ) http.Handler {
36+ return NewWithOptions (serviceBroker , logger , append ([]Option {WithBrokerCredentials (brokerCredentials )}, opts ... )... )
37+ }
38+
39+ func NewWithOptions (serviceBroker domain.ServiceBroker , logger * slog.Logger , opts ... Option ) http.Handler {
40+ var cfg config
41+ WithOptions (opts ... )(& cfg )
42+
43+ mw := append (append (cfg .authMiddleware , defaultMiddleware (logger )... ), cfg .additionalMiddleware ... )
44+ r := router (serviceBroker , logger )
45+
46+ return middleware .Use (r , mw ... )
3347}
3448
35- func NewWithCustomAuth (serviceBroker ServiceBroker , logger * slog.Logger , authMiddleware middlewareFunc ) http.Handler {
49+ func NewWithCustomAuth (serviceBroker domain. ServiceBroker , logger * slog.Logger , authMiddleware func ( handler http. Handler ) http. Handler ) http.Handler {
3650 return NewWithOptions (serviceBroker , logger , WithCustomAuth (authMiddleware ))
3751}
3852
39- func AttachRoutes (router chi.Router , serviceBroker ServiceBroker , logger * slog.Logger ) {
40- attachRoutes (router , serviceBroker , logger )
53+ type config struct {
54+ authMiddleware []func (http.Handler ) http.Handler
55+ additionalMiddleware []func (http.Handler ) http.Handler
56+ }
57+
58+ type Option func (* config )
59+
60+ func WithBrokerCredentials (brokerCredentials BrokerCredentials ) Option {
61+ return func (c * config ) {
62+ c .authMiddleware = append (c .authMiddleware , auth .NewWrapper (brokerCredentials .Username , brokerCredentials .Password ).Wrap )
63+ }
64+ }
65+
66+ // WithCustomAuth adds the specified middleware *before* any other middleware.
67+ // Despite the name, any middleware can be added whether nor not it has anything to do with authentication.
68+ // But `WithAdditionalMiddleware()` may be a better choice if the middleware is not related to authentication.
69+ // Can be called multiple times.
70+ func WithCustomAuth (authMiddleware func (handler http.Handler ) http.Handler ) Option {
71+ return func (c * config ) {
72+ c .authMiddleware = append (c .authMiddleware , authMiddleware )
73+ }
74+ }
75+
76+ // WithAdditionalMiddleware adds the specified middleware *after* the default middleware.
77+ // Can be called multiple times.
78+ func WithAdditionalMiddleware (m func (http.Handler ) http.Handler ) Option {
79+ return func (c * config ) {
80+ c .additionalMiddleware = append (c .additionalMiddleware , m )
81+ }
82+ }
83+
84+ func WithOptions (opts ... Option ) Option {
85+ return func (c * config ) {
86+ for _ , o := range opts {
87+ o (c )
88+ }
89+ }
4190}
4291
43- func attachRoutes ( router chi. Router , serviceBroker ServiceBroker , logger * slog.Logger ) {
92+ func router ( serviceBroker ServiceBroker , logger * slog.Logger ) http. Handler {
4493 apiHandler := handlers .NewApiHandler (serviceBroker , logger )
45- router .Get ("/v2/catalog" , apiHandler .Catalog )
94+ r := http .NewServeMux ()
95+ r .HandleFunc ("GET /v2/catalog" , apiHandler .Catalog )
4696
47- router .Get ("/v2/service_instances/{instance_id}" , apiHandler .GetInstance )
48- router .Put ("/v2/service_instances/{instance_id}" , apiHandler .Provision )
49- router .Delete ("/v2/service_instances/{instance_id}" , apiHandler .Deprovision )
50- router .Get ("/v2/service_instances/{instance_id}/last_operation" , apiHandler .LastOperation )
51- router .Patch ("/v2/service_instances/{instance_id}" , apiHandler .Update )
97+ r .HandleFunc ("PUT /v2/service_instances/{instance_id}" , apiHandler .Provision )
98+ r .HandleFunc ("GET /v2/service_instances/{instance_id}" , apiHandler .GetInstance )
99+ r .HandleFunc ("PATCH /v2/service_instances/{instance_id}" , apiHandler .Update )
100+ r .HandleFunc ("DELETE /v2/service_instances/{instance_id}" , apiHandler .Deprovision )
52101
53- router .Get ("/v2/service_instances/{instance_id}/service_bindings/{binding_id}" , apiHandler .GetBinding )
54- router .Put ("/v2/service_instances/{instance_id}/service_bindings/{binding_id}" , apiHandler .Bind )
55- router .Delete ("/v2/service_instances/{instance_id}/service_bindings/{binding_id}" , apiHandler .Unbind )
102+ r .HandleFunc ("GET /v2/service_instances/{instance_id}/last_operation" , apiHandler .LastOperation )
103+
104+ r .HandleFunc ("PUT /v2/service_instances/{instance_id}/service_bindings/{binding_id}" , apiHandler .Bind )
105+ r .HandleFunc ("GET /v2/service_instances/{instance_id}/service_bindings/{binding_id}" , apiHandler .GetBinding )
106+ r .HandleFunc ("DELETE /v2/service_instances/{instance_id}/service_bindings/{binding_id}" , apiHandler .Unbind )
107+
108+ r .HandleFunc ("GET /v2/service_instances/{instance_id}/service_bindings/{binding_id}/last_operation" , apiHandler .LastBindingOperation )
109+
110+ return r
111+ }
56112
57- router .Get ("/v2/service_instances/{instance_id}/service_bindings/{binding_id}/last_operation" , apiHandler .LastBindingOperation )
113+ func defaultMiddleware (logger * slog.Logger ) []func (http.Handler ) http.Handler {
114+ return []func (http.Handler ) http.Handler {
115+ middlewares.APIVersionMiddleware {Logger : logger }.ValidateAPIVersionHdr ,
116+ middlewares .AddCorrelationIDToContext ,
117+ middlewares .AddOriginatingIdentityToContext ,
118+ middlewares .AddInfoLocationToContext ,
119+ middlewares .AddRequestIdentityToContext ,
120+ }
58121}
0 commit comments