httpkit

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Feb 3, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package httpkit provides standard HTTP middleware and response utilities.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func JSONError

func JSONError(w http.ResponseWriter, r *http.Request, statusCode int, message string)

JSONError writes a JSON error response with the given status code and message. If a request ID is present in the request context, it is included in the response body.

func Logging

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

Logging returns middleware that logs each request's method, path, status code, and duration using the provided structured logger. If a request ID is present in the context, it is included in the log entry.

func Recovery

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

Recovery returns middleware that catches panics in downstream handlers, logs them at Error level with stack information, and returns a 500 JSON error.

func RequestID

func RequestID(next http.Handler) http.Handler

RequestID is middleware that generates a unique request ID, stores it in the request context, and sets it as the X-Request-ID response header.

func RequestIDFrom

func RequestIDFrom(ctx context.Context) string

RequestIDFrom retrieves the request ID from the context. Returns an empty string if no request ID is present.

Types

type ErrorResponse

type ErrorResponse struct {
	Error      string `json:"error"`
	StatusCode int    `json:"status_code"`
	RequestID  string `json:"request_id,omitempty"`
}

ErrorResponse is the standard JSON error body returned by JSONError.

Jump to

Keyboard shortcuts

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