github.com/josephspurrier/go-swagger@v0.2.1-0.20221129144919-1f672a142a00/examples/auto-configure/restapi/auto_configure_a_to_do_list_application.go (about)

     1  // Code generated by go-swagger; DO NOT EDIT.
     2  // Auto configures api handlers Implementations.
     3  
     4  package restapi
     5  
     6  import (
     7  	"crypto/tls"
     8  	"net/http"
     9  
    10  	"github.com/go-openapi/errors"
    11  	"github.com/go-openapi/runtime"
    12  	"github.com/go-openapi/runtime/middleware"
    13  
    14  	"github.com/go-swagger/go-swagger/examples/auto-configure/implementation"
    15  	"github.com/go-swagger/go-swagger/examples/auto-configure/restapi/operations"
    16  	"github.com/go-swagger/go-swagger/examples/auto-configure/restapi/operations/todos"
    17  )
    18  
    19  //go:generate swagger generate server --target ../../auto-configure --name AToDoListApplication --spec ../swagger.yml --implementation-package github.com/go-swagger/go-swagger/examples/auto-configure/implementation --principal interface{}
    20  
    21  // This file auto configures the api backend implementation.
    22  // implementation package must already exist.
    23  // implementation.New() is implemented by user, and must return an object
    24  // or interface that implements Handler interface defined below.
    25  var Impl Handler = implementation.New()
    26  
    27  // Handler handles all api server backend configurations and requests
    28  type Handler interface {
    29  	Authable
    30  	Configurable
    31  	TodosHandler
    32  }
    33  
    34  // Configurable handles all server configurations
    35  type Configurable interface {
    36  	ConfigureFlags(api *operations.AToDoListApplicationAPI)
    37  	ConfigureTLS(tlsConfig *tls.Config)
    38  	ConfigureServer(s *http.Server, scheme, addr string)
    39  	CustomConfigure(api *operations.AToDoListApplicationAPI)
    40  	SetupMiddlewares(handler http.Handler) http.Handler
    41  	SetupGlobalMiddleware(handler http.Handler) http.Handler
    42  }
    43  
    44  // Authable handles server authentication
    45  type Authable interface {
    46  	// Applies when the "x-todolist-token" header is set
    47  	KeyAuth(token string) (interface{}, error)
    48  }
    49  
    50  /* TodosHandler  */
    51  type TodosHandler interface {
    52  	AddOne(params todos.AddOneParams, principal interface{}) middleware.Responder
    53  	DestroyOne(params todos.DestroyOneParams, principal interface{}) middleware.Responder
    54  	FindTodos(params todos.FindTodosParams, principal interface{}) middleware.Responder
    55  	UpdateOne(params todos.UpdateOneParams, principal interface{}) middleware.Responder
    56  }
    57  
    58  func configureFlags(api *operations.AToDoListApplicationAPI) {
    59  	Impl.ConfigureFlags(api)
    60  }
    61  
    62  func configureAPI(api *operations.AToDoListApplicationAPI) http.Handler {
    63  
    64  	api.ServeError = errors.ServeError
    65  
    66  	api.UseSwaggerUI()
    67  
    68  	api.JSONConsumer = runtime.JSONConsumer()
    69  
    70  	api.JSONProducer = runtime.JSONProducer()
    71  
    72  	// Applies when the "x-todolist-token" header is set
    73  	api.KeyAuth = func(token string) (interface{}, error) {
    74  		return Impl.KeyAuth(token)
    75  	}
    76  
    77  	api.TodosAddOneHandler = todos.AddOneHandlerFunc(func(params todos.AddOneParams, principal interface{}) middleware.Responder {
    78  		return Impl.AddOne(params, principal)
    79  	})
    80  	api.TodosDestroyOneHandler = todos.DestroyOneHandlerFunc(func(params todos.DestroyOneParams, principal interface{}) middleware.Responder {
    81  		return Impl.DestroyOne(params, principal)
    82  	})
    83  	api.TodosFindTodosHandler = todos.FindTodosHandlerFunc(func(params todos.FindTodosParams, principal interface{}) middleware.Responder {
    84  		return Impl.FindTodos(params, principal)
    85  	})
    86  	api.TodosUpdateOneHandler = todos.UpdateOneHandlerFunc(func(params todos.UpdateOneParams, principal interface{}) middleware.Responder {
    87  		return Impl.UpdateOne(params, principal)
    88  	})
    89  
    90  	api.PreServerShutdown = func() {}
    91  
    92  	api.ServerShutdown = func() {}
    93  
    94  	// CustomConfigure can override or add to configurations set above
    95  	Impl.CustomConfigure(api)
    96  
    97  	return setupGlobalMiddleware(api.Serve(setupMiddlewares))
    98  }
    99  
   100  // The TLS configuration before HTTPS server starts.
   101  func configureTLS(tlsConfig *tls.Config) {
   102  	// Make all necessary changes to the TLS configuration here.
   103  	Impl.ConfigureTLS(tlsConfig)
   104  }
   105  
   106  // As soon as server is initialized but not run yet, this function will be called.
   107  // If you need to modify a config, store server instance to stop it individually later, this is the place.
   108  // This function can be called multiple times, depending on the number of serving schemes.
   109  // scheme value will be set accordingly: "http", "https" or "unix".
   110  func configureServer(s *http.Server, scheme, addr string) {
   111  	Impl.ConfigureServer(s, scheme, addr)
   112  }
   113  
   114  // The middleware configuration is for the handler executors. These do not apply to the swagger.json document.
   115  // The middleware executes after routing but before authentication, binding and validation.
   116  func setupMiddlewares(handler http.Handler) http.Handler {
   117  	return Impl.SetupMiddlewares(handler)
   118  }
   119  
   120  // The middleware configuration happens before anything, this middleware also applies to serving the swagger.json document.
   121  // So this is a good place to plug in a panic handling middleware, logging and metrics.
   122  func setupGlobalMiddleware(handler http.Handler) http.Handler {
   123  	return Impl.SetupGlobalMiddleware(handler)
   124  }