Documentation ¶
Overview ¶
Package httpserver provides API to standard http server with graceful shutdown.
Typical usage:
httpserver.New( httpserver.WithName("my-server"), httpserver.WithAddress("127.0.0.1:8080"), httpserver.WithHandler(myHandler), ).Run(ctx)
Index ¶
- Constants
- type Config
- type ConfigService
- type Option
- func WithAddress(address string) Option
- func WithConfig(service ConfigService, key string) Option
- func WithHandler(handler http.Handler) Option
- func WithLogger(logger log.Logger) Option
- func WithName(name string) Option
- func WithReadHeaderTimeout(readHeaderTimeout int64) Option
- func WithReadTimeout(readTimeout int64) Option
- func WithWriteTimeout(writeTimeout int64) Option
- type Server
Constants ¶
const ( // DefAddress is the default server's address that will be listened to. DefAddress = "127.0.0.1:8080" // DefName is the default server's name. DefName = "http-server" // DefReadHeaderTimeout is the default maximum time in seconds to read http header. DefReadHeaderTimeout = 2 // DefReadTimeout is the default maximum time in seconds to read request. DefReadTimeout = 3 // DefWriteTimeout is the maximum time in seconds to write request response. DefWriteTimeout = 3 )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// contains filtered or unexported fields
}
Config structure with server configuration. Shouldn't be created manually.
type ConfigService ¶
ConfigService interface used to obtain configuration from somewhere into some specific structure.
type Option ¶
Option function that is fed to New and MustNew. Obtain them using 'With' functions down below.
func WithAddress ¶
WithAddress option applies provided address that will be listened to. Default: "127.0.0.1:8080".
func WithConfig ¶
func WithConfig(service ConfigService, key string) Option
WithConfig option retrieves configuration from provided configuration service.
Example JSON configuration with all possible fields (if some are not present, defaults will be used):
{ "name": "server-name", "address": "127.0.0.1:8080" }
func WithHandler ¶
WithHandler option applies provided handler. Default: http.NotFoundHandler().
func WithLogger ¶
WithLogger option applies provided logger. Default: standard logger with name equal to server's one.
func WithReadHeaderTimeout ¶
WithReadHeaderTimeout option applies provided maximum time in seconds to read http header. Default: 2.
func WithReadTimeout ¶
WithReadTimeout option applies provided maximum time in seconds to read request. Default: 3.
func WithWriteTimeout ¶
WithWriteTimeout option applies provided maximum time in seconds to write response. Default: 3.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server structure that provides http server functionality. Don't create manually, use the functions down below instead.
func (*Server) ReadHeaderTimeout ¶
ReadHeaderTimeout method gets server's maximum time to read http header.
func (*Server) ReadTimeout ¶
ReadTimeout method gets server's maximum time to read request.
func (*Server) Run ¶
Run method runs server listen loop. It is blocking so you probably want to run it in a separate goroutine. If you pass cancellable context here, you will be able to gracefully shutdown server that waits for all requests to complete.
Only upgraded connections (such as websocket ones) will not be waited for, you will need to shutdown them manually.
func (*Server) WriteTimeout ¶
WriteTimeout method gets server's maximum time to write response.