{"modulePath":"github.com/AndreiD108/d108-go","version":"v0.2.0","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package httpserver\n\nPackage httpserver wraps http.Server with the request middleware of work (one request.handled per request, a span, panic recovery), GET /healthz, optional CORS and optional pprof. /healthz and /debug/pprof/ sit outside the middleware, so health checks and profiling write no lines.\n\n\tsrv := httpserver.New(log, routes, httpserver.Addr(\"127.0.0.1:8080\"), httpserver.CORS(\"https://app.example\"))\n\tlifecycle.OnStop(ctx, \"http\", func() error { return srv.Shutdown(context.Background()) })\n\treturn srv.ListenAndServe(ctx)\n\nUnder lifecycle.Run the stop step \"http\" is the server's drain: Shutdown waits for the running requests, so the steps registered before it (store, watcher) run after the drain, and ListenAndServe returns once the step has run.\n\nA daemon serves the same server on its Unix socket, or on a network address when other machines call it, through Serve:\n\n\tln, err := socket.Listen(ctx, socket.Path(paths.For(id.Name).Runtime, id.Name))\n\tif err != nil {\n\t\treturn err\n\t}\n\treturn srv.Serve(ctx, ln)\n\n## Constants\n\n```go\nconst ShutdownGrace = 10 * time.Second\n```\nShutdownGrace bounds the graceful stop ListenAndServe starts when its context is cancelled; connections still open after it are closed.\n\n## Types\n\n```go\ntype Option func(*Server)\n```\nOption changes how New builds the server.\n\n```go\nfunc Addr(addr string) Option\n```\nAddr sets the listen address (default \"127.0.0.1:8080\").\n\n```go\nfunc CORS(origins ...string) Option\n```\nCORS allows browsers at origins to call the API: Access-Control-Allow-Origin for a matching Origin header, and a 204 answer to preflight requests. \"\\*\" allows every origin.\n\n```go\nfunc Pprof() Option\n```\nPprof serves net/http/pprof under /debug/pprof/, outside the request middleware.\n\n```go\ntype Server struct {\n\t*http.Server\n\t// contains filtered or unexported fields\n}\n```\nServer is an http.Server with the contract's request handling around the app's handler.\n\n```go\nfunc New(log *slog.Logger, handler http.Handler, opts ...Option) *Server\n```\nNew builds the server: handler behind work.HTTP(log) and CORS, GET /healthz answering \"ok\", ReadHeaderTimeout 10 s and IdleTimeout 2 min. Every other field of the embedded http.Server may be set before ListenAndServe.\n\n```go\nfunc (s *Server) ListenAndServe(ctx context.Context) error\n```\nListenAndServe serves until Shutdown is called or ctx is cancelled, and returns nil in both cases. Request contexts carry ctx's values (logger, lifecycle) but not its cancellation, so a stop lets running requests finish. On ctx cancellation it calls Shutdown with ShutdownGrace and returns when that stop has finished: running requests are done or the grace has passed and their connections are closed.\n\n```go\nfunc (s *Server) Serve(ctx context.Context, l net.Listener) error\n```\nServe is ListenAndServe on a listener the caller made: a Unix socket from socket.Listen, or a network listener. It closes l when it returns.\n\n```go\nfunc (s *Server) Shutdown(ctx context.Context) error\n```\nShutdown stops accepting connections and waits for running requests until ctx ends; when ctx ends first the remaining connections are closed and ctx's error is returned.\n\n","path":"github.com/AndreiD108/d108-go/httpserver","name":"httpserver","synopsis":"Package httpserver wraps http.Server with the request middleware of work (one request.handled per request, a span, panic recovery), GET /healthz, optional CORS and optional pprof.","isRedistributable":true}
