{"modulePath":"github.com/go-oryn/oryn/httpserver","version":"v0.0.0-20261108220520-b4a2bd7558f6","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package httpserver\n\nPackage httpserver provides an HTTP server built on [http.ServeMux](/net/http#ServeMux), instrumented with OpenTelemetry.\n\nHandlers, middlewares and handler groups are registered with [AsHandler](#AsHandler), [AsMiddleware](#AsMiddleware) and [AsHandlerGroup](#AsHandlerGroup), and built by fx with their dependencies. The server is started with [Serve](#Serve):\n\n\tcore.New(\n\t\thttpserver.Module,\n\t\thttpserver.AsMiddleware(NewAuthMiddleware),\n\t\thttpserver.AsHandler(\"GET /users/{id}\", NewGetUserHandler),\n\t).Run(httpserver.Serve())\n\n## Constants\n\n```go\nconst (\n\tDefaultAddress           = \":8080\"\n\tDefaultReadHeaderTimeout = 10 * time.Second\n\tDefaultIdleTimeout       = 2 * time.Minute\n)\n```\nDefaults of the config keys.\n\n```go\nconst ModuleName = \"httpserver\"\n```\nModuleName is the name of the httpserver fx module.\n\n## Variables\n\n```go\nvar Module = fx.Module(\n\tModuleName,\n\tfx.Provide(NewServer),\n)\n```\nModule provides the [\\*http.Server](/net/http#Server), with the registered handlers and middlewares.\n\n## Functions\n\n```go\nfunc AsHandler(pattern string, handler any, middlewares ...any) fx.Option\n```\nAsHandler registers a handler for the [http.ServeMux](/net/http#ServeMux) pattern (e.g. \"GET /users/{id}\"), with optional middlewares applied to this handler only, after the global ones.\n\nThe handler is either a value (an [http.Handler](/net/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](#AsMiddleware).\n\n```go\nfunc AsHandlerGroup(prefix string, options ...GroupOption) fx.Option\n```\nAsHandlerGroup 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\".\n\n\thttpserver.AsHandlerGroup(\"/api\",\n\t\thttpserver.WithMiddlewares(NewAuthMiddleware),\n\t\thttpserver.WithHandler(\"GET /users\", NewListUsersHandler),\n\t\thttpserver.WithHandler(\"POST /users\", NewCreateUserHandler, NewAuditMiddleware),\n\t\thttpserver.WithGroup(\"/admin\",\n\t\t\thttpserver.WithMiddlewares(NewAdminMiddleware),\n\t\t\thttpserver.WithHandler(\"GET /stats\", NewStatsHandler),\n\t\t),\n\t)\n\n```go\nfunc AsMiddleware(middlewares ...any) fx.Option\n```\nAsMiddleware registers global middlewares, applied to every handler in registration order: the first registered middleware is the outermost one.\n\nA middleware is either a value (a [Middleware](#Middleware), or a func(http.Handler) http.Handler), or a constructor returning one, whose parameters are injected by fx.\n\n```go\nfunc NewServer(params NewServerParams) (*http.Server, error)\n```\nNewServer returns the HTTP server. Every request goes through, from the outermost:\n\n 1. the OpenTelemetry instrumentation (spans and metrics), skipped for the excluded paths,\n 2. the request logs, unless disabled, and skipped for the excluded paths,\n 3. the panic recovery,\n 4. the routing, then the global, group and handler middlewares, and the handler.\n\n```go\nfunc Serve() fx.Option\n```\nServe starts the HTTP server with the application, on httpserver.address, and shuts it down gracefully when the application stops. Once started, the server Addr is the listening address (useful with port 0). If the server fails, the application is shut down.\n\n## Types\n\n```go\ntype GroupOption func(*group)\n```\nGroupOption configures a handler group, see [AsHandlerGroup](#AsHandlerGroup).\n\n```go\nfunc WithGroup(prefix string, options ...GroupOption) GroupOption\n```\nWithGroup adds a sub group, whose prefix is relative to the group one, and whose handlers also get the group middlewares. See [AsHandlerGroup](#AsHandlerGroup).\n\n```go\nfunc WithHandler(pattern string, handler any, middlewares ...any) GroupOption\n```\nWithHandler adds a handler to the group, with optional middlewares applied to this handler only, after the group middlewares. See [AsHandler](#AsHandler).\n\n```go\nfunc WithMiddlewares(middlewares ...any) GroupOption\n```\nWithMiddlewares 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.\n\n```go\ntype Middleware interface {\n\tWrap(next http.Handler) http.Handler\n}\n```\nMiddleware wraps a handler, to run code before and after it.\n\n```go\ntype MiddlewareFunc func(next http.Handler) http.Handler\n```\nMiddlewareFunc is a func(http.Handler) http.Handler [Middleware](#Middleware).\n\n```go\nfunc (f MiddlewareFunc) Wrap(next http.Handler) http.Handler\n```\nWrap calls f(next).\n\n```go\ntype NewServerParams struct {\n\tfx.In\n\n\tConfig         *config.Config\n\tLogger         *slog.Logger\n\tTracerProvider trace.TracerProvider\n\tMeterProvider  metric.MeterProvider\n\tPropagator     propagation.TextMapPropagator\n\tMiddlewares    []registeredMiddleware `group:\"oryn-httpserver-middlewares\"`\n\tRoutes         []route                `group:\"oryn-httpserver-routes\"`\n}\n```\nNewServerParams are the dependencies of [NewServer](#NewServer).\n\n","path":"github.com/go-oryn/oryn/httpserver","name":"httpserver","synopsis":"Package httpserver provides an HTTP server built on http.ServeMux, instrumented with OpenTelemetry.","isRedistributable":true}
