Help us shape the future of Go! Take the 2026 Go Developer Survey

httpserver

package
v1.119.0 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Overview

Package httpserver builds the HTTP transport every service in this repo runs on: CORS, access logging with correlation IDs, panic recovery, and the Serve/Shutdown lifecycle pkg/boot.Service expects. A service package supplies only its own routes and gets the rest for free instead of rebuilding http.Server and its middleware chain by hand.

One Server hosts any number of Registrations on one mux behind one CORS and access-logging layer; http.ServeMux precedence decides which route a request gets, and panics at boot on a pattern two of them claim.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Check added in v1.86.1

type Check func(ctx context.Context) error

Check reports whether a registration can serve; a non-nil error says why not. It runs on every merged /health request, so it must be cheap and bounded, e.g. a database ping with its own timeout.

type Config

type Config struct {
	Name      string
	Component string
	Address   string

	CORS     config.CORSConfig
	Timeouts config.HTTPDefaults

	// Metrics, when non-nil, adds request metrics to the chain. Set by
	// pkg/boot.Boot for every service, so a service package never decides
	// whether its HTTP surface is instrumented.
	Metrics *metrics.Metrics

	// Registrations are the hosted services: one for a standalone binary.
	Registrations []Registration
}

Config is what pkg/boot.Boot builds from Registrations plus what only the platform layer decides: name, listen address, and the CORS/timeout defaults every HTTP server in this repo shares.

type Registration

type Registration struct {
	// SetupRoutes registers the service's handlers on mux. Required.
	SetupRoutes func(mux *http.ServeMux)

	// HealthCheck is what Merge folds into the consolidated binary's one
	// /health; a registration without one counts as healthy there. New does
	// not read it: a service running alone serves its own health route from
	// SetupRoutes.
	HealthCheck Check

	// Closers are closed, in order, after the HTTP server has shut down.
	Closers []io.Closer
}

Registration is what a service provides: its routes, never the transport around them.

func Merge added in v1.86.1

func Merge(first Registration, rest ...Registration) Registration

Merge hosts several registrations as one, for the consolidated binary. Each registration's routes live on their own mux and a request goes to the first one, in argument order, with a matching route; a path two of them claim therefore does not panic at boot. The merged /health shadows theirs with the body auth's own /health sends, ok only when every HealthCheck passes. Closers are concatenated.

type Server

type Server struct {
	// contains filtered or unexported fields
}

func New

func New(cfg Config) (*Server, error)

New builds a Server hosting cfg.Registrations. Like http.ServeMux it panics on programmer error: an empty registration list, or a pattern two registrations both claim. It returns an error only if the request metrics can't be registered.

func (*Server) Address

func (s *Server) Address() string

func (*Server) Handler

func (s *Server) Handler() http.Handler

Handler returns the fully-wrapped handler: request metrics outermost when configured, then access logging, then panic recovery, then CORS.

func (*Server) Logger

func (s *Server) Logger() *logrus.Entry

func (*Server) Name

func (s *Server) Name() string

func (*Server) Serve

func (s *Server) Serve() error

func (*Server) Shutdown

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

Jump to

Keyboard shortcuts

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