github.com/circl-dev/go-swagger@v0.31.0/examples/authentication/restapi/configure_auth_sample.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/circl-dev/runtime"
    10  	"github.com/circl-dev/runtime/middleware"
    11  	"github.com/go-openapi/errors"
    12  
    13  	"github.com/circl-dev/go-swagger/examples/authentication/models"
    14  	"github.com/circl-dev/go-swagger/examples/authentication/restapi/operations"
    15  	"github.com/circl-dev/go-swagger/examples/authentication/restapi/operations/customers"
    16  )
    17  
    18  //go:generate swagger generate server --target ../../authentication --name AuthSample --spec ../swagger.yml --principal models.Principal
    19  
    20  func configureFlags(api *operations.AuthSampleAPI) {
    21  	// api.CommandLineOptionsGroups = []swag.CommandLineOptionsGroup{ ... }
    22  }
    23  
    24  func configureAPI(api *operations.AuthSampleAPI) http.Handler {
    25  	// configure the api here
    26  	api.ServeError = errors.ServeError
    27  
    28  	// Set your custom logger if needed. Default one is log.Printf
    29  	// Expected interface func(string, ...interface{})
    30  	//
    31  	// Example:
    32  	// api.Logger = log.Printf
    33  
    34  	api.UseSwaggerUI()
    35  	// To continue using redoc as your UI, uncomment the following line
    36  	// api.UseRedoc()
    37  
    38  	api.JSONConsumer = runtime.JSONConsumer()
    39  
    40  	api.JSONProducer = runtime.JSONProducer()
    41  
    42  	// Applies when the "x-token" header is set
    43  	if api.KeyAuth == nil {
    44  		api.KeyAuth = func(token string) (*models.Principal, error) {
    45  			return nil, errors.NotImplemented("api key auth (key) x-token from header param [x-token] has not yet been implemented")
    46  		}
    47  	}
    48  
    49  	// Set your custom authorizer if needed. Default one is security.Authorized()
    50  	// Expected interface runtime.Authorizer
    51  	//
    52  	// Example:
    53  	// api.APIAuthorizer = security.Authorized()
    54  
    55  	if api.CustomersCreateHandler == nil {
    56  		api.CustomersCreateHandler = customers.CreateHandlerFunc(func(params customers.CreateParams, principal *models.Principal) middleware.Responder {
    57  			return middleware.NotImplemented("operation customers.Create has not yet been implemented")
    58  		})
    59  	}
    60  	if api.CustomersGetIDHandler == nil {
    61  		api.CustomersGetIDHandler = customers.GetIDHandlerFunc(func(params customers.GetIDParams, principal *models.Principal) middleware.Responder {
    62  			return middleware.NotImplemented("operation customers.GetID has not yet been implemented")
    63  		})
    64  	}
    65  
    66  	api.PreServerShutdown = func() {}
    67  
    68  	api.ServerShutdown = func() {}
    69  
    70  	return setupGlobalMiddleware(api.Serve(setupMiddlewares))
    71  }
    72  
    73  // The TLS configuration before HTTPS server starts.
    74  func configureTLS(tlsConfig *tls.Config) {
    75  	// Make all necessary changes to the TLS configuration here.
    76  }
    77  
    78  // As soon as server is initialized but not run yet, this function will be called.
    79  // If you need to modify a config, store server instance to stop it individually later, this is the place.
    80  // This function can be called multiple times, depending on the number of serving schemes.
    81  // scheme value will be set accordingly: "http", "https" or "unix".
    82  func configureServer(s *http.Server, scheme, addr string) {
    83  }
    84  
    85  // The middleware configuration is for the handler executors. These do not apply to the swagger.json document.
    86  // The middleware executes after routing but before authentication, binding and validation.
    87  func setupMiddlewares(handler http.Handler) http.Handler {
    88  	return handler
    89  }
    90  
    91  // The middleware configuration happens before anything, this middleware also applies to serving the swagger.json document.
    92  // So this is a good place to plug in a panic handling middleware, logging and metrics.
    93  func setupGlobalMiddleware(handler http.Handler) http.Handler {
    94  	return handler
    95  }