-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathapi.go
121 lines (95 loc) · 4.57 KB
/
api.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright (C) 2015-Present Pivotal Software, Inc. All rights reserved.
// This program and the accompanying materials are made available under
// the terms of the under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package brokerapi
import (
"log/slog"
"net/http"
"github.com/pivotal-cf/brokerapi/v12/internal/middleware"
"github.com/pivotal-cf/brokerapi/v12/auth"
"github.com/pivotal-cf/brokerapi/v12/domain"
"github.com/pivotal-cf/brokerapi/v12/handlers"
"github.com/pivotal-cf/brokerapi/v12/middlewares"
)
type BrokerCredentials struct {
Username string
Password string
}
func New(serviceBroker domain.ServiceBroker, logger *slog.Logger, brokerCredentials BrokerCredentials, opts ...Option) http.Handler {
return NewWithOptions(serviceBroker, logger, append([]Option{WithBrokerCredentials(brokerCredentials)}, opts...)...)
}
func NewWithOptions(serviceBroker domain.ServiceBroker, logger *slog.Logger, opts ...Option) http.Handler {
var cfg config
WithOptions(opts...)(&cfg)
mw := append(append(cfg.authMiddleware, defaultMiddleware(logger)...), cfg.additionalMiddleware...)
r := router(serviceBroker, logger)
return middleware.Use(r, mw...)
}
func NewWithCustomAuth(serviceBroker domain.ServiceBroker, logger *slog.Logger, authMiddleware func(handler http.Handler) http.Handler) http.Handler {
return NewWithOptions(serviceBroker, logger, WithCustomAuth(authMiddleware))
}
type config struct {
authMiddleware []func(http.Handler) http.Handler
additionalMiddleware []func(http.Handler) http.Handler
}
type Option func(*config)
func WithBrokerCredentials(brokerCredentials BrokerCredentials) Option {
return func(c *config) {
c.authMiddleware = append(c.authMiddleware, auth.NewWrapper(brokerCredentials.Username, brokerCredentials.Password).Wrap)
}
}
// WithCustomAuth adds the specified middleware *before* any other middleware.
// Despite the name, any middleware can be added whether nor not it has anything to do with authentication.
// But `WithAdditionalMiddleware()` may be a better choice if the middleware is not related to authentication.
// Can be called multiple times.
func WithCustomAuth(authMiddleware func(handler http.Handler) http.Handler) Option {
return func(c *config) {
c.authMiddleware = append(c.authMiddleware, authMiddleware)
}
}
// WithAdditionalMiddleware adds the specified middleware *after* the default middleware.
// Can be called multiple times.
func WithAdditionalMiddleware(m func(http.Handler) http.Handler) Option {
return func(c *config) {
c.additionalMiddleware = append(c.additionalMiddleware, m)
}
}
func WithOptions(opts ...Option) Option {
return func(c *config) {
for _, o := range opts {
o(c)
}
}
}
func router(serviceBroker ServiceBroker, logger *slog.Logger) http.Handler {
apiHandler := handlers.NewApiHandler(serviceBroker, logger)
r := http.NewServeMux()
r.HandleFunc("GET /v2/catalog", apiHandler.Catalog)
r.HandleFunc("PUT /v2/service_instances/{instance_id}", apiHandler.Provision)
r.HandleFunc("GET /v2/service_instances/{instance_id}", apiHandler.GetInstance)
r.HandleFunc("PATCH /v2/service_instances/{instance_id}", apiHandler.Update)
r.HandleFunc("DELETE /v2/service_instances/{instance_id}", apiHandler.Deprovision)
r.HandleFunc("GET /v2/service_instances/{instance_id}/last_operation", apiHandler.LastOperation)
r.HandleFunc("PUT /v2/service_instances/{instance_id}/service_bindings/{binding_id}", apiHandler.Bind)
r.HandleFunc("GET /v2/service_instances/{instance_id}/service_bindings/{binding_id}", apiHandler.GetBinding)
r.HandleFunc("DELETE /v2/service_instances/{instance_id}/service_bindings/{binding_id}", apiHandler.Unbind)
r.HandleFunc("GET /v2/service_instances/{instance_id}/service_bindings/{binding_id}/last_operation", apiHandler.LastBindingOperation)
return r
}
func defaultMiddleware(logger *slog.Logger) []func(http.Handler) http.Handler {
return []func(http.Handler) http.Handler{
middlewares.APIVersionMiddleware{Logger: logger}.ValidateAPIVersionHdr,
middlewares.AddCorrelationIDToContext,
middlewares.AddOriginatingIdentityToContext,
middlewares.AddInfoLocationToContext,
middlewares.AddRequestIdentityToContext,
}
}