Documentation
¶
Overview ¶
Package httpserver owns Forge's secure HTTP server defaults and lifecycle.
Index ¶
- type Middleware
- type Options
- type Rewriter
- type Server
- func New(cfg config.Config, handler http.Handler, logger *slog.Logger) (*Server, error)
- func NewWithMiddleware(cfg config.Config, handler http.Handler, logger *slog.Logger, ...) (*Server, error)
- func NewWithOptions(cfg config.Config, handler http.Handler, logger *slog.Logger, opts Options) (*Server, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Middleware ¶
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 ¶
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 ¶
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 ¶
Handler returns the server's complete handler, so it can be tested without listening.