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