httpserver

package
v0.0.0-...-b5a44e9 Latest Latest
Warning

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

Go to latest
Published: Aug 2, 2024 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package httpserver makes running an HTTP server easy as httpserver.Serve(ctx, routes, options...)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Address

func Address(addr string) func(*http.Server)

Address specifies the TCP address for the server to listen on, in the form "host:port".

func ErrorLog

func ErrorLog(logger *slog.Logger) func(*http.Server)

ErrorLog specifies an optional logger for errors accepting connections, unexpected behavior from handlers, and underlying FileSystem errors.

func MaxHeaderBytes

func MaxHeaderBytes(maxHeaderBytes int) func(*http.Server)

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 New

func New(ctx context.Context, options ...func(*http.Server)) *http.Server

func ReadHeaderTimeout

func ReadHeaderTimeout(seconds int) func(*http.Server)

ReadHeaderTimeout is the amount of time allowed to read request headers.

func Routes

func Routes(routes ...Route) func(*http.Server)

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

func Serve(ctx context.Context, options ...Option) error

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.

func TLSConfig

func TLSConfig(tlsConfig *tls.Config) func(*http.Server)

TLSConfig provides a TLS configuration for use by ServeTLS and ListenAndServeTLS.

Types

type Option

type Option = func(*http.Server)

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
	}
}

type Route

type Route struct {
	Pattern    string
	Handler    http.Handler
	Middleware []func(http.Handler) http.Handler
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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