github.com/circl-dev/go-swagger@v0.31.0/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/circl-dev/runtime"
    13  	"github.com/circl-dev/runtime/middleware"
    14  	"github.com/circl-dev/runtime/security"
    15  	"github.com/circl-dev/loads"
    16  
    17  	"github.com/circl-dev/go-swagger/examples/contributed-templates/stratoscale/restapi/operations"
    18  	"github.com/circl-dev/go-swagger/examples/contributed-templates/stratoscale/restapi/operations/pet"
    19  	"github.com/circl-dev/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 -inpkg
    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 -inpkg
    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  
    90  // Handler returns an http.Handler given the handler configuration
    91  // It mounts all the business logic implementers in the right routing.
    92  func Handler(c Config) (http.Handler, error) {
    93  	h, _, err := HandlerAPI(c)
    94  	return h, err
    95  }
    96  
    97  // HandlerAPI returns an http.Handler given the handler configuration
    98  // and the corresponding *Petstore instance.
    99  // It mounts all the business logic implementers in the right routing.
   100  func HandlerAPI(c Config) (http.Handler, *operations.PetstoreAPI, error) {
   101  	spec, err := loads.Analyzed(swaggerCopy(SwaggerJSON), "")
   102  	if err != nil {
   103  		return nil, nil, fmt.Errorf("analyze swagger: %v", err)
   104  	}
   105  	api := operations.NewPetstoreAPI(spec)
   106  	api.ServeError = errors.ServeError
   107  	api.Logger = c.Logger
   108  
   109  	if c.APIKeyAuthenticator != nil {
   110  		api.APIKeyAuthenticator = c.APIKeyAuthenticator
   111  	}
   112  	if c.BasicAuthenticator != nil {
   113  		api.BasicAuthenticator = c.BasicAuthenticator
   114  	}
   115  	if c.BearerAuthenticator != nil {
   116  		api.BearerAuthenticator = c.BearerAuthenticator
   117  	}
   118  
   119  	api.JSONConsumer = runtime.JSONConsumer()
   120  	api.JSONProducer = runtime.JSONProducer()
   121  	api.RolesAuth = func(token string) (interface{}, error) {
   122  		if c.AuthRoles == nil {
   123  			return token, nil
   124  		}
   125  		return c.AuthRoles(token)
   126  	}
   127  
   128  	api.APIAuthorizer = authorizer(c.Authorizer)
   129  	api.StoreInventoryGetHandler = store.InventoryGetHandlerFunc(func(params store.InventoryGetParams, principal interface{}) middleware.Responder {
   130  		ctx := params.HTTPRequest.Context()
   131  		ctx = storeAuth(ctx, principal)
   132  		return c.StoreAPI.InventoryGet(ctx, params)
   133  	})
   134  	api.StoreOrderCreateHandler = store.OrderCreateHandlerFunc(func(params store.OrderCreateParams, principal interface{}) middleware.Responder {
   135  		ctx := params.HTTPRequest.Context()
   136  		ctx = storeAuth(ctx, principal)
   137  		return c.StoreAPI.OrderCreate(ctx, params)
   138  	})
   139  	api.StoreOrderDeleteHandler = store.OrderDeleteHandlerFunc(func(params store.OrderDeleteParams, principal interface{}) middleware.Responder {
   140  		ctx := params.HTTPRequest.Context()
   141  		ctx = storeAuth(ctx, principal)
   142  		return c.StoreAPI.OrderDelete(ctx, params)
   143  	})
   144  	api.StoreOrderGetHandler = store.OrderGetHandlerFunc(func(params store.OrderGetParams, principal interface{}) middleware.Responder {
   145  		ctx := params.HTTPRequest.Context()
   146  		ctx = storeAuth(ctx, principal)
   147  		return c.StoreAPI.OrderGet(ctx, params)
   148  	})
   149  	api.PetPetCreateHandler = pet.PetCreateHandlerFunc(func(params pet.PetCreateParams, principal interface{}) middleware.Responder {
   150  		ctx := params.HTTPRequest.Context()
   151  		ctx = storeAuth(ctx, principal)
   152  		return c.PetAPI.PetCreate(ctx, params)
   153  	})
   154  	api.PetPetDeleteHandler = pet.PetDeleteHandlerFunc(func(params pet.PetDeleteParams, principal interface{}) middleware.Responder {
   155  		ctx := params.HTTPRequest.Context()
   156  		ctx = storeAuth(ctx, principal)
   157  		return c.PetAPI.PetDelete(ctx, params)
   158  	})
   159  	api.PetPetGetHandler = pet.PetGetHandlerFunc(func(params pet.PetGetParams, principal interface{}) middleware.Responder {
   160  		ctx := params.HTTPRequest.Context()
   161  		ctx = storeAuth(ctx, principal)
   162  		return c.PetAPI.PetGet(ctx, params)
   163  	})
   164  	api.PetPetListHandler = pet.PetListHandlerFunc(func(params pet.PetListParams, principal interface{}) middleware.Responder {
   165  		ctx := params.HTTPRequest.Context()
   166  		ctx = storeAuth(ctx, principal)
   167  		return c.PetAPI.PetList(ctx, params)
   168  	})
   169  	api.PetPetUpdateHandler = pet.PetUpdateHandlerFunc(func(params pet.PetUpdateParams, principal interface{}) middleware.Responder {
   170  		ctx := params.HTTPRequest.Context()
   171  		ctx = storeAuth(ctx, principal)
   172  		return c.PetAPI.PetUpdate(ctx, params)
   173  	})
   174  	api.PetPetUploadImageHandler = pet.PetUploadImageHandlerFunc(func(params pet.PetUploadImageParams, principal interface{}) middleware.Responder {
   175  		ctx := params.HTTPRequest.Context()
   176  		ctx = storeAuth(ctx, principal)
   177  		return c.PetAPI.PetUploadImage(ctx, params)
   178  	})
   179  	api.ServerShutdown = func() {}
   180  	return api.Serve(c.InnerMiddleware), api, nil
   181  }
   182  
   183  // swaggerCopy copies the swagger json to prevent data races in runtime
   184  func swaggerCopy(orig json.RawMessage) json.RawMessage {
   185  	c := make(json.RawMessage, len(orig))
   186  	copy(c, orig)
   187  	return c
   188  }
   189  
   190  // authorizer is a helper function to implement the runtime.Authorizer interface.
   191  type authorizer func(*http.Request) error
   192  
   193  func (a authorizer) Authorize(req *http.Request, principal interface{}) error {
   194  	if a == nil {
   195  		return nil
   196  	}
   197  	ctx := storeAuth(req.Context(), principal)
   198  	return a(req.WithContext(ctx))
   199  }
   200  
   201  func storeAuth(ctx context.Context, principal interface{}) context.Context {
   202  	return context.WithValue(ctx, AuthKey, principal)
   203  }