github.com/thetreep/go-swagger@v0.0.0-20240223100711-35af64f14f01/examples/contributed-templates/stratoscale/restapi/configure_petstore.go (about) 1 // Code generated by go-swagger; DO NOT EDIT. 2 3 package restapi 4 5 import ( 6 "context" 7 "encoding/json" 8 "fmt" 9 "net/http" 10 11 "github.com/go-openapi/errors" 12 "github.com/go-openapi/loads" 13 "github.com/go-openapi/runtime" 14 "github.com/go-openapi/runtime/middleware" 15 "github.com/go-openapi/runtime/security" 16 17 "github.com/thetreep/go-swagger/examples/contributed-templates/stratoscale/restapi/operations" 18 "github.com/thetreep/go-swagger/examples/contributed-templates/stratoscale/restapi/operations/pet" 19 "github.com/thetreep/go-swagger/examples/contributed-templates/stratoscale/restapi/operations/store" 20 ) 21 22 type contextKey string 23 24 const AuthKey contextKey = "Auth" 25 26 //go:generate mockery --name PetAPI --inpackage 27 28 /* PetAPI */ 29 type PetAPI interface { 30 /* PetCreate Add a new pet to the store */ 31 PetCreate(ctx context.Context, params pet.PetCreateParams) middleware.Responder 32 33 /* PetDelete Deletes a pet */ 34 PetDelete(ctx context.Context, params pet.PetDeleteParams) middleware.Responder 35 36 /* PetGet Get pet by it's ID */ 37 PetGet(ctx context.Context, params pet.PetGetParams) middleware.Responder 38 39 /* PetList List pets */ 40 PetList(ctx context.Context, params pet.PetListParams) middleware.Responder 41 42 /* PetUpdate Update an existing pet */ 43 PetUpdate(ctx context.Context, params pet.PetUpdateParams) middleware.Responder 44 45 /* PetUploadImage uploads an image */ 46 PetUploadImage(ctx context.Context, params pet.PetUploadImageParams) middleware.Responder 47 } 48 49 //go:generate mockery --name StoreAPI --inpackage 50 51 /* StoreAPI */ 52 type StoreAPI interface { 53 /* InventoryGet Returns pet inventories by status */ 54 InventoryGet(ctx context.Context, params store.InventoryGetParams) middleware.Responder 55 56 /* OrderCreate Place an order for a pet */ 57 OrderCreate(ctx context.Context, params store.OrderCreateParams) middleware.Responder 58 59 /* OrderDelete Delete purchase order by ID */ 60 OrderDelete(ctx context.Context, params store.OrderDeleteParams) middleware.Responder 61 62 /* OrderGet Find purchase order by ID */ 63 OrderGet(ctx context.Context, params store.OrderGetParams) middleware.Responder 64 } 65 66 // Config is configuration for Handler 67 type Config struct { 68 PetAPI 69 StoreAPI 70 Logger func(string, ...interface{}) 71 // InnerMiddleware is for the handler executors. These do not apply to the swagger.json document. 72 // The middleware executes after routing but before authentication, binding and validation 73 InnerMiddleware func(http.Handler) http.Handler 74 75 // Authorizer is used to authorize a request after the Auth function was called using the "Auth*" functions 76 // and the principal was stored in the context in the "AuthKey" context value. 77 Authorizer func(*http.Request) error 78 79 // AuthRoles Applies when the "X-Auth-Roles" header is set 80 AuthRoles func(token string) (interface{}, error) 81 82 // Authenticator to use for all APIKey authentication 83 APIKeyAuthenticator func(string, string, security.TokenAuthentication) runtime.Authenticator 84 // Authenticator to use for all Bearer authentication 85 BasicAuthenticator func(security.UserPassAuthentication) runtime.Authenticator 86 // Authenticator to use for all Basic authentication 87 BearerAuthenticator func(string, security.ScopedTokenAuthentication) runtime.Authenticator 88 89 // JSONConsumer is a json consumer that will replace the default if not nil. 90 JSONConsumer runtime.Consumer 91 } 92 93 // Handler returns an http.Handler given the handler configuration 94 // It mounts all the business logic implementers in the right routing. 95 func Handler(c Config) (http.Handler, error) { 96 h, _, err := HandlerAPI(c) 97 return h, err 98 } 99 100 // HandlerAPI returns an http.Handler given the handler configuration 101 // and the corresponding *Petstore instance. 102 // It mounts all the business logic implementers in the right routing. 103 func HandlerAPI(c Config) (http.Handler, *operations.PetstoreAPI, error) { 104 spec, err := loads.Analyzed(swaggerCopy(SwaggerJSON), "") 105 if err != nil { 106 return nil, nil, fmt.Errorf("analyze swagger: %v", err) 107 } 108 api := operations.NewPetstoreAPI(spec) 109 api.ServeError = errors.ServeError 110 api.Logger = c.Logger 111 112 if c.APIKeyAuthenticator != nil { 113 api.APIKeyAuthenticator = c.APIKeyAuthenticator 114 } 115 if c.BasicAuthenticator != nil { 116 api.BasicAuthenticator = c.BasicAuthenticator 117 } 118 if c.BearerAuthenticator != nil { 119 api.BearerAuthenticator = c.BearerAuthenticator 120 } 121 122 if c.JSONConsumer != nil { 123 api.JSONConsumer = c.JSONConsumer 124 } else { 125 api.JSONConsumer = runtime.JSONConsumer() 126 } 127 api.JSONProducer = runtime.JSONProducer() 128 api.RolesAuth = func(token string) (interface{}, error) { 129 if c.AuthRoles == nil { 130 return token, nil 131 } 132 return c.AuthRoles(token) 133 } 134 135 api.APIAuthorizer = authorizer(c.Authorizer) 136 api.StoreInventoryGetHandler = store.InventoryGetHandlerFunc(func(params store.InventoryGetParams, principal interface{}) middleware.Responder { 137 ctx := params.HTTPRequest.Context() 138 ctx = storeAuth(ctx, principal) 139 return c.StoreAPI.InventoryGet(ctx, params) 140 }) 141 api.StoreOrderCreateHandler = store.OrderCreateHandlerFunc(func(params store.OrderCreateParams, principal interface{}) middleware.Responder { 142 ctx := params.HTTPRequest.Context() 143 ctx = storeAuth(ctx, principal) 144 return c.StoreAPI.OrderCreate(ctx, params) 145 }) 146 api.StoreOrderDeleteHandler = store.OrderDeleteHandlerFunc(func(params store.OrderDeleteParams, principal interface{}) middleware.Responder { 147 ctx := params.HTTPRequest.Context() 148 ctx = storeAuth(ctx, principal) 149 return c.StoreAPI.OrderDelete(ctx, params) 150 }) 151 api.StoreOrderGetHandler = store.OrderGetHandlerFunc(func(params store.OrderGetParams, principal interface{}) middleware.Responder { 152 ctx := params.HTTPRequest.Context() 153 ctx = storeAuth(ctx, principal) 154 return c.StoreAPI.OrderGet(ctx, params) 155 }) 156 api.PetPetCreateHandler = pet.PetCreateHandlerFunc(func(params pet.PetCreateParams, principal interface{}) middleware.Responder { 157 ctx := params.HTTPRequest.Context() 158 ctx = storeAuth(ctx, principal) 159 return c.PetAPI.PetCreate(ctx, params) 160 }) 161 api.PetPetDeleteHandler = pet.PetDeleteHandlerFunc(func(params pet.PetDeleteParams, principal interface{}) middleware.Responder { 162 ctx := params.HTTPRequest.Context() 163 ctx = storeAuth(ctx, principal) 164 return c.PetAPI.PetDelete(ctx, params) 165 }) 166 api.PetPetGetHandler = pet.PetGetHandlerFunc(func(params pet.PetGetParams, principal interface{}) middleware.Responder { 167 ctx := params.HTTPRequest.Context() 168 ctx = storeAuth(ctx, principal) 169 return c.PetAPI.PetGet(ctx, params) 170 }) 171 api.PetPetListHandler = pet.PetListHandlerFunc(func(params pet.PetListParams, principal interface{}) middleware.Responder { 172 ctx := params.HTTPRequest.Context() 173 ctx = storeAuth(ctx, principal) 174 return c.PetAPI.PetList(ctx, params) 175 }) 176 api.PetPetUpdateHandler = pet.PetUpdateHandlerFunc(func(params pet.PetUpdateParams, principal interface{}) middleware.Responder { 177 ctx := params.HTTPRequest.Context() 178 ctx = storeAuth(ctx, principal) 179 return c.PetAPI.PetUpdate(ctx, params) 180 }) 181 api.PetPetUploadImageHandler = pet.PetUploadImageHandlerFunc(func(params pet.PetUploadImageParams, principal interface{}) middleware.Responder { 182 ctx := params.HTTPRequest.Context() 183 ctx = storeAuth(ctx, principal) 184 return c.PetAPI.PetUploadImage(ctx, params) 185 }) 186 api.ServerShutdown = func() {} 187 return api.Serve(c.InnerMiddleware), api, nil 188 } 189 190 // swaggerCopy copies the swagger json to prevent data races in runtime 191 func swaggerCopy(orig json.RawMessage) json.RawMessage { 192 c := make(json.RawMessage, len(orig)) 193 copy(c, orig) 194 return c 195 } 196 197 // authorizer is a helper function to implement the runtime.Authorizer interface. 198 type authorizer func(*http.Request) error 199 200 func (a authorizer) Authorize(req *http.Request, principal interface{}) error { 201 if a == nil { 202 return nil 203 } 204 ctx := storeAuth(req.Context(), principal) 205 return a(req.WithContext(ctx)) 206 } 207 208 func storeAuth(ctx context.Context, principal interface{}) context.Context { 209 return context.WithValue(ctx, AuthKey, principal) 210 }