HTTPPipeline
Middleware
Add request pipeline behavior around endpoint execution.
Middleware runs before or around endpoint execution. It operates on the raw HTTP context and controls whether the next delegate should continue.
type Middleware interface {
// Invoke handles the current request and calls next to continue the pipeline.
Invoke(ctx *Context, next RequestDelegate) error
}Use middleware for cross-cutting HTTP behavior such as logging, correlation IDs, authentication, metrics, or request/response decoration.
type LoggingMiddleware struct {
logger *slog.Logger
}
func NewLoggingMiddleware(logger *slog.Logger) *LoggingMiddleware {
return &LoggingMiddleware{logger: logger}
}
func (m *LoggingMiddleware) Invoke(
ctx *http.Context,
next http.RequestDelegate,
) error {
start := time.Now()
err := next(ctx)
m.logger.Info("request completed",
"method", ctx.Request().Method(),
"path", ctx.Request().Path(),
"status", ctx.Response().Status(),
"duration", time.Since(start),
)
return err
}Call next(ctx) when the request should continue to the next middleware or
matched endpoint. Return early when the middleware should stop the pipeline.
