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

proxy

package
v1.4.1 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2026 License: MIT Imports: 21 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var IPAccessRules = newIPAccessRegistry()

IPAccessRules is the package-level registry used by the Caddy handler.

View Source
var RateLimiters = &RateLimiterRegistry{
	limiters: make(map[string]*domainLimiter),
}

RateLimiters is the package-level registry used by the Caddy handler.

View Source
var RequestStatsCh chan<- RequestStatEvent

RequestStatsCh is set before Caddy starts; metrics are sent here.

Functions

func NormalizePath

func NormalizePath(path string) string

NormalizePath replaces dynamic path segments (numeric IDs, UUIDs) with {id}.

Types

type CaddyConfig

type CaddyConfig struct {
	ListenAddr       string   // e.g. ":443"
	HTTPListenAddr   string   // optional HTTP listener for plain-HTTP to HTTPS redirect, e.g. ":80"
	ExtraListenAddrs []string // optional extra listeners for the main server, e.g. [":50051"]; same routes and TLS
	TLSMode          string   // "auto", "custom", "off", "local"
	TLSEmail         string   // ACME email, used when TLSMode is "auto"
	DataDir          string   // data directory for Caddy storage
}

CaddyConfig holds configuration for a CaddyProxy.

type CaddyProxy

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

CaddyProxy is a Proxy backed by Caddy.

func NewCaddyProxy

func NewCaddyProxy(cfg CaddyConfig) *CaddyProxy

NewCaddyProxy creates a CaddyProxy from the given config.

func (*CaddyProxy) BuildConfigJSON

func (c *CaddyProxy) BuildConfigJSON() ([]byte, error)

BuildConfigJSON builds and returns the Caddy JSON config. Exported for testing.

func (*CaddyProxy) ForceReload added in v1.4.1

func (c *CaddyProxy) ForceReload() error

ForceReload reloads Caddy with the current routes even if the generated config is unchanged. Use after files referenced by the config change on disk (e.g. a custom cert upload rewrites the same path).

func (*CaddyProxy) SetRoutes

func (c *CaddyProxy) SetRoutes(routes []Route) error

SetRoutes stores routes, configures rate limiters, and reloads Caddy config.

func (*CaddyProxy) Stop

func (c *CaddyProxy) Stop() error

Stop stops all Caddy instances.

type DockerResolver

type DockerResolver struct {
	Client dockerInspector
}

DockerResolver resolves <service>:<port> upstreams to <container-ip>:<port> via Docker API, using compose project/service labels.

func (*DockerResolver) ContainerIP

func (r *DockerResolver) ContainerIP(project, service, netName string) (string, error)

ContainerIP returns the IP of the first running container matching the given compose project and service, on the given docker network. Returns "" (no error) when the container exists but is not yet attached to the network, or when no container matches. Returns a non-nil error only on Docker failures.

type IPAccessHandler

type IPAccessHandler struct{}

IPAccessHandler is a Caddy middleware that enforces per-domain IP allowlists.

func (IPAccessHandler) CaddyModule

func (IPAccessHandler) CaddyModule() caddy.ModuleInfo

func (*IPAccessHandler) Provision

func (h *IPAccessHandler) Provision(_ caddy.Context) error

func (*IPAccessHandler) ServeHTTP

func (*IPAccessHandler) Validate

func (h *IPAccessHandler) Validate() error

type MockProxy

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

MockProxy is an in-memory Proxy for testing.

func NewMockProxy

func NewMockProxy() *MockProxy

NewMockProxy creates a new MockProxy.

func (*MockProxy) HasRoute

func (m *MockProxy) HasRoute(domain string) bool

HasRoute reports whether a route with the given domain exists.

func (*MockProxy) Routes

func (m *MockProxy) Routes() []Route

Routes returns a copy of the current routes.

func (*MockProxy) SetRoutes

func (m *MockProxy) SetRoutes(routes []Route) error

SetRoutes stores routes.

func (*MockProxy) Stop

func (m *MockProxy) Stop() error

Stop marks the proxy as stopped.

type Proxy

type Proxy interface {
	SetRoutes(routes []Route) error
	Stop() error
}

Proxy manages reverse-proxy routes.

type RateLimitConfig

type RateLimitConfig struct {
	Requests int
	Window   time.Duration
	Burst    int
	By       string
}

RateLimitConfig holds parsed rate-limit settings for a route.

type RateLimitHandler

type RateLimitHandler struct{}

RateLimitHandler is a Caddy middleware that enforces per-domain rate limits.

func (RateLimitHandler) CaddyModule

func (RateLimitHandler) CaddyModule() caddy.ModuleInfo

func (*RateLimitHandler) Provision

func (h *RateLimitHandler) Provision(_ caddy.Context) error

func (*RateLimitHandler) ServeHTTP

func (*RateLimitHandler) Validate

func (h *RateLimitHandler) Validate() error

type RateLimiterRegistry

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

RateLimiterRegistry maps domains to their per-domain limiter.

func (*RateLimiterRegistry) Allow

func (reg *RateLimiterRegistry) Allow(domain string, r *http.Request) bool

Allow returns true if the request for domain should be allowed. Returns true when no limiter is configured for the domain.

func (*RateLimiterRegistry) Set

func (reg *RateLimiterRegistry) Set(domain string, cfg *RateLimitConfig)

Set registers or replaces the rate limit config for domain.

type RequestMetrics

type RequestMetrics struct{}

RequestMetrics is a Caddy middleware that records request stats.

func (RequestMetrics) CaddyModule

func (RequestMetrics) CaddyModule() caddy.ModuleInfo

func (*RequestMetrics) Provision

func (m *RequestMetrics) Provision(_ caddy.Context) error

func (*RequestMetrics) ServeHTTP

func (*RequestMetrics) Validate

func (m *RequestMetrics) Validate() error

type RequestStatEvent

type RequestStatEvent struct {
	Domain     string
	StatusCode int
	LatencyMs  float64
	Method     string
	Path       string
}

RequestStatEvent carries per-request stats.

type Route

type Route struct {
	AppSlug    string
	Domain     string
	Upstream   string // "localhost:{port}"
	TLS        string // "auto", "custom", "off"
	CertDir    string // directory containing certs for custom TLS
	RateLimit  *RateLimitConfig
	AllowedIPs []string // validated IPs and CIDRs
	Protocol   string   // "http" (default), "h2c" or "grpc"
	Path       string   // optional Caddy path matcher, e.g. "/ws*"
}

Route holds routing config for a deployed app.

func ResolveRoutes

func ResolveRoutes(app *compose.AppConfig, resolver UpstreamResolver) ([]Route, error)

ResolveRoutes derives Routes from an AppConfig (one per endpoint). Returns empty slice if no endpoints are defined (no error). If resolver is non-nil, endpoints without a published host port try to resolve to <container-ip>:<port> via Docker API and fall back to DNS on failure or empty result.

type UpstreamResolver

type UpstreamResolver interface {
	ContainerIP(project, service, network string) (string, error)
}

UpstreamResolver resolves a compose service+port to a concrete upstream host string. Implementations may use Docker API lookups to map a service name to a container IP on the shared network. A nil resolver disables container-IP resolution and callers fall through to DNS.

Jump to

Keyboard shortcuts

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