Documentation ¶
Index ¶
- func Run(ctx context.Context, addr string, handler http.Handler) error
- func WithErrorLog(l *log.Logger) serverOption
- func WithGracefulShutdown(d time.Duration) serverOption
- func WithIdleTimeout(d time.Duration) serverOption
- func WithMaxHeaderBytes(n int) serverOption
- func WithPreconfiguredServer(s *http.Server) serverOption
- func WithReadHeaderTimeout(d time.Duration) serverOption
- func WithReadTimeout(d time.Duration) serverOption
- func WithTLSConfig(t *tls.Config) serverOption
- func WithTLSNextProto(f func(*http.Server, *tls.Conn, http.Handler)) serverOption
- func WithWriteTimeout(d time.Duration) serverOption
- type Server
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Run ¶
Run starts an HTTP server on the specified address and runs the provided handler. It returns an error if the server fails to start or encounters an error during execution. The context.Context parameter allows for graceful shutdown of the server. The addr parameter specifies the address to listen on, e.g., ":8080" for all interfaces on port 8080. The handler parameter is an http.Handler that defines the behavior of the server.
func WithErrorLog ¶
WithErrorLog sets the error logger for the server. If nil, the log package's standard logger is used.
func WithGracefulShutdown ¶ added in v0.1.1
WithGracefulShutdown sets the graceful shutdown timeout. If zero, the default timeout of 5 seconds is used.
func WithIdleTimeout ¶
WithIdleTimeout sets the maximum amount of time to wait for the next request when keep-alives are enabled. If IdleTimeout is zero, the value of ReadTimeout is used. If both are zero, there is no timeout.
func WithMaxHeaderBytes ¶
func WithMaxHeaderBytes(n int) serverOption
WithMaxHeaderBytes sets the maximum size of request headers. This prevents attacks where an attacker sends a large header to consume server resources. If zero, DefaultMaxHeaderBytes of 1MB is used.
func WithPreconfiguredServer ¶
WithPreconfiguredServer allows you to provide a preconfigured http.Server instance. This is useful if you want to configure TLS or other options that are not exposed by this package.
func WithReadHeaderTimeout ¶
WithReadHeaderTimeout sets the amount of time allowed to read request headers. A duration of 0 means no timeout.
func WithReadTimeout ¶
WithReadTimeout sets the maximum duration for reading the entire request, including the body. This also includes the time spent reading the request header. If the server does not receive a new request within this duration it will close the connection. A duration of 0 means no timeout.
func WithTLSConfig ¶
WithTLSConfig sets the TLS configuration to use when starting TLS. If nil, the default configuration is used. If non-nil, HTTP/2 support may not be enabled by default.
func WithTLSNextProto ¶
WithTLSNextProto sets a function to be called after a TLS handshake has been completed. This is useful for protocols which require interaction immediately after the handshake. If non-nil, HTTP/2 support may not be enabled by default.
func WithWriteTimeout ¶
WithWriteTimeout sets the maximum duration before timing out writes of the response. A duration of 0 means no timeout.
Types ¶
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server represents an HTTP server wrapper. It contains an underlying http.Server instance and provides methods to start and stop the server. It also provides a Run function to start an HTTP server with graceful shutdown. The server is stopped gracefully when the context is cancelled or a shutdown signal is received.
func New ¶
New creates a new instance of the new HTTP server with the specified address, handler, and optional server options. The server options can be used to customize the server's behavior. The addr parameter specifies the address to listen on, e.g., ":8080" for all interfaces on port 8080. The handler parameter is an http.Handler that defines the behavior of the server. The opt parameter is a variadic list of server options. The server options are applied in order, so the last option overrides the previous ones. The server options are applied before the server is started.
func (*Server) Close ¶
Close stops the server immediately without waiting for active connections to finish. It returns an error if the server fails to stop.