github.com/thetreep/go-swagger@v0.0.0-20240223100711-35af64f14f01/examples/generated/restapi/configure_petstore.go (about)

     1  // This file is safe to edit. Once it exists it will not be overwritten
     2  
     3  package restapi
     4  
     5  import (
     6  	"crypto/tls"
     7  	"net/http"
     8  
     9  	"github.com/go-openapi/errors"
    10  	"github.com/go-openapi/runtime"
    11  	"github.com/go-openapi/runtime/middleware"
    12  
    13  	"github.com/thetreep/go-swagger/examples/generated/restapi/operations"
    14  	"github.com/thetreep/go-swagger/examples/generated/restapi/operations/pet"
    15  	"github.com/thetreep/go-swagger/examples/generated/restapi/operations/store"
    16  	"github.com/thetreep/go-swagger/examples/generated/restapi/operations/user"
    17  )
    18  
    19  //go:generate swagger generate server --target ../../generated --name Petstore --spec ../swagger-petstore.json --principal interface{}
    20  
    21  func configureFlags(api *operations.PetstoreAPI) {
    22  	// api.CommandLineOptionsGroups = []swag.CommandLineOptionsGroup{ ... }
    23  }
    24  
    25  func configureAPI(api *operations.PetstoreAPI) http.Handler {
    26  	// configure the api here
    27  	api.ServeError = errors.ServeError
    28  
    29  	// Set your custom logger if needed. Default one is log.Printf
    30  	// Expected interface func(string, ...interface{})
    31  	//
    32  	// Example:
    33  	// api.Logger = log.Printf
    34  
    35  	api.UseSwaggerUI()
    36  	// To continue using redoc as your UI, uncomment the following line
    37  	// api.UseRedoc()
    38  
    39  	api.JSONConsumer = runtime.JSONConsumer()
    40  	api.UrlformConsumer = runtime.DiscardConsumer
    41  	api.XMLConsumer = runtime.XMLConsumer()
    42  
    43  	api.JSONProducer = runtime.JSONProducer()
    44  	api.XMLProducer = runtime.XMLProducer()
    45  
    46  	// Applies when the "api_key" header is set
    47  	if api.APIKeyAuth == nil {
    48  		api.APIKeyAuth = func(token string) (interface{}, error) {
    49  			return nil, errors.NotImplemented("api key auth (api_key) api_key from header param [api_key] has not yet been implemented")
    50  		}
    51  	}
    52  	if api.PetstoreAuthAuth == nil {
    53  		api.PetstoreAuthAuth = func(token string, scopes []string) (interface{}, error) {
    54  			return nil, errors.NotImplemented("oauth2 bearer auth (petstore_auth) has not yet been implemented")
    55  		}
    56  	}
    57  
    58  	// Set your custom authorizer if needed. Default one is security.Authorized()
    59  	// Expected interface runtime.Authorizer
    60  	//
    61  	// Example:
    62  	// api.APIAuthorizer = security.Authorized()
    63  	// You may change here the memory limit for this multipart form parser. Below is the default (32 MB).
    64  	// pet.UpdatePetWithFormMaxParseMemory = 32 << 20
    65  
    66  	if api.PetAddPetHandler == nil {
    67  		api.PetAddPetHandler = pet.AddPetHandlerFunc(
    68  			func(params pet.AddPetParams, principal interface{}) middleware.Responder {
    69  				return middleware.NotImplemented("operation pet.AddPet has not yet been implemented")
    70  			},
    71  		)
    72  	}
    73  	if api.UserCreateUserHandler == nil {
    74  		api.UserCreateUserHandler = user.CreateUserHandlerFunc(
    75  			func(params user.CreateUserParams) middleware.Responder {
    76  				return middleware.NotImplemented("operation user.CreateUser has not yet been implemented")
    77  			},
    78  		)
    79  	}
    80  	if api.UserCreateUsersWithArrayInputHandler == nil {
    81  		api.UserCreateUsersWithArrayInputHandler = user.CreateUsersWithArrayInputHandlerFunc(
    82  			func(params user.CreateUsersWithArrayInputParams) middleware.Responder {
    83  				return middleware.NotImplemented("operation user.CreateUsersWithArrayInput has not yet been implemented")
    84  			},
    85  		)
    86  	}
    87  	if api.UserCreateUsersWithListInputHandler == nil {
    88  		api.UserCreateUsersWithListInputHandler = user.CreateUsersWithListInputHandlerFunc(
    89  			func(params user.CreateUsersWithListInputParams) middleware.Responder {
    90  				return middleware.NotImplemented("operation user.CreateUsersWithListInput has not yet been implemented")
    91  			},
    92  		)
    93  	}
    94  	if api.StoreDeleteOrderHandler == nil {
    95  		api.StoreDeleteOrderHandler = store.DeleteOrderHandlerFunc(
    96  			func(params store.DeleteOrderParams) middleware.Responder {
    97  				return middleware.NotImplemented("operation store.DeleteOrder has not yet been implemented")
    98  			},
    99  		)
   100  	}
   101  	if api.PetDeletePetHandler == nil {
   102  		api.PetDeletePetHandler = pet.DeletePetHandlerFunc(
   103  			func(params pet.DeletePetParams, principal interface{}) middleware.Responder {
   104  				return middleware.NotImplemented("operation pet.DeletePet has not yet been implemented")
   105  			},
   106  		)
   107  	}
   108  	if api.UserDeleteUserHandler == nil {
   109  		api.UserDeleteUserHandler = user.DeleteUserHandlerFunc(
   110  			func(params user.DeleteUserParams) middleware.Responder {
   111  				return middleware.NotImplemented("operation user.DeleteUser has not yet been implemented")
   112  			},
   113  		)
   114  	}
   115  	if api.PetFindPetsByStatusHandler == nil {
   116  		api.PetFindPetsByStatusHandler = pet.FindPetsByStatusHandlerFunc(
   117  			func(params pet.FindPetsByStatusParams, principal interface{}) middleware.Responder {
   118  				return middleware.NotImplemented("operation pet.FindPetsByStatus has not yet been implemented")
   119  			},
   120  		)
   121  	}
   122  	if api.PetFindPetsByTagsHandler == nil {
   123  		api.PetFindPetsByTagsHandler = pet.FindPetsByTagsHandlerFunc(
   124  			func(params pet.FindPetsByTagsParams, principal interface{}) middleware.Responder {
   125  				return middleware.NotImplemented("operation pet.FindPetsByTags has not yet been implemented")
   126  			},
   127  		)
   128  	}
   129  	if api.StoreGetOrderByIDHandler == nil {
   130  		api.StoreGetOrderByIDHandler = store.GetOrderByIDHandlerFunc(
   131  			func(params store.GetOrderByIDParams) middleware.Responder {
   132  				return middleware.NotImplemented("operation store.GetOrderByID has not yet been implemented")
   133  			},
   134  		)
   135  	}
   136  	if api.PetGetPetByIDHandler == nil {
   137  		api.PetGetPetByIDHandler = pet.GetPetByIDHandlerFunc(
   138  			func(params pet.GetPetByIDParams, principal interface{}) middleware.Responder {
   139  				return middleware.NotImplemented("operation pet.GetPetByID has not yet been implemented")
   140  			},
   141  		)
   142  	}
   143  	if api.UserGetUserByNameHandler == nil {
   144  		api.UserGetUserByNameHandler = user.GetUserByNameHandlerFunc(
   145  			func(params user.GetUserByNameParams) middleware.Responder {
   146  				return middleware.NotImplemented("operation user.GetUserByName has not yet been implemented")
   147  			},
   148  		)
   149  	}
   150  	if api.UserLoginUserHandler == nil {
   151  		api.UserLoginUserHandler = user.LoginUserHandlerFunc(
   152  			func(params user.LoginUserParams) middleware.Responder {
   153  				return middleware.NotImplemented("operation user.LoginUser has not yet been implemented")
   154  			},
   155  		)
   156  	}
   157  	if api.UserLogoutUserHandler == nil {
   158  		api.UserLogoutUserHandler = user.LogoutUserHandlerFunc(
   159  			func(params user.LogoutUserParams) middleware.Responder {
   160  				return middleware.NotImplemented("operation user.LogoutUser has not yet been implemented")
   161  			},
   162  		)
   163  	}
   164  	if api.StorePlaceOrderHandler == nil {
   165  		api.StorePlaceOrderHandler = store.PlaceOrderHandlerFunc(
   166  			func(params store.PlaceOrderParams) middleware.Responder {
   167  				return middleware.NotImplemented("operation store.PlaceOrder has not yet been implemented")
   168  			},
   169  		)
   170  	}
   171  	if api.PetUpdatePetHandler == nil {
   172  		api.PetUpdatePetHandler = pet.UpdatePetHandlerFunc(
   173  			func(params pet.UpdatePetParams, principal interface{}) middleware.Responder {
   174  				return middleware.NotImplemented("operation pet.UpdatePet has not yet been implemented")
   175  			},
   176  		)
   177  	}
   178  	if api.PetUpdatePetWithFormHandler == nil {
   179  		api.PetUpdatePetWithFormHandler = pet.UpdatePetWithFormHandlerFunc(
   180  			func(params pet.UpdatePetWithFormParams, principal interface{}) middleware.Responder {
   181  				return middleware.NotImplemented("operation pet.UpdatePetWithForm has not yet been implemented")
   182  			},
   183  		)
   184  	}
   185  	if api.UserUpdateUserHandler == nil {
   186  		api.UserUpdateUserHandler = user.UpdateUserHandlerFunc(
   187  			func(params user.UpdateUserParams) middleware.Responder {
   188  				return middleware.NotImplemented("operation user.UpdateUser has not yet been implemented")
   189  			},
   190  		)
   191  	}
   192  
   193  	api.PreServerShutdown = func() {}
   194  
   195  	api.ServerShutdown = func() {}
   196  
   197  	return setupGlobalMiddleware(api.Serve(setupMiddlewares))
   198  }
   199  
   200  // The TLS configuration before HTTPS server starts.
   201  func configureTLS(tlsConfig *tls.Config) {
   202  	// Make all necessary changes to the TLS configuration here.
   203  }
   204  
   205  // As soon as server is initialized but not run yet, this function will be called.
   206  // If you need to modify a config, store server instance to stop it individually later, this is the place.
   207  // This function can be called multiple times, depending on the number of serving schemes.
   208  // scheme value will be set accordingly: "http", "https" or "unix".
   209  func configureServer(s *http.Server, scheme, addr string) {
   210  }
   211  
   212  // The middleware configuration is for the handler executors. These do not apply to the swagger.json document.
   213  // The middleware executes after routing but before authentication, binding and validation.
   214  func setupMiddlewares(handler http.Handler) http.Handler {
   215  	return handler
   216  }
   217  
   218  // The middleware configuration happens before anything, this middleware also applies to serving the swagger.json document.
   219  // So this is a good place to plug in a panic handling middleware, logging and metrics.
   220  func setupGlobalMiddleware(handler http.Handler) http.Handler {
   221  	return handler
   222  }