Documentation ¶
Overview ¶
Package httpserver makes running an HTTP server easy as httpserver.Serve(ctx, routes, options...)
Index ¶
- func Address(addr string) func(*http.Server)
- func ErrorLog(logger *slog.Logger) func(*http.Server)
- func MaxHeaderBytes(maxHeaderBytes int) func(*http.Server)
- func New(ctx context.Context, options ...func(*http.Server)) *http.Server
- func ReadHeaderTimeout(seconds int) func(*http.Server)
- func Routes(routes ...Route) func(*http.Server)
- func Serve(ctx context.Context, options ...Option) error
- func TLSConfig(tlsConfig *tls.Config) func(*http.Server)
- type Option
- type Route
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Address ¶
Address specifies the TCP address for the server to listen on, in the form "host:port".
func ErrorLog ¶
ErrorLog specifies an optional logger for errors accepting connections, unexpected behavior from handlers, and underlying FileSystem errors.
func MaxHeaderBytes ¶
MaxHeaderBytes controls the maximum number of bytes the server will read parsing the request header's keys and values, including the request line.
If not specified, or set to zero, a default of 1 MiB is used.
func ReadHeaderTimeout ¶
ReadHeaderTimeout is the amount of time allowed to read request headers.
func Routes ¶
Routes sets http.Server.Handler to an http.ServeMux configured to handle the provided routes. If http.Server.Handler is already set, the routes are added to the existing http.ServeMux. If no routes are specified, the server will use http.DefaultServeMux.
func Serve ¶
Serve HTTP requests for the provided routes. The server will gracefully shut down when the provided context is done or a SIGINT or SIGTERM signal is received.
The server is automatically configured with sensible defaults that can be overridden by functional options. The options are applied in the order they are provided.
Types ¶
type Option ¶
Option is a functional option for configuring an http.Server.
The Option type is just an alias for a function that takes an *http.Server. It is only purpose is to provice clarity that a function is intended to be used as a functional option. Using the Option type is not required to define custom option functions. For example, the following two function signatures are equivalent:
// Using the Option type func MyCustomOption() Option { // Return a closure to modify the server return func(s *http.Server) { // do something with srv } } // Not using the Option type func MyCustomOption() func(*http.Server) { // Return a closure to modify the server return func(s *http.Server) { // do something with srv } }