Documentation
¶
Overview ¶
Package server provides a simple router based https://github.com/gorilla/mux, with a SpecOpenAPIHandler type return the response (not serialized) and an error, as well as a Middleware type to allow middleware for this new SpecOpenAPIHandler type.
You can see middleware implementation in the github.com/mwm-io/gapi/middleware package.
import ( "http" "log" "github.com/mwm-io/gapi/server" ) r := server.NewMux() // Add your http handlers. var h SpecOpenAPIHandler server.AddHandler(r, http.MethodGet, "/hello", h) // Add your server options here. err := server.ServeAndHandleShutdown(r) if err != nil { log.Fatal(err) }
Index ¶
- func AddDocHandlers(r *mux.Router, middlewares ...handler.Middleware) error
- func AddHandler(router *mux.Router, method, path string, f handler.Handler)
- func AddHandlerFactory(router *mux.Router, method, path string, f handler.Factory)
- func NewMux() *mux.Router
- func NewServer(r *mux.Router, opts ...Option) *http.Server
- func ServeAndHandleShutdown(r *mux.Router, opts ...Option) error
- func ServeAndHandleTlSShutdown(r *mux.Router, certCRT, certKey string, opts ...Option) error
- func StartProcessAndHandleStopSignals(process func() error, shutdown func(ctx context.Context) error, opts ...Option) error
- func UseMiddlewares(middlewares ...handler.Middleware)
- type CORS
- type Option
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddDocHandlers ¶
func AddDocHandlers(r *mux.Router, middlewares ...handler.Middleware) error
AddDocHandlers will add the necessary handlers to serve a rapidoc endpoint: 2 endpoints to serve rapidoc.html and oauth-receiver.html from rapidoc and one endpoint to serve the json openapi definition of your API.
func AddHandler ¶
AddHandler register a new handler to the given mux router on a given method and path.
func AddHandlerFactory ¶
AddHandlerFactory register a new handler factory to the given mux router on a given method and path. You can use it if you want a new instance of your handler for each call. It must be use useful if :
- you use a middleware for handle request params like middleware.BodyDecoder, middleware.PathParameters, etc.
- you store properties in your handler struct during Serve process
func NewServer ¶
NewServer returns a new configured *http.Server, using an existing mux.Router. You can override the default configuration using the available Option.
func ServeAndHandleShutdown ¶
ServeAndHandleShutdown start a *http.Server with the default configuration (overridden by the given Option) This function lock your program until a signal stopping your program is received. (see WithStopSignals)
func ServeAndHandleTlSShutdown ¶
ServeAndHandleTlSShutdown start a *http.Server with TLS and the default configuration (overridden by the given Option) This function lock your program until a signal stopping your program is received. (see WithStopSignals)
func StartProcessAndHandleStopSignals ¶
func StartProcessAndHandleStopSignals(process func() error, shutdown func(ctx context.Context) error, opts ...Option) error
StartProcessAndHandleStopSignals starts the given process and listen for os stop signals to stop it, executing the shutdown function. It can also take Option to customize StopSignals, Context, and StopTimeout.
func UseMiddlewares ¶ added in v0.1.0
func UseMiddlewares(middlewares ...handler.Middleware)
UseMiddlewares appends a given list of handler.Middleware to middlewares chain.
Middleware can be used to intercept or otherwise modify requests and/or responses, and are executed in list order.
Types ¶
type Option ¶
type Option func(*serverOptions)
Option is an option to modify the default configuration of the http server.
func WithCORS ¶
WithCORS sets the cors configuration. By default, authorize "*" with methods "GET", "HEAD", "POST", "PUT", "PATCH", "DELETE", "OPTIONS" and headers "Content-Type", "Authorization"
func WithContext ¶
WithContext specify a parent context for the *http.Server. It will be passed to every request. By default, creates a new context.
func WithPort ¶
WithPort sets the server port on which to listen to. If the port is empty, it will first look the PORT environment variable. If the PORT environment variable is empty, it will take the default value. (8080 for http and 443 for https)
func WithStopSignals ¶
WithStopSignals specify on which os.Signal we must shut down the server. By default, os.Interrupt, syscall.SIGINT and syscall.SIGTERM
func WithStopTimeout ¶
WithStopTimeout set the timeout when the shutting down the server. By default, 3 seconds.
func WithStrictSlash ¶
WithStrictSlash specify if we use the strictSlash configuration or not. When true, if the route path is "/path/", accessing "/path" will perform a redirect to the former and vice versa. In other words, your application will always see the path as specified in the route.