github.com/thetreep/go-swagger@v0.0.0-20240223100711-35af64f14f01/examples/stream-server/restapi/configure_countdown.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  	"io"
     8  	"net/http"
     9  
    10  	errors "github.com/go-openapi/errors"
    11  	runtime "github.com/go-openapi/runtime"
    12  	middleware "github.com/go-openapi/runtime/middleware"
    13  	"github.com/thetreep/go-swagger/examples/stream-server/biz"
    14  
    15  	"github.com/thetreep/go-swagger/examples/stream-server/restapi/operations"
    16  )
    17  
    18  //go:generate swagger generate server --target .. --name Countdown --spec ../swagger.yml
    19  
    20  func configureFlags(api *operations.CountdownAPI) {
    21  	// api.CommandLineOptionsGroups = []swag.CommandLineOptionsGroup{ ... }
    22  }
    23  
    24  func configureAPI(api *operations.CountdownAPI) 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.JSONConsumer = runtime.JSONConsumer()
    35  
    36  	api.JSONProducer = runtime.JSONProducer()
    37  
    38  	myCounter := &biz.MyCounter{}
    39  	api.ElapseHandler = operations.ElapseHandlerFunc(
    40  		func(params operations.ElapseParams) middleware.Responder {
    41  			if params.Length == 11 {
    42  				return operations.NewElapseForbidden()
    43  			}
    44  			return middleware.ResponderFunc(
    45  				func(rw http.ResponseWriter, p runtime.Producer) {
    46  					f, _ := rw.(http.Flusher)
    47  					rw.WriteHeader(200)
    48  					_ = myCounter.Down(params.Length, &flushWriter{f: f, w: rw})
    49  				},
    50  			)
    51  
    52  		},
    53  	)
    54  
    55  	api.ServerShutdown = func() {}
    56  
    57  	return setupGlobalMiddleware(api.Serve(setupMiddlewares))
    58  }
    59  
    60  // Via https://play.golang.org/p/PpbPyXbtEs
    61  type flushWriter struct {
    62  	f http.Flusher
    63  	w io.Writer
    64  }
    65  
    66  // Via https://play.golang.org/p/PpbPyXbtEs
    67  func (fw *flushWriter) Write(p []byte) (n int, err error) {
    68  	n, err = fw.w.Write(p)
    69  	if fw.f != nil {
    70  		fw.f.Flush()
    71  	}
    72  	return
    73  }
    74  
    75  // The TLS configuration before HTTPS server starts.
    76  func configureTLS(tlsConfig *tls.Config) {
    77  	// Make all necessary changes to the TLS configuration here.
    78  }
    79  
    80  // As soon as server is initialized but not run yet, this function will be called.
    81  // If you need to modify a config, store server instance to stop it individually later, this is the place.
    82  // This function can be called multiple times, depending on the number of serving schemes.
    83  // scheme value will be set accordingly: "http", "https" or "unix".
    84  func configureServer(s *http.Server, scheme, addr string) {
    85  }
    86  
    87  // The middleware configuration is for the handler executors. These do not apply to the swagger.json document.
    88  // The middleware executes after routing but before authentication, binding and validation.
    89  func setupMiddlewares(handler http.Handler) http.Handler {
    90  	return handler
    91  }
    92  
    93  // The middleware configuration happens before anything, this middleware also applies to serving the swagger.json document.
    94  // So this is a good place to plug in a panic handling middleware, logging and metrics.
    95  func setupGlobalMiddleware(handler http.Handler) http.Handler {
    96  	return handler
    97  }