github.com/dirkm/go-swagger@v0.19.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/go-openapi/loads"
    13  	"github.com/go-openapi/runtime"
    14  	"github.com/go-openapi/runtime/middleware"
    15  
    16  	"github.com/go-swagger/go-swagger/examples/contributed-templates/stratoscale/restapi/operations"
    17  	"github.com/go-swagger/go-swagger/examples/contributed-templates/stratoscale/restapi/operations/pet"
    18  	"github.com/go-swagger/go-swagger/examples/contributed-templates/stratoscale/restapi/operations/store"
    19  )
    20  
    21  type contextKey string
    22  
    23  const AuthKey contextKey = "Auth"
    24  
    25  //go:generate mockery -name PetAPI -inpkg
    26  
    27  // PetAPI
    28  type PetAPI interface {
    29  	PetCreate(ctx context.Context, params pet.PetCreateParams) middleware.Responder
    30  	PetDelete(ctx context.Context, params pet.PetDeleteParams) middleware.Responder
    31  	PetGet(ctx context.Context, params pet.PetGetParams) middleware.Responder
    32  	PetList(ctx context.Context, params pet.PetListParams) middleware.Responder
    33  	PetUpdate(ctx context.Context, params pet.PetUpdateParams) middleware.Responder
    34  	PetUploadImage(ctx context.Context, params pet.PetUploadImageParams) middleware.Responder
    35  }
    36  
    37  //go:generate mockery -name StoreAPI -inpkg
    38  
    39  // StoreAPI
    40  type StoreAPI interface {
    41  	InventoryGet(ctx context.Context, params store.InventoryGetParams) middleware.Responder
    42  	OrderCreate(ctx context.Context, params store.OrderCreateParams) middleware.Responder
    43  	// OrderDelete is For valid response try integer IDs with positive integer value. Negative or non-integer values will generate API errors
    44  	OrderDelete(ctx context.Context, params store.OrderDeleteParams) middleware.Responder
    45  	// OrderGet is For valid response try integer IDs with value >= 1 and <= 10. Other values will generated exceptions
    46  	OrderGet(ctx context.Context, params store.OrderGetParams) middleware.Responder
    47  }
    48  
    49  // Config is configuration for Handler
    50  type Config struct {
    51  	PetAPI
    52  	StoreAPI
    53  	Logger func(string, ...interface{})
    54  	// InnerMiddleware is for the handler executors. These do not apply to the swagger.json document.
    55  	// The middleware executes after routing but before authentication, binding and validation
    56  	InnerMiddleware func(http.Handler) http.Handler
    57  
    58  	// Authorizer is used to authorize a request after the Auth function was called using the "Auth*" functions
    59  	// and the principal was stored in the context in the "AuthKey" context value.
    60  	Authorizer func(*http.Request) error
    61  
    62  	// AuthRoles Applies when the "X-Auth-Roles" header is set
    63  	AuthRoles func(token string) (interface{}, error)
    64  }
    65  
    66  // Handler returns an http.Handler given the handler configuration
    67  // It mounts all the business logic implementers in the right routing.
    68  func Handler(c Config) (http.Handler, error) {
    69  	h, _, err := HandlerAPI(c)
    70  	return h, err
    71  }
    72  
    73  // HandlerAPI returns an http.Handler given the handler configuration
    74  // and the corresponding *Petstore instance.
    75  // It mounts all the business logic implementers in the right routing.
    76  func HandlerAPI(c Config) (http.Handler, *operations.PetstoreAPI, error) {
    77  	spec, err := loads.Analyzed(swaggerCopy(SwaggerJSON), "")
    78  	if err != nil {
    79  		return nil, nil, fmt.Errorf("analyze swagger: %v", err)
    80  	}
    81  	api := operations.NewPetstoreAPI(spec)
    82  	api.ServeError = errors.ServeError
    83  	api.Logger = c.Logger
    84  
    85  	api.JSONConsumer = runtime.JSONConsumer()
    86  	api.JSONProducer = runtime.JSONProducer()
    87  	api.RolesAuth = func(token string) (interface{}, error) {
    88  		if c.AuthRoles == nil {
    89  			return token, nil
    90  		}
    91  		return c.AuthRoles(token)
    92  	}
    93  
    94  	api.APIAuthorizer = authorizer(c.Authorizer)
    95  	api.StoreInventoryGetHandler = store.InventoryGetHandlerFunc(func(params store.InventoryGetParams, principal interface{}) middleware.Responder {
    96  		ctx := params.HTTPRequest.Context()
    97  		ctx = storeAuth(ctx, principal)
    98  		return c.StoreAPI.InventoryGet(ctx, params)
    99  	})
   100  	api.StoreOrderCreateHandler = store.OrderCreateHandlerFunc(func(params store.OrderCreateParams, principal interface{}) middleware.Responder {
   101  		ctx := params.HTTPRequest.Context()
   102  		ctx = storeAuth(ctx, principal)
   103  		return c.StoreAPI.OrderCreate(ctx, params)
   104  	})
   105  	api.StoreOrderDeleteHandler = store.OrderDeleteHandlerFunc(func(params store.OrderDeleteParams, principal interface{}) middleware.Responder {
   106  		ctx := params.HTTPRequest.Context()
   107  		ctx = storeAuth(ctx, principal)
   108  		return c.StoreAPI.OrderDelete(ctx, params)
   109  	})
   110  	api.StoreOrderGetHandler = store.OrderGetHandlerFunc(func(params store.OrderGetParams, principal interface{}) middleware.Responder {
   111  		ctx := params.HTTPRequest.Context()
   112  		ctx = storeAuth(ctx, principal)
   113  		return c.StoreAPI.OrderGet(ctx, params)
   114  	})
   115  	api.PetPetCreateHandler = pet.PetCreateHandlerFunc(func(params pet.PetCreateParams, principal interface{}) middleware.Responder {
   116  		ctx := params.HTTPRequest.Context()
   117  		ctx = storeAuth(ctx, principal)
   118  		return c.PetAPI.PetCreate(ctx, params)
   119  	})
   120  	api.PetPetDeleteHandler = pet.PetDeleteHandlerFunc(func(params pet.PetDeleteParams, principal interface{}) middleware.Responder {
   121  		ctx := params.HTTPRequest.Context()
   122  		ctx = storeAuth(ctx, principal)
   123  		return c.PetAPI.PetDelete(ctx, params)
   124  	})
   125  	api.PetPetGetHandler = pet.PetGetHandlerFunc(func(params pet.PetGetParams, principal interface{}) middleware.Responder {
   126  		ctx := params.HTTPRequest.Context()
   127  		ctx = storeAuth(ctx, principal)
   128  		return c.PetAPI.PetGet(ctx, params)
   129  	})
   130  	api.PetPetListHandler = pet.PetListHandlerFunc(func(params pet.PetListParams, principal interface{}) middleware.Responder {
   131  		ctx := params.HTTPRequest.Context()
   132  		ctx = storeAuth(ctx, principal)
   133  		return c.PetAPI.PetList(ctx, params)
   134  	})
   135  	api.PetPetUpdateHandler = pet.PetUpdateHandlerFunc(func(params pet.PetUpdateParams, principal interface{}) middleware.Responder {
   136  		ctx := params.HTTPRequest.Context()
   137  		ctx = storeAuth(ctx, principal)
   138  		return c.PetAPI.PetUpdate(ctx, params)
   139  	})
   140  	api.PetPetUploadImageHandler = pet.PetUploadImageHandlerFunc(func(params pet.PetUploadImageParams, principal interface{}) middleware.Responder {
   141  		ctx := params.HTTPRequest.Context()
   142  		ctx = storeAuth(ctx, principal)
   143  		return c.PetAPI.PetUploadImage(ctx, params)
   144  	})
   145  	api.ServerShutdown = func() {}
   146  	return api.Serve(c.InnerMiddleware), api, nil
   147  }
   148  
   149  // swaggerCopy copies the swagger json to prevent data races in runtime
   150  func swaggerCopy(orig json.RawMessage) json.RawMessage {
   151  	c := make(json.RawMessage, len(orig))
   152  	copy(c, orig)
   153  	return c
   154  }
   155  
   156  // authorizer is a helper function to implement the runtime.Authorizer interface.
   157  type authorizer func(*http.Request) error
   158  
   159  func (a authorizer) Authorize(req *http.Request, principal interface{}) error {
   160  	if a == nil {
   161  		return nil
   162  	}
   163  	ctx := storeAuth(req.Context(), principal)
   164  	return a(req.WithContext(ctx))
   165  }
   166  
   167  func storeAuth(ctx context.Context, principal interface{}) context.Context {
   168  	return context.WithValue(ctx, AuthKey, principal)
   169  }