httpserver

package
v0.0.0-...-d267593 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package httpserver owns Forge's secure HTTP server defaults and lifecycle.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Middleware

type Middleware func(http.Handler) http.Handler

Middleware wraps an HTTP handler. Middleware is applied in the order passed to NewWithMiddleware: the first middleware is the outermost custom layer. Forge's request ID, client address resolution (web.ClientIP) and security headers remain outside custom middleware; custom middleware remains inside body limiting and panic recovery.

type Options

type Options struct {
	// Instrument wraps the chain directly inside the request-ID layer and
	// outside panic recovery, so it observes every response, including
	// recovered panics and 413s, and can extend the request logger. Forge
	// installs its telemetry middleware here. Nil means none.
	Instrument Middleware
	// Middlewares are application middleware, as for NewWithMiddleware.
	Middlewares []Middleware
	// Rewrite runs once per request after the rewrites of Middlewares that
	// return a Rewriter and before the first of Middlewares. forge.App runs
	// the rewrites of the middleware added with App.Use here. Nil means
	// none.
	Rewrite func(*http.Request) *http.Request
}

Options configure NewWithOptions.

type Rewriter

type Rewriter interface {
	RewriteRequest(r *http.Request) *http.Request
}

Rewriter is implemented by a handler a Middleware returns when the middleware rewrites the request for routing, as i18n.Translator.Middleware does with NegotiationOptions.PathPrefix (/pt-BR/admin is routed as /admin). The server calls RewriteRequest of every such middleware, in registration order, once per request and before the first application middleware runs, so every middleware sees the request path the router uses whatever its position: a path guard registered before the rewriting middleware cannot be bypassed by the part the rewrite removes. RewriteRequest returns a non-nil request, must be idempotent for the request it returns (the middleware's ServeHTTP may apply it again) and cannot answer the request. Request IDs, telemetry and the panic log run before it and see the request as sent.

type Server

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

Server is Forge's HTTP server: the standard library server behind a fixed chain of request ID, client address, security headers, body limit and panic recovery.

func New

func New(cfg config.Config, handler http.Handler, logger *slog.Logger) (*Server, error)

New returns a Server for handler with the configuration's timeouts, limits and transport.

func NewWithMiddleware

func NewWithMiddleware(
	cfg config.Config,
	handler http.Handler,
	logger *slog.Logger,
	middlewares ...Middleware,
) (*Server, error)

NewWithMiddleware constructs the standard Forge server with optional application middleware around the handler.

func NewWithOptions

func NewWithOptions(cfg config.Config, handler http.Handler, logger *slog.Logger, opts Options) (*Server, error)

NewWithOptions constructs the standard Forge server with an optional instrumentation layer and application middleware.

func (*Server) Handler

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

Handler returns the server's complete handler, so it can be tested without listening.

func (*Server) Run

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

Run serves until ctx is cancelled and then shuts down gracefully, waiting up to the configured shutdown timeout for in-flight requests and then closing their connections. It returns nil after a clean stop and an error for a listener failure or a forced close.

Jump to

Keyboard shortcuts

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