quickproxy

package
v1.2.3 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package quickproxy provides a small reverse-proxy layer that tries a set of configured upstream targets first, and falls back to a caller-supplied http.Handler (typically static file serving) when the upstream is unreachable or returns 404.

Index

Constants

View Source
const DefaultTimeout = 10 * time.Second

DefaultTimeout is the dial and response-header timeout applied to upstream requests when no WithTimeout option is given. It does not limit response body streaming.

Variables

This section is empty.

Functions

This section is empty.

Types

type Option

type Option func(*options)

Option configures a Service.

func WithTimeout

func WithTimeout(d time.Duration) Option

WithTimeout sets the dial and response-header timeout used when connecting to upstream targets. It does not limit response body streaming, so it won't interrupt long-lived downloads or SSE/WebSocket connections once established.

type Rule

type Rule struct {
	// URLPrefix is the URL path prefix this rule matches. Must start with "/".
	URLPrefix string

	// Target is the upstream base URL, e.g. "http://localhost:3000".
	// The incoming request path and query are forwarded unchanged; only the
	// scheme and host are rewritten to Target's.
	Target string

	// Exclude is a list of URL path prefixes that this rule should not
	// proxy, even though they fall under URLPrefix. Each entry is a full
	// path from root and must itself start with URLPrefix (e.g. rule
	// URLPrefix "/api" excluding a subpath must use "/api/health", not
	// "/health"). A request matching an Exclude prefix is treated as if
	// this rule didn't match at all: matching continues against any other
	// configured rule, falling back if none match. This is typically used
	// to carve out paths (e.g. "/health") from a catch-all "/" rule so
	// they're served by the fallback handler instead of being proxied.
	Exclude []string
}

Rule maps a URL path prefix to an upstream target. A Rule with URLPrefix "/" acts as a catch-all passthrough.

type Service

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

Service holds a compiled set of proxy rules and performs longest-prefix matching against them. A Service is safe for concurrent use once returned from NewService; Handler must be called once per Service to wire up the fallback handler before the returned http.Handler is served.

func NewService

func NewService(rules []Rule, opts ...Option) (*Service, error)

NewService compiles the given rules into a Service. Rules are matched by longest URLPrefix, so a catch-all "/" rule can coexist with more specific rules such as "/api".

func (*Service) Handler

func (s *Service) Handler(fallback http.Handler) http.Handler

Handler returns an http.Handler that tries the configured proxy rules first (longest-prefix match), and calls fallback when no rule matches, the upstream is unreachable, or the upstream returns 404. Any other upstream response (2xx, other 4xx, 5xx) is streamed through to the client unchanged.

Handler wires up ErrorHandler on the Service's compiled rules, so it should be called once per Service, before the returned http.Handler starts serving requests.

Jump to

Keyboard shortcuts

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