github.com/rzurga/go-swagger@v0.28.1-0.20211109195225-5d1f453ffa3a/examples/todo-list/restapi/configure_todo_list.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/go-swagger/go-swagger/examples/todo-list/restapi/operations"
    14  	"github.com/go-swagger/go-swagger/examples/todo-list/restapi/operations/todos"
    15  )
    16  
    17  //go:generate swagger generate server --target ../../todo-list --name TodoList --spec ../swagger.yml --principal interface{}
    18  
    19  func configureFlags(api *operations.TodoListAPI) {
    20  	// api.CommandLineOptionsGroups = []swag.CommandLineOptionsGroup{ ... }
    21  }
    22  
    23  func configureAPI(api *operations.TodoListAPI) http.Handler {
    24  	// configure the api here
    25  	api.ServeError = errors.ServeError
    26  
    27  	// Set your custom logger if needed. Default one is log.Printf
    28  	// Expected interface func(string, ...interface{})
    29  	//
    30  	// Example:
    31  	// api.Logger = log.Printf
    32  
    33  	api.UseSwaggerUI()
    34  	// To continue using redoc as your UI, uncomment the following line
    35  	// api.UseRedoc()
    36  
    37  	api.JSONConsumer = runtime.JSONConsumer()
    38  
    39  	api.JSONProducer = runtime.JSONProducer()
    40  
    41  	// Applies when the "x-todolist-token" header is set
    42  	if api.KeyAuth == nil {
    43  		api.KeyAuth = func(token string) (interface{}, error) {
    44  			return nil, errors.NotImplemented("api key auth (key) x-todolist-token from header param [x-todolist-token] has not yet been implemented")
    45  		}
    46  	}
    47  
    48  	// Set your custom authorizer if needed. Default one is security.Authorized()
    49  	// Expected interface runtime.Authorizer
    50  	//
    51  	// Example:
    52  	// api.APIAuthorizer = security.Authorized()
    53  	// You may change here the memory limit for this multipart form parser. Below is the default (32 MB).
    54  	// todos.FindMaxParseMemory = 32 << 20
    55  
    56  	if api.TodosAddOneHandler == nil {
    57  		api.TodosAddOneHandler = todos.AddOneHandlerFunc(func(params todos.AddOneParams, principal interface{}) middleware.Responder {
    58  			return middleware.NotImplemented("operation todos.AddOne has not yet been implemented")
    59  		})
    60  	}
    61  	if api.TodosDestroyOneHandler == nil {
    62  		api.TodosDestroyOneHandler = todos.DestroyOneHandlerFunc(func(params todos.DestroyOneParams, principal interface{}) middleware.Responder {
    63  			return middleware.NotImplemented("operation todos.DestroyOne has not yet been implemented")
    64  		})
    65  	}
    66  	if api.TodosFindHandler == nil {
    67  		api.TodosFindHandler = todos.FindHandlerFunc(func(params todos.FindParams, principal interface{}) middleware.Responder {
    68  			return middleware.NotImplemented("operation todos.Find has not yet been implemented")
    69  		})
    70  	}
    71  	if api.TodosUpdateOneHandler == nil {
    72  		api.TodosUpdateOneHandler = todos.UpdateOneHandlerFunc(func(params todos.UpdateOneParams, principal interface{}) middleware.Responder {
    73  			return middleware.NotImplemented("operation todos.UpdateOne has not yet been implemented")
    74  		})
    75  	}
    76  
    77  	api.PreServerShutdown = func() {}
    78  
    79  	api.ServerShutdown = func() {}
    80  
    81  	return setupGlobalMiddleware(api.Serve(setupMiddlewares))
    82  }
    83  
    84  // The TLS configuration before HTTPS server starts.
    85  func configureTLS(tlsConfig *tls.Config) {
    86  	// Make all necessary changes to the TLS configuration here.
    87  }
    88  
    89  // As soon as server is initialized but not run yet, this function will be called.
    90  // If you need to modify a config, store server instance to stop it individually later, this is the place.
    91  // This function can be called multiple times, depending on the number of serving schemes.
    92  // scheme value will be set accordingly: "http", "https" or "unix".
    93  func configureServer(s *http.Server, scheme, addr string) {
    94  }
    95  
    96  // The middleware configuration is for the handler executors. These do not apply to the swagger.json document.
    97  // The middleware executes after routing but before authentication, binding and validation.
    98  func setupMiddlewares(handler http.Handler) http.Handler {
    99  	return handler
   100  }
   101  
   102  // The middleware configuration happens before anything, this middleware also applies to serving the swagger.json document.
   103  // So this is a good place to plug in a panic handling middleware, logging and metrics.
   104  func setupGlobalMiddleware(handler http.Handler) http.Handler {
   105  	return handler
   106  }