forked from go-swagger/go-swagger
/
configure_petstore.go
203 lines (172 loc) · 7.55 KB
/
configure_petstore.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// Code generated by go-swagger; DO NOT EDIT.
package restapi
import (
"context"
"encoding/json"
"fmt"
"net/http"
"github.com/go-openapi/errors"
"github.com/circl-dev/runtime"
"github.com/circl-dev/runtime/middleware"
"github.com/circl-dev/runtime/security"
"github.com/circl-dev/loads"
"github.com/circl-dev/go-swagger/examples/contributed-templates/stratoscale/restapi/operations"
"github.com/circl-dev/go-swagger/examples/contributed-templates/stratoscale/restapi/operations/pet"
"github.com/circl-dev/go-swagger/examples/contributed-templates/stratoscale/restapi/operations/store"
)
type contextKey string
const AuthKey contextKey = "Auth"
//go:generate mockery -name PetAPI -inpkg
/* PetAPI */
type PetAPI interface {
/* PetCreate Add a new pet to the store */
PetCreate(ctx context.Context, params pet.PetCreateParams) middleware.Responder
/* PetDelete Deletes a pet */
PetDelete(ctx context.Context, params pet.PetDeleteParams) middleware.Responder
/* PetGet Get pet by it's ID */
PetGet(ctx context.Context, params pet.PetGetParams) middleware.Responder
/* PetList List pets */
PetList(ctx context.Context, params pet.PetListParams) middleware.Responder
/* PetUpdate Update an existing pet */
PetUpdate(ctx context.Context, params pet.PetUpdateParams) middleware.Responder
/* PetUploadImage uploads an image */
PetUploadImage(ctx context.Context, params pet.PetUploadImageParams) middleware.Responder
}
//go:generate mockery -name StoreAPI -inpkg
/* StoreAPI */
type StoreAPI interface {
/* InventoryGet Returns pet inventories by status */
InventoryGet(ctx context.Context, params store.InventoryGetParams) middleware.Responder
/* OrderCreate Place an order for a pet */
OrderCreate(ctx context.Context, params store.OrderCreateParams) middleware.Responder
/* OrderDelete Delete purchase order by ID */
OrderDelete(ctx context.Context, params store.OrderDeleteParams) middleware.Responder
/* OrderGet Find purchase order by ID */
OrderGet(ctx context.Context, params store.OrderGetParams) middleware.Responder
}
// Config is configuration for Handler
type Config struct {
PetAPI
StoreAPI
Logger func(string, ...interface{})
// InnerMiddleware is for the handler executors. These do not apply to the swagger.json document.
// The middleware executes after routing but before authentication, binding and validation
InnerMiddleware func(http.Handler) http.Handler
// Authorizer is used to authorize a request after the Auth function was called using the "Auth*" functions
// and the principal was stored in the context in the "AuthKey" context value.
Authorizer func(*http.Request) error
// AuthRoles Applies when the "X-Auth-Roles" header is set
AuthRoles func(token string) (interface{}, error)
// Authenticator to use for all APIKey authentication
APIKeyAuthenticator func(string, string, security.TokenAuthentication) runtime.Authenticator
// Authenticator to use for all Bearer authentication
BasicAuthenticator func(security.UserPassAuthentication) runtime.Authenticator
// Authenticator to use for all Basic authentication
BearerAuthenticator func(string, security.ScopedTokenAuthentication) runtime.Authenticator
}
// Handler returns an http.Handler given the handler configuration
// It mounts all the business logic implementers in the right routing.
func Handler(c Config) (http.Handler, error) {
h, _, err := HandlerAPI(c)
return h, err
}
// HandlerAPI returns an http.Handler given the handler configuration
// and the corresponding *Petstore instance.
// It mounts all the business logic implementers in the right routing.
func HandlerAPI(c Config) (http.Handler, *operations.PetstoreAPI, error) {
spec, err := loads.Analyzed(swaggerCopy(SwaggerJSON), "")
if err != nil {
return nil, nil, fmt.Errorf("analyze swagger: %v", err)
}
api := operations.NewPetstoreAPI(spec)
api.ServeError = errors.ServeError
api.Logger = c.Logger
if c.APIKeyAuthenticator != nil {
api.APIKeyAuthenticator = c.APIKeyAuthenticator
}
if c.BasicAuthenticator != nil {
api.BasicAuthenticator = c.BasicAuthenticator
}
if c.BearerAuthenticator != nil {
api.BearerAuthenticator = c.BearerAuthenticator
}
api.JSONConsumer = runtime.JSONConsumer()
api.JSONProducer = runtime.JSONProducer()
api.RolesAuth = func(token string) (interface{}, error) {
if c.AuthRoles == nil {
return token, nil
}
return c.AuthRoles(token)
}
api.APIAuthorizer = authorizer(c.Authorizer)
api.StoreInventoryGetHandler = store.InventoryGetHandlerFunc(func(params store.InventoryGetParams, principal interface{}) middleware.Responder {
ctx := params.HTTPRequest.Context()
ctx = storeAuth(ctx, principal)
return c.StoreAPI.InventoryGet(ctx, params)
})
api.StoreOrderCreateHandler = store.OrderCreateHandlerFunc(func(params store.OrderCreateParams, principal interface{}) middleware.Responder {
ctx := params.HTTPRequest.Context()
ctx = storeAuth(ctx, principal)
return c.StoreAPI.OrderCreate(ctx, params)
})
api.StoreOrderDeleteHandler = store.OrderDeleteHandlerFunc(func(params store.OrderDeleteParams, principal interface{}) middleware.Responder {
ctx := params.HTTPRequest.Context()
ctx = storeAuth(ctx, principal)
return c.StoreAPI.OrderDelete(ctx, params)
})
api.StoreOrderGetHandler = store.OrderGetHandlerFunc(func(params store.OrderGetParams, principal interface{}) middleware.Responder {
ctx := params.HTTPRequest.Context()
ctx = storeAuth(ctx, principal)
return c.StoreAPI.OrderGet(ctx, params)
})
api.PetPetCreateHandler = pet.PetCreateHandlerFunc(func(params pet.PetCreateParams, principal interface{}) middleware.Responder {
ctx := params.HTTPRequest.Context()
ctx = storeAuth(ctx, principal)
return c.PetAPI.PetCreate(ctx, params)
})
api.PetPetDeleteHandler = pet.PetDeleteHandlerFunc(func(params pet.PetDeleteParams, principal interface{}) middleware.Responder {
ctx := params.HTTPRequest.Context()
ctx = storeAuth(ctx, principal)
return c.PetAPI.PetDelete(ctx, params)
})
api.PetPetGetHandler = pet.PetGetHandlerFunc(func(params pet.PetGetParams, principal interface{}) middleware.Responder {
ctx := params.HTTPRequest.Context()
ctx = storeAuth(ctx, principal)
return c.PetAPI.PetGet(ctx, params)
})
api.PetPetListHandler = pet.PetListHandlerFunc(func(params pet.PetListParams, principal interface{}) middleware.Responder {
ctx := params.HTTPRequest.Context()
ctx = storeAuth(ctx, principal)
return c.PetAPI.PetList(ctx, params)
})
api.PetPetUpdateHandler = pet.PetUpdateHandlerFunc(func(params pet.PetUpdateParams, principal interface{}) middleware.Responder {
ctx := params.HTTPRequest.Context()
ctx = storeAuth(ctx, principal)
return c.PetAPI.PetUpdate(ctx, params)
})
api.PetPetUploadImageHandler = pet.PetUploadImageHandlerFunc(func(params pet.PetUploadImageParams, principal interface{}) middleware.Responder {
ctx := params.HTTPRequest.Context()
ctx = storeAuth(ctx, principal)
return c.PetAPI.PetUploadImage(ctx, params)
})
api.ServerShutdown = func() {}
return api.Serve(c.InnerMiddleware), api, nil
}
// swaggerCopy copies the swagger json to prevent data races in runtime
func swaggerCopy(orig json.RawMessage) json.RawMessage {
c := make(json.RawMessage, len(orig))
copy(c, orig)
return c
}
// authorizer is a helper function to implement the runtime.Authorizer interface.
type authorizer func(*http.Request) error
func (a authorizer) Authorize(req *http.Request, principal interface{}) error {
if a == nil {
return nil
}
ctx := storeAuth(req.Context(), principal)
return a(req.WithContext(ctx))
}
func storeAuth(ctx context.Context, principal interface{}) context.Context {
return context.WithValue(ctx, AuthKey, principal)
}