http

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Jun 10, 2025 License: MIT Imports: 12 Imported by: 0

README

http

http/compress

Http middleware (inbound - todo) and RoundTripper (outbound) that handles compression (br.deflate,gzip,zstd).

http/log

Http middleware (inbound) and RoundTripper (outbound) using slog.

Inbound Logger (http middleware)

Inbound logger middleware (http handler) can be initialized with

A logger (WithLogger)

The logger that is set with this function is the logger will be used to log the traffic. If no logger is set the slog.Default() will be used.

A level (WithLogInLevel)

The level that is set with this function is the log level in which the middleware will log the traffic. Default value: slog.LevelDebug.

A log mode (WithMode)

The log mode can take two values Drain and Tee.

  • When Drain is selected the body of the income request is read entirely upon receiving and a copy of the body will be passed to the next http handlers.
  • When Tee is selected a tee reader wraps incoming request's body and then the request is passed to the next http handlers. The request body will be read when (and only) the next (or final) http handlers, read it. Default value: Drain.
A log policy (WithLogPolicy)

The log policy can indicate conditions

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DrainAndCloseBody added in v0.0.2

func DrainAndCloseBody(body io.ReadCloser) error

func DrainAndCloseRequest

func DrainAndCloseRequest(r *http.Request) error

DrainAndCloseRequest can be used (most probably with defer) from the client side to ensure that the http request body is consumed til the end and closed.

func DrainAndCloseResponse

func DrainAndCloseResponse(r *http.Response) error

DrainAndCloseResponse can be used (most probably with defer) from the client side to ensure that the http response body is consumed til the end and closed.

func EchoHandler added in v0.0.2

func EchoHandler(logger *slog.Logger) func(http.ResponseWriter, *http.Request)

func NewHTTPClient

func NewHTTPClient(globalTimeout time.Duration) *http.Client

NewHTTPClient returns a new default http client.

func Recoverer

func Recoverer(logger *slog.Logger) func(http.Handler) http.Handler

Recoverer returns a recoverer http middleware that logs every panic into the provided slog logger.

func URLSafeBase64 added in v0.0.2

func URLSafeBase64() *base64.Encoding

URLSafeBase64 returns a base64.Encoding based on base64.URLEncoding replacing the default padding character ('=') padding character to a url safe one ('~'). In URL parameters, the following characters are considered safe and do not need encoding [rfc3986](https://www.rfc-editor.org/rfc/rfc3986.html#section-3.1): Alphabetic characters: A-Z, a-z Digits: 0-9 Hyphen: - Underscore: _ Period: . Tilde: ~

func UnpackHeaders added in v0.0.2

func UnpackHeaders(h http.Header) map[string]string

func UnpackRequest added in v0.0.2

func UnpackRequest(r *http.Request) (map[string]any, error)

func UnpackRequestBody added in v0.0.2

func UnpackRequestBody(h http.Header, body io.ReadCloser) (any, error)

func UnpackURL added in v0.0.2

func UnpackURL(u *url.URL) map[string]any

Types

This section is empty.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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