Documentation
¶
Overview ¶
Package httpx provides HTTP server utilities and helpers for Kedastral services.
Index ¶
- func HealthHandler() http.HandlerFunc
- func HealthHandlerWithCheck(check func() error) http.HandlerFunc
- func LoggingMiddleware(logger *slog.Logger) func(http.Handler) http.Handler
- func RecoveryMiddleware(logger *slog.Logger) func(http.Handler) http.Handler
- func WriteError(w http.ResponseWriter, status int, err error)
- func WriteErrorMessage(w http.ResponseWriter, status int, message string)
- func WriteJSON(w http.ResponseWriter, status int, v any) error
- type ErrorResponse
- type Server
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func HealthHandler ¶
func HealthHandler() http.HandlerFunc
HealthHandler returns an http.Handler that always responds with 200 OK. This can be used for basic health checks that don't require any logic.
For more complex health checks (e.g., checking staleness), create a custom handler.
func HealthHandlerWithCheck ¶
func HealthHandlerWithCheck(check func() error) http.HandlerFunc
HealthHandlerWithCheck returns an http.Handler that calls a check function. If the check returns an error, a 503 Service Unavailable is returned. Otherwise, returns 200 OK.
func LoggingMiddleware ¶
LoggingMiddleware returns middleware that logs HTTP requests. It logs the method, path, status code, and duration of each request.
func RecoveryMiddleware ¶
RecoveryMiddleware returns middleware that recovers from panics in HTTP handlers. If a panic occurs, it logs the error and returns a 500 Internal Server Error.
func WriteError ¶
func WriteError(w http.ResponseWriter, status int, err error)
WriteError writes a JSON error response with the specified status code. The error message is extracted from the error and wrapped in an ErrorResponse. Error responses should use format: {"error":"<msg>"}
func WriteErrorMessage ¶
func WriteErrorMessage(w http.ResponseWriter, status int, message string)
WriteErrorMessage writes a JSON error response with a custom message.
Types ¶
type ErrorResponse ¶
type ErrorResponse struct {
Error string `json:"error"`
}
ErrorResponse represents a JSON error response. This provides consistent error formatting across all endpoints.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server wraps http.Server with graceful shutdown capabilities. It provides a clean API for starting and stopping HTTP servers.
func NewServer ¶
NewServer creates a new HTTP server that listens on the specified address. The handler can be nil to use http.DefaultServeMux.