Documentation
¶
Overview ¶
Package httpkit provides standard HTTP middleware and response utilities.
Index ¶
- func JSONError(w http.ResponseWriter, r *http.Request, statusCode int, message string)
- func Logging(logger *slog.Logger) func(http.Handler) http.Handler
- func Recovery(logger *slog.Logger) func(http.Handler) http.Handler
- func RequestID(next http.Handler) http.Handler
- func RequestIDFrom(ctx context.Context) string
- type ErrorResponse
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func JSONError ¶
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 ¶
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 ¶
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 ¶
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 ¶
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.