httpserver

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2026 License: MIT Imports: 9 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

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. /healthz and /debug/pprof/ sit outside the middleware, so health checks and profiling write no lines.

srv := httpserver.New(log, routes, httpserver.Addr("127.0.0.1:8080"), httpserver.CORS("https://app.example"))
lifecycle.OnStop(ctx, "http", func() error { return srv.Shutdown(context.Background()) })
return srv.ListenAndServe(ctx)

Under 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.

A daemon serves the same server on its Unix socket, or on a network address when other machines call it, through Serve:

ln, err := socket.Listen(ctx, socket.Path(paths.For(id.Name).Runtime, id.Name))
if err != nil {
	return err
}
return srv.Serve(ctx, ln)

Index

Constants

View Source
const ShutdownGrace = 10 * time.Second

ShutdownGrace bounds the graceful stop ListenAndServe starts when its context is cancelled; connections still open after it are closed.

Variables

This section is empty.

Functions

This section is empty.

Types

type Option

type Option func(*Server)

Option changes how New builds the server.

func Addr

func Addr(addr string) Option

Addr sets the listen address (default "127.0.0.1:8080").

func CORS

func CORS(origins ...string) Option

CORS 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.

func Pprof

func Pprof() Option

Pprof serves net/http/pprof under /debug/pprof/, outside the request middleware.

type Server

type Server struct {
	*http.Server
	// contains filtered or unexported fields
}

Server is an http.Server with the contract's request handling around the app's handler.

func New

func New(log *slog.Logger, handler http.Handler, opts ...Option) *Server

New 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.

func (*Server) ListenAndServe

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

ListenAndServe 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.

func (*Server) Serve added in v0.2.0

func (s *Server) Serve(ctx context.Context, l net.Listener) error

Serve is ListenAndServe on a listener the caller made: a Unix socket from socket.Listen, or a network listener. It closes l when it returns.

func (*Server) Shutdown

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

Shutdown 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.

Jump to

Keyboard shortcuts

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