http

package
v0.29.0 Latest Latest
Warning

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

Go to latest
Published: May 27, 2026 License: MIT Imports: 22 Imported by: 0

Documentation

Overview

Package http provides HTTP server implementation and request handlers.

Package http provides HTTP server implementation and request handlers using Gin web framework. The server uses Clean Architecture principles with structured logging (slog) and graceful shutdown.

This server uses Gin (github.com/gin-gonic/gin) for HTTP routing while maintaining compatibility with the application's existing patterns:

  • Custom slog-based logging middleware (instead of Gin's default logger)
  • Gin-compatible error handling utilities (httputil.HandleErrorGin)
  • Manual http.Server configuration for timeout and graceful shutdown control

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BusinessMetricsMiddleware added in v0.29.0

func BusinessMetricsMiddleware(bm metrics.BusinessMetrics, domain, operation string) gin.HandlerFunc

BusinessMetricsMiddleware records operation count and duration for a named route. Must be placed before the handler in the middleware chain. Uses HTTP response status to determine success (< 400) vs error (>= 400).

func CustomLoggerMiddleware

func CustomLoggerMiddleware(logger *slog.Logger) gin.HandlerFunc

CustomLoggerMiddleware provides structured logging using slog. This replaces Gin's default logger to maintain consistency with the application's existing logging patterns.

func CustomRecoveryMiddleware added in v0.23.0

func CustomRecoveryMiddleware(logger *slog.Logger) gin.HandlerFunc

CustomRecoveryMiddleware provides panic recovery using slog. This replaces Gin's default recovery middleware to provide structured error logs for critical failures.

func MaxRequestBodySizeMiddleware added in v0.27.0

func MaxRequestBodySizeMiddleware(limit int64) gin.HandlerFunc

MaxRequestBodySizeMiddleware restricts the request body size.

Types

type MetricsServer added in v0.14.0

type MetricsServer struct {
	// contains filtered or unexported fields
}

MetricsServer represents the HTTP server for Prometheus metrics.

func NewDefaultMetricsServer added in v0.23.0

func NewDefaultMetricsServer(
	host string,
	port int,
	logger *slog.Logger,
	metricsProvider *metrics.Provider,
	readTimeout time.Duration,
	writeTimeout time.Duration,
	idleTimeout time.Duration,
) *MetricsServer

NewDefaultMetricsServer creates a new MetricsServer with default timeouts.

func NewMetricsServer added in v0.14.0

func NewMetricsServer(
	host string,
	port int,
	logger *slog.Logger,
	metricsProvider *metrics.Provider,
	readTimeout time.Duration,
	writeTimeout time.Duration,
	idleTimeout time.Duration,
) *MetricsServer

NewMetricsServer creates a new MetricsServer.

func (*MetricsServer) GetHandler added in v0.22.0

func (s *MetricsServer) GetHandler() http.Handler

GetHandler returns the http.Handler for testing purposes.

func (*MetricsServer) Server added in v0.28.0

func (s *MetricsServer) Server() *http.Server

Server returns the underlying http.Server for testing purposes.

func (*MetricsServer) Shutdown added in v0.14.0

func (s *MetricsServer) Shutdown(ctx context.Context) error

Shutdown gracefully shuts down the metrics HTTP server.

func (*MetricsServer) Start added in v0.14.0

func (s *MetricsServer) Start(ctx context.Context) error

Start starts the metrics HTTP server and handles context cancellation.

type RouterDeps added in v0.29.0

type RouterDeps struct {
	ClientHandler          *authHTTP.ClientHandler
	TokenHandler           *authHTTP.TokenHandler
	AuditLogHandler        *authHTTP.AuditLogHandler
	SecretHandler          *secretsHTTP.SecretHandler
	TransitKeyHandler      *transitHTTP.TransitKeyHandler
	CryptoHandler          *transitHTTP.CryptoHandler
	TokenizationKeyHandler *tokenizationHTTP.TokenizationKeyHandler
	TokenizationHandler    *tokenizationHTTP.TokenizationHandler
	TokenUseCase           authUseCase.TokenUseCase
	AuditLogUseCase        authUseCase.AuditLogUseCase
	BusinessMetrics        metrics.BusinessMetrics
	MetricsProvider        *metrics.Provider
	MetricsNamespace       string
}

RouterDeps bundles the handler and usecase dependencies required by SetupRouter. Using a struct instead of positional parameters prevents silent field-swap bugs and makes adding a new handler a named-field change rather than a positional one.

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server represents the HTTP server.

func NewServer

func NewServer(
	db *sql.DB,
	host string,
	port int,
	readTimeout time.Duration,
	writeTimeout time.Duration,
	idleTimeout time.Duration,
	logger *slog.Logger,
) *Server

NewServer creates a new HTTP server.

func (*Server) GetHandler

func (s *Server) GetHandler() http.Handler

GetHandler returns the http.Handler for testing purposes. Returns nil if SetupRouter has not been called yet.

func (*Server) SetupRouter

func (s *Server) SetupRouter(ctx context.Context, cfg *config.Config, deps RouterDeps)

SetupRouter configures the Gin router with all routes and middleware. This method is called during server initialization with all required dependencies.

func (*Server) Shutdown

func (s *Server) Shutdown(ctx context.Context) error

Shutdown gracefully shuts down the HTTP server.

func (*Server) Start

func (s *Server) Start(ctx context.Context) error

Start starts the HTTP server.

Jump to

Keyboard shortcuts

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