httpsrv

package
v0.44.2 Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2026 License: MIT Imports: 11 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 httpsrv is the listener plumbing behind the hub's four endpoints (tunnel, admin, proxy, metrics). It gives them one server shape — h2c, with the timeouts a public-facing listener needs — one place to start them, and one place to drain them on shutdown.

A Group owns the servers it starts, so the hub command reads as the list of listeners it brings up:

g := httpsrv.NewGroup(logger)
if err := g.Start(ctx, "tunnel", addr, handler, httpsrv.MaxConns(n)); err != nil {
	return err
}
...
forced := g.Drain(5 * time.Second)

Start binds synchronously and serves in the background, so a port already in use fails at boot with the address in the error, while a serve error later in the process's life is logged under the listener's name.

Every listener here is plaintext: transport encryption is the deployment's job (a TLS edge, ingress, or mesh in front of the hub).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HostGuard

func HostGuard(allowed []string, next http.Handler) http.Handler

HostGuard rejects requests whose Host header is not allow-listed, which is what stops a DNS-rebinding page from driving the plaintext, loopback-served console and its token-minting endpoint. Loopback names are always allowed; a deployment exposed through a proxy adds its public hostname. A single "*" entry disables the check.

Types

type Group

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

Group starts HTTP listeners and drains them together. Start it from one goroutine (a boot sequence) and Drain it after: the group is not safe for concurrent Starts.

func NewGroup

func NewGroup(logger *zap.Logger) *Group

NewGroup returns an empty group. Serve errors are logged to logger under the name each listener was started with.

func (*Group) Drain

func (g *Group) Drain(grace time.Duration) bool

Drain shuts every started listener down gracefully, giving in-flight requests up to grace to finish. It reports whether a second signal (a second Ctrl-C during the grace period) forced the close instead: the caller decides what to say about that, Drain only decides when to stop waiting.

The parent context already consumed the first signal, so Drain re-arms its own handler for the duration of the drain.

func (*Group) Start

func (g *Group) Start(ctx context.Context, name, addr string, handler http.Handler, opts ...Option) error

Start binds addr and serves handler on it in the background. The bind happens before returning, so an address in use is reported here; name labels the listener in any later serve error.

type Option

type Option func(net.Listener) net.Listener

Option adapts a listener before it is served, e.g. to cap connections.

func MaxConns

func MaxConns(n int) Option

MaxConns caps concurrent connections on the listener. A value of 0 or less leaves it uncapped.

Jump to

Keyboard shortcuts

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