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

httpserver

package
v1.21.2 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2026 License: GPL-3.0 Imports: 21 Imported by: 0

Documentation

Overview

Package httpserver builds the Hertz server of the HTTP API: the middleware every request passes (tracing, request logging, CORS), the routes of package routes, and the Telegram webhook and payment notify handlers that their modules register.

Index

Constants

View Source
const (
	// TrustNoProxy is the TrustedProxies entry that believes no forwarding
	// header: every client is the connection's peer. It is for a server its
	// clients reach directly, and it cannot be combined with other entries.
	TrustNoProxy = "none"
	// TrustPrivateNetworks is the entry that stands for the default networks
	// (appconfig.DefaultTrustedProxies) inside a longer list, so a public
	// proxy can be added without spelling the private ranges out.
	TrustPrivateNetworks = "private"
)
View Source
const (
	HealthzPath = "/healthz"
	ReadyzPath  = "/readyz"
)

Health endpoint paths, for the orchestrator's probes and the healthcheck command.

View Source
const (
	DefaultProbeTTL     = 5 * time.Second
	DefaultProbeTimeout = 2 * time.Second
)

DefaultProbeTTL and DefaultProbeTimeout are the probe cache lifetime and the bound on one round of pings when HealthDependencies names none.

Variables

This section is empty.

Functions

func ParseTrustedProxies added in v1.21.0

func ParseTrustedProxies(entries []string) ([]*net.IPNet, error)

ParseTrustedProxies parses the configured reverse proxies, each an IP address or a CIDR, into the networks whose X-Forwarded-For and X-Real-IP headers are believed. An empty configuration means the default networks, the entry "private" stands for them and ["none"] trusts nothing. It reports every entry that is none of these, so a typo does not silently widen or narrow the trust.

func RegisterHealthHandlers added in v1.21.0

func RegisterHealthHandlers(engine *server.Hertz, deps HealthDependencies)

RegisterHealthHandlers adds the liveness and readiness endpoints to engine. They carry no authentication and reveal nothing but a status and, when not ready, the name of the dependency that does not answer.

Types

type Dependencies

type Dependencies struct {
	Routes           routes.Dependencies
	Notification     notification.Service
	TelegramBotToken func() string
	RequestMetadata  requestmeta.Enricher
	// TrustedProxies lists the reverse proxies (IP addresses or CIDRs) whose
	// X-Forwarded-For and X-Real-IP headers name the client. Empty believes
	// no header: the client address is the connection's remote address.
	TrustedProxies []string
	// AllowedOrigins lists the browser origins CORS admits, as
	// scheme://host[:port]. Empty reflects the request's Origin.
	AllowedOrigins []string
	// HTTP bounds the listener; a zero field leaves Hertz's default.
	HTTP config.HTTPConfig
}

type HealthDependencies added in v1.21.0

type HealthDependencies struct {
	Bootstrapped Readiness
	Probes       []Probe
	// ProbeTTL is how long a probe result is reused, so readiness checks
	// cannot hammer the database and Redis; zero means DefaultProbeTTL.
	ProbeTTL time.Duration
	// ProbeTimeout bounds one round of pings; zero means
	// DefaultProbeTimeout.
	ProbeTimeout time.Duration
}

HealthDependencies is what the health endpoints report on.

type Probe added in v1.21.0

type Probe struct {
	Name string
	Ping func(ctx context.Context) error
}

Probe pings one dependency the server cannot serve without, such as the database or Redis. Name is what the readiness response reports when the ping fails; the error itself is only logged, so no address reaches a client.

type Readiness added in v1.21.0

type Readiness interface {
	Done() <-chan struct{}
	Err() error
}

Readiness is the runtime bootstrap's signal: Done closes once the runtime settings are loaded or the bootstrap failed, and Err reports the failure. The composition root's lifecycle.Readiness provides it.

type Server

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

func New

func New(deps Dependencies, addr string, tlsConfig *tls.Config) *Server

func (*Server) Engine

func (s *Server) Engine() *server.Hertz

func (*Server) Shutdown

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

Shutdown stops accepting connections and waits for the open requests until ctx ends; Start returns nil afterwards.

func (*Server) Start

func (s *Server) Start() (err error)

Start serves until Shutdown and returns the error that stopped the server before then, a listener that could not bind above all. It used to log that error only, leaving a process that consumed tasks with no API to serve them; the caller decides now, and fails fast.

Jump to

Keyboard shortcuts

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