Header-Based HTTP Routing

Overview

Toron supports Header-Based Routing, allowing requests to be routed to specific handlers or reverse proxy upstreams based on HTTP request header names and values (e.g. X-Version, X-Environment, X-Tenant-ID).

Common Use Cases

  • API Versioning: Route X-Version: v2 to new microservices while X-Version: v1 routes to legacy endpoints.
  • Canary & A/B Testing: Direct requests with X-Canary: true to experimental deployment targets.
  • Multi-Tenant Routing: Direct X-Tenant-ID: acme requests to isolated database or server clusters.

Configuration in routes.yaml

routes:
  - type: "upstream"
    prefix: "/api"
    headers:
      X-Version: "v2"
    target: "http://localhost:9092"

  - type: "upstream"
    prefix: "/api"
    headers:
      X-Version: "v1"
    target: "http://localhost:9091"

Programmatic Route Registration

r := router.New()

// Register route conditional on X-Version: v2 header
r.GETHeader("/api/data", "X-Version", "v2", func(req *httpparser.Request, res *httpparser.Response) {
    res.Header.Set("Content-Type", "application/json")
    _, _ = res.WriteString(`{"version":"v2"}`)
})

// Register fallback route with no header condition
r.GET("/api/data", func(req *httpparser.Request, res *httpparser.Response) {
    res.Header.Set("Content-Type", "application/json")
    _, _ = res.WriteString(`{"version":"v1"}`)
})

// Register header-conditional reverse proxy route
_ = r.ProxyHeader("/api", "X-Env", "staging", "http://staging-server:9090")
Toron · তোরণ GitHub