router

package
v0.36.0 Latest Latest
Warning

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

Go to latest
Published: Jul 7, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package router implements first-match HTTP request routing.

Routes are evaluated in order. The first route whose path pattern, domain pattern, and method filter match the incoming request is selected. If no route matches, the router returns nil and the caller should respond with 404.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type MatchResult

type MatchResult struct {
	RouteIndex    int               // index of the matched route in the service's route list
	Action        string            // resolved action name
	DomainPattern string            // the pattern from config, e.g. "*.myapp.dev"
	MatchDomain   string            // captured "*" wildcard value(s), e.g. "sub" for *.myapp.dev
	MatchGlob     string            // captured "**" glob suffix, e.g. "example.com" for *.storage.**
	MatchPath     string            // the path pattern, e.g. "/api/*"
	Domain        string            // actual request host (no port)
	Path          string            // actual request path
	Target        string            // selected target from balancer (empty if no balancer)
	Vars          map[string]string // route-level variables from "set"

	// Speed limiting — populated from route config.
	// Shared buckets are route-wide; BPS rates are for per-connection bucket creation.
	DownloadBucket *throttle.Bucket // shared download bucket (nil when per-conn or no limit)
	UploadBucket   *throttle.Bucket // shared upload bucket (nil when per-conn or no limit)
	DownloadBps    int64            // per-connection download bytes/sec (0 = no limit)
	UploadBps      int64            // per-connection upload bytes/sec (0 = no limit)
	// contains filtered or unexported fields
}

MatchResult holds data captured during route matching, available to handlers.

func GetMatchResult

func GetMatchResult(r *http.Request) *MatchResult

GetMatchResult retrieves the MatchResult from request context.

func (*MatchResult) Done

func (m *MatchResult) Done()

Done signals that the request/connection has completed. For connection-tracking balancers (e.g. leastconn), this decrements the active connection counter. Safe to call multiple times or on nil.

func (*MatchResult) RouteID

func (m *MatchResult) RouteID(service string) string

RouteID returns a compact identifier for this route (e.g. "web:0").

type Router

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

Router holds compiled routes for a single service.

func New

func New(routes []*config.Route) *Router

New compiles a list of config routes into a Router.

func (*Router) HasSpeed

func (rt *Router) HasSpeed() bool

HasSpeed returns true if any route in this router has speed limiting configured.

func (*Router) Match

func (rt *Router) Match(r *http.Request) (*http.Request, string)

Match finds the first matching route and returns the enriched request with MatchResult stored in context, plus the action name.

func (*Router) MatchAction

func (rt *Router) MatchAction(r *http.Request) string

MatchAction returns only the action name for the matching route, without allocating a MatchResult or modifying the request context. Used by the handler fast path when plugins and access logging are disabled.

func (*Router) RouteBalancer

func (rt *Router) RouteBalancer(index int) balancer.Balancer

RouteBalancer returns the balancer instance for the route at the given index. Returns nil if the index is out of range or the route has no balancer.

func (*Router) SetRouteBalancer

func (rt *Router) SetRouteBalancer(index int, bal balancer.Balancer)

SetRouteBalancer replaces the balancer for the route at the given index. Used to wrap a flat balancer in a Grouped balancer for plugin-managed routes.

Jump to

Keyboard shortcuts

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