httpx

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2023 License: MIT Imports: 9 Imported by: 0

README

httpx

License go.mod Go version GoDoc Latest tag Go Report

Useful set of functions and middlewares to use on top of net/http.

Motivations

On any project where I need a http server, I find myself copy/pasting the same boilerplate over and over again. This project aims at keeping all that code reusable and properly tested.

func runServer(ctx context.Context) error {
	var (
		address string
		handler http.HandlerFunc
		logger  *slog.Logger
	)

	listener, err := httpx.NewListener(ctx, address)
	if err != nil {
		return fmt.Errorf("unable to create listener: %w", err)
	}

	server := httpx.NewServer(handler, httpx.ServerWithErrorLogger(slog.NewLogLogger(logger.Handler(), slog.LevelWarn)))
	return httpx.Serve(ctx, server, listener, time.Second*15)
}

License

This project is under the MIT licence, please see the LICENCE file.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ListenAndServe added in v1.1.0

func ListenAndServe(ctx context.Context, address string, handler http.HandlerFunc) error

ListenAndServe is a shortcut for NewListener and Serve.

func MiddlewareRecover added in v1.1.0

func MiddlewareRecover(callbacks ...MiddlewareRecoverFunc) func(http.Handler) http.Handler

MiddlewareRecover is a middleware that recover requests from a panic.

func NewListener

func NewListener(ctx context.Context, address string, opts ...ListenerOption) (net.Listener, error)

NewListener creates a new listener.

func NewServer

func NewServer(handler http.HandlerFunc, opts ...ServerOption) *http.Server

NewServer returns a configured http server with robust defaults.

func Serve added in v1.1.0

func Serve(ctx context.Context, s *http.Server, l net.Listener, shutdownTimeout time.Duration) error

Serve serves the server through the provided listener. On context cancellation, the server tries to gracefully shut down for as long as shutdownTimeout. Once this timeout is reached, the server is stopped, any way.

Types

type ListenerOption

type ListenerOption func(*listenerOptions) error

ListenerOption defines options applier for the listener.

func ListenWithIntermediateTLSConfig added in v1.1.0

func ListenWithIntermediateTLSConfig(certFile, keyFile string) ListenerOption

ListenWithIntermediateTLSConfig sets the tls configuration for tls.NewListener. "Intermediate" is defined based on this website: https://wiki.mozilla.org/Security/Server_Side_TLS.

func ListenWithKeepAlive

func ListenWithKeepAlive(keepAlive time.Duration) ListenerOption

ListenWithKeepAlive sets keepalive period.

func ListenWithNetwork

func ListenWithNetwork(network string) ListenerOption

ListenWithNetwork sets the network option for net.Listen().

func ListenWithTLSConfig

func ListenWithTLSConfig(cfg *tls.Config) ListenerOption

ListenWithTLSConfig sets the tls configuration for tls.NewListener.

func ListenWithoutKeepAlive

func ListenWithoutKeepAlive() ListenerOption

ListenWithoutKeepAlive disables the keepalive on the listener.

type MiddlewareRecoverFunc added in v1.1.0

type MiddlewareRecoverFunc func(rw http.ResponseWriter, r *http.Request, reason any)

MiddlewareRecoverFunc is the signature of the callback called each time the request panics.

type ServerOption

type ServerOption func(*serverOptions)

ServerOption defines options applier for the server.

func ServerWithErrorLogger added in v1.1.0

func ServerWithErrorLogger(logger *log.Logger) ServerOption

ServerWithErrorLogger sets the provider logger to be used for errors in the http server.

Jump to

Keyboard shortcuts

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