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