Documentation ¶
Overview ¶
Package server exposes a reusable HTTP server with graceful shutdown and preconfigured ping, health check and shutdown endpoints. Server uses gorilla/mux(https://github.com/gorilla/mux) as its main request router.
Index ¶
- Constants
- type Configs
- type Server
- type ServerImpl
- func (s *ServerImpl) GetHTTPServer() *http.Server
- func (s *ServerImpl) RegisterHealthcheckEndpoint(path string, handler func(w http.ResponseWriter, r *http.Request))
- func (s *ServerImpl) RegisterOnShutdown(f func())
- func (s *ServerImpl) RegisterServerShutdownHandler(f ShutdownHandler)
- func (s *ServerImpl) RegisterServerStartHandler(f func(s *http.Server) error)
- func (s *ServerImpl) Start() error
- func (s *ServerImpl) Stop() error
- type ShutdownHandler
Constants ¶
const ( // DefaultPort holds the default port the server will listen on. DefaultPort = 9090 // DefaultShutdownTimeout holds the timeout to shutdown the server. DefaultShutdownTimeout = 10 * time.Second // DefaultReadTimeout holds the default read timeout. DefaultReadTimeout = 15 * time.Second // DefaultWriteTimeout holds the default write timeout. DefaultWriteTimeout = 15 * time.Second // DefaultPingEndpoint holds the default ping endpoint. DefaultPingEndpoint = "/ping" // DefaultHealthcheckEndpoint holds the default healtcheck endpoint. DefaultHealthcheckEndpoint = "/healthcheck" // DefaultShutdownEndpoint holds the default shutdown endpoint. DefaultShutdownEndpoint = "/shutdown" )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Configs ¶
type Configs struct { Port int ShutdownTimeout time.Duration ReadTimeout time.Duration WriteTimeout time.Duration PingEndpoint string HealthcheckEndpoint string ShutdownEndpoint string }
Configs holds server specific configs. Port holds the server port. ShutdownTimeout holds the timeout to shutdown the server. ReadTimeout holds the read timeout. WriteTimeout holds the write timeout. PingEndpoint holds the ping endpoint. HealthcheckEndpoint holds the healthcheck endpoint. ShutdownEndpoint holds the shutdown endpoint.
func NewConfigs ¶
func NewConfigs() *Configs
NewConfigs initializes a new instance of Configs with default values.
type Server ¶
type Server interface { Start() error Stop() error GetHTTPServer() *http.Server RegisterOnShutdown(f func()) RegisterServerStartHandler(f func(s *http.Server) error) RegisterHealthcheckEndpoint(path string, handler func(w http.ResponseWriter, r *http.Request)) RegisterServerShutdownHandler(f ShutdownHandler) }
Server represents a HTTP server.
type ServerImpl ¶
type ServerImpl struct { Configs *Configs Router *mux.Router HTTPServer *http.Server // contains filtered or unexported fields }
ServerImpl implements a HTTP Server.
func (*ServerImpl) GetHTTPServer ¶
func (s *ServerImpl) GetHTTPServer() *http.Server
GetHTTPServer returns the HTTP server instance,
func (*ServerImpl) RegisterHealthcheckEndpoint ¶
func (s *ServerImpl) RegisterHealthcheckEndpoint(path string, handler func(w http.ResponseWriter, r *http.Request))
RegisterHealthcheckEndpoint register the handler to handle healthcheck responses.
func (*ServerImpl) RegisterOnShutdown ¶
func (s *ServerImpl) RegisterOnShutdown(f func())
RegisterOnShutdown registers a function to call on Shutdown. It delegates the calls to the standard http.Server package.
func (*ServerImpl) RegisterServerShutdownHandler ¶
func (s *ServerImpl) RegisterServerShutdownHandler(f ShutdownHandler)
RegisterServerShutdownHandler registers a function that should shutdown the HTTP server.
func (*ServerImpl) RegisterServerStartHandler ¶
func (s *ServerImpl) RegisterServerStartHandler(f func(s *http.Server) error)
RegisterServerStartHandler registers a function that should start the HTTP server.
func (*ServerImpl) Start ¶
func (s *ServerImpl) Start() error
Start starts the server and blocks, listening for requests.
func (*ServerImpl) Stop ¶
func (s *ServerImpl) Stop() error
Stop stops the server gracefully and synchronously, returning any error detected during shutdown. The ShutdownTimeout is respected for all in-flight requests. When the server is no longer processing any requests, Stop() will return and the server won't listen for requests anymore.