httpserver

package
v1.8.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 14, 2022 License: Apache-2.0 Imports: 12 Imported by: 1

Documentation

Index

Constants

View Source
const (
	// DefaultShutdownTimeout is the default shutdown timeout.
	DefaultShutdownTimeout = 10 * time.Second
	// DefaultReadHeaderTimeout is the default read header timeout.
	DefaultReadHeaderTimeout = 30 * time.Second
	// DefaultIdleTimeout is the amount of time an HTTP/2 connection can be idle.
	DefaultIdleTimeout = 3 * time.Minute
)

Variables

This section is empty.

Functions

func Run added in v1.8.0

func Run(
	ctx context.Context,
	logger *zap.Logger,
	listener net.Listener,
	handler http.Handler,
	options ...RunOption,
) (retErr error)

Run will start a HTTP server listening on the provided listener and serving the provided handler. This call is blocking and the run is cancelled when the input context is cancelled, the listener is closed upon return.

The Run function can be configured further by passing a variety of options.

Types

type HTTPHandlerMapperOption

type HTTPHandlerMapperOption func(*httpHandlerMapper)

HTTPHandlerMapperOption is an option for a new HTTPHandlerMapper.

func HTTPHandlerMapperWithPrefix

func HTTPHandlerMapperWithPrefix(prefix string) HTTPHandlerMapperOption

HTTPHandlerMapperWithPrefix returns a new HTTPHandlerMapperOption that uses the prefix.

type Mapper

type Mapper interface {
	Map(chi.Router) error
}

Mapper initializes a Router.

func MapperWithMiddlewares added in v1.8.0

func MapperWithMiddlewares(mapper Mapper, middlewares chi.Middlewares) Mapper

MapperWithMiddlewares rewrites the Map function of the Mapper to call the provided middlewares inside a chi Group before mapping

func NewHTTPHandlerMapper

func NewHTTPHandlerMapper(
	handler http.Handler,
	options ...HTTPHandlerMapperOption,
) Mapper

NewHTTPHandlerMapper returns a new Mapper for the http.Handler.

type MapperFunc

type MapperFunc func(chi.Router) error

MapperFunc is a function for a Mapper

func (MapperFunc) Map

func (m MapperFunc) Map(router chi.Router) error

Map implements Mapper.

type RunOption added in v1.8.0

type RunOption = RunnerOption

RunOption is an option for a new Run.

func RunWithReadHeaderTimeout added in v1.8.0

func RunWithReadHeaderTimeout(readHeaderTimeout time.Duration) RunOption

RunWithReadHeaderTimeout returns a new RunOption that uses the given read header timeout.

The default is to use DefaultReadHeaderTimeout. If readHeaderTimeout is 0, no read header timeout will be used.

func RunWithShutdownTimeout added in v1.8.0

func RunWithShutdownTimeout(shutdownTimeout time.Duration) RunOption

RunWithShutdownTimeout returns a new RunOption that uses the given shutdown timeout.

The default is to use DefaultShutdownTimeout. If shutdownTimeout is 0, no graceful shutdown will be performed.

func RunWithTLSConfig added in v1.8.0

func RunWithTLSConfig(tlsConfig *tls.Config) RunOption

RunWithTLSConfig returns a new RunOption that uses the given tls.Config.

The default is to use no TLS.

type Runner

type Runner interface {
	// Run runs the router.
	//
	// Blocking.
	// The runner is cancelled when the input context is cancelled.
	// The listener is closed upon return.
	//
	// Response write errors are logged. Response write errors can be ignored.
	//
	// Can be called multiple times, resulting in different runs.
	Run(ctx context.Context, listener net.Listener, mappers ...Mapper) error
}

Runner is a runner.

func NewRunner

func NewRunner(logger *zap.Logger, options ...RunnerOption) Runner

NewRunner returns a new Runner.

type RunnerOption

type RunnerOption func(*runner)

RunnerOption is an option for a new Runner.

func RunnerWithHealth

func RunnerWithHealth() RunnerOption

RunnerWithHealth returns a new RunnerOption that turns a health check endpoint on at /health.

The default is to not turn on health.

func RunnerWithMaxBodySize

func RunnerWithMaxBodySize(maxBodySize int64) RunnerOption

RunnerWithMaxBodySize returns a new RunnerOption that sets the max size of incoming request body.

The default is to not limit body size.

func RunnerWithMiddlewares

func RunnerWithMiddlewares(middlewares ...func(http.Handler) http.Handler) RunnerOption

RunnerWithMiddlewares returns a new RunnerOption that use middlewares when the Runner Run.

func RunnerWithReadHeaderTimeout

func RunnerWithReadHeaderTimeout(readHeaderTimeout time.Duration) RunnerOption

RunnerWithReadHeaderTimeout returns a new RunnerOption that uses the given read header timeout.

The default is to use DefaultReadHeaderTimeout. If readHeaderTimeout is 0, no read header timeout will be used.

func RunnerWithShutdownTimeout

func RunnerWithShutdownTimeout(shutdownTimeout time.Duration) RunnerOption

RunnerWithShutdownTimeout returns a new RunnerOption that uses the given shutdown timeout.

The default is to use DefaultShutdownTimeout. If shutdownTimeout is 0, no graceful shutdown will be performed.

func RunnerWithSilentEndpoints added in v1.8.0

func RunnerWithSilentEndpoints(silentEndpoints ...string) RunnerOption

RunnerWithSilentEndpoints returns a new RunnerOption that disables logging from the provided endpoints.

The default is to not silence any endpoints.

func RunnerWithTLSConfig

func RunnerWithTLSConfig(tlsConfig *tls.Config) RunnerOption

RunnerWithTLSConfig returns a new RunnerOption that uses the given tls.Config.

The default is to use no TLS.

func RunnerWithWalkFunc

func RunnerWithWalkFunc(walkFunc chi.WalkFunc) RunnerOption

RunnerWithWalkFunc returns a new RunnerOption that runs chi.Walk to walk the router after all middlewares and routes have been mounted, but before the server is started.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL