httpx

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Dec 17, 2025 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package httpx provides HTTP server utilities and helpers for Kedastral services.

Index

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

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

LoggingMiddleware returns middleware that logs HTTP requests. It logs the method, path, status code, and duration of each request.

func RecoveryMiddleware

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

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.

func WriteJSON

func WriteJSON(w http.ResponseWriter, status int, v any) error

WriteJSON writes a JSON response with the specified status code. The value v is marshaled to JSON and written to the response writer. If marshaling fails, a 500 Internal Server Error is written instead.

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

func NewServer(addr string, handler http.Handler, logger *slog.Logger) *Server

NewServer creates a new HTTP server that listens on the specified address. The handler can be nil to use http.DefaultServeMux.

func (*Server) Start

func (s *Server) Start() error

Start begins serving HTTP requests. This method blocks until the server is stopped. Returns an error if the server fails to start or is stopped ungracefully.

func (*Server) Stop

func (s *Server) Stop(timeout time.Duration) error

Stop gracefully shuts down the server. It waits up to the specified timeout for active connections to complete.

Jump to

Keyboard shortcuts

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