Documentation
¶
Overview ¶
Package httpserver provides an HTTP server built on http.ServeMux, instrumented with OpenTelemetry.
Handlers, middlewares and handler groups are registered with AsHandler, AsMiddleware and AsHandlerGroup, and built by fx with their dependencies. The server is started with Serve:
core.New(
httpserver.Module,
httpserver.AsMiddleware(NewAuthMiddleware),
httpserver.AsHandler("GET /users/{id}", NewGetUserHandler),
).Run(httpserver.Serve())
Index ¶
- Constants
- Variables
- func AsHandler(pattern string, handler any, middlewares ...any) fx.Option
- func AsHandlerGroup(prefix string, options ...GroupOption) fx.Option
- func AsMiddleware(middlewares ...any) fx.Option
- func NewServer(params NewServerParams) (*http.Server, error)
- func Serve() fx.Option
- type GroupOption
- type Middleware
- type MiddlewareFunc
- type NewServerParams
Constants ¶
const ( DefaultAddress = ":8080" DefaultReadHeaderTimeout = 10 * time.Second DefaultIdleTimeout = 2 * time.Minute )
Defaults of the config keys.
const ModuleName = "httpserver"
ModuleName is the name of the httpserver fx module.
Variables ¶
var Module = fx.Module( ModuleName, fx.Provide(NewServer), )
Module provides the *http.Server, with the registered handlers and middlewares.
Functions ¶
func AsHandler ¶
AsHandler registers a handler for the http.ServeMux pattern (e.g. "GET /users/{id}"), with optional middlewares applied to this handler only, after the global ones.
The handler is either a value (an http.Handler, or a func(http.ResponseWriter, *http.Request)), or a constructor returning one, whose parameters are injected by fx. Middlewares are described in AsMiddleware.
func AsHandlerGroup ¶
func AsHandlerGroup(prefix string, options ...GroupOption) fx.Option
AsHandlerGroup registers a group of handlers sharing a path prefix (e.g. "/api/v1") and middlewares. The patterns of the group handlers are relative to the prefix: "GET /users" in the group "/api" serves "GET /api/users".
httpserver.AsHandlerGroup("/api",
httpserver.WithMiddlewares(NewAuthMiddleware),
httpserver.WithHandler("GET /users", NewListUsersHandler),
httpserver.WithHandler("POST /users", NewCreateUserHandler, NewAuditMiddleware),
httpserver.WithGroup("/admin",
httpserver.WithMiddlewares(NewAdminMiddleware),
httpserver.WithHandler("GET /stats", NewStatsHandler),
),
)
func AsMiddleware ¶
AsMiddleware registers global middlewares, applied to every handler in registration order: the first registered middleware is the outermost one.
A middleware is either a value (a Middleware, or a func(http.Handler) http.Handler), or a constructor returning one, whose parameters are injected by fx.
func NewServer ¶
func NewServer(params NewServerParams) (*http.Server, error)
NewServer returns the HTTP server. Every request goes through, from the outermost:
- the OpenTelemetry instrumentation (spans and metrics), skipped for the excluded paths,
- the request logs, unless disabled, and skipped for the excluded paths,
- the panic recovery,
- the routing, then the global, group and handler middlewares, and the handler.
Types ¶
type GroupOption ¶
type GroupOption func(*group)
GroupOption configures a handler group, see AsHandlerGroup.
func WithGroup ¶
func WithGroup(prefix string, options ...GroupOption) GroupOption
WithGroup adds a sub group, whose prefix is relative to the group one, and whose handlers also get the group middlewares. See AsHandlerGroup.
func WithHandler ¶
func WithHandler(pattern string, handler any, middlewares ...any) GroupOption
WithHandler adds a handler to the group, with optional middlewares applied to this handler only, after the group middlewares. See AsHandler.
func WithMiddlewares ¶
func WithMiddlewares(middlewares ...any) GroupOption
WithMiddlewares adds middlewares to all the handlers of the group, including its sub groups, wherever the option is placed. They are applied after the global and parent group middlewares.
type Middleware ¶
Middleware wraps a handler, to run code before and after it.
type MiddlewareFunc ¶
MiddlewareFunc is a func(http.Handler) http.Handler Middleware.
type NewServerParams ¶
type NewServerParams struct {
fx.In
Config *config.Config
Logger *slog.Logger
TracerProvider trace.TracerProvider
MeterProvider metric.MeterProvider
Propagator propagation.TextMapPropagator
Middlewares []registeredMiddleware `group:"oryn-httpserver-middlewares"`
Routes []route `group:"oryn-httpserver-routes"`
}
NewServerParams are the dependencies of NewServer.