fox

package module
v0.31.0 Latest Latest
Warning

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

Go to latest
Published: Jul 13, 2026 License: Apache-2.0 Imports: 39 Imported by: 8 modules, 3 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.

README

Fox

Go Reference tests codecov GitHub release (latest SemVer) GitHub go.mod Go version

[!NOTE] This repository has been transferred from github.com/tigerwill90/fox to github.com/fox-toolkit/fox. Existing users should update their imports and go.mod accordingly.

Fox is a lightweight and high performance HTTP request router for Go, designed for building reverse proxies, API gateways, or other applications that require managing routes at runtime based on configuration changes or external events. It is also well-suited for general use cases such as microservices and REST APIs, though it focuses on routing and does not include convenience helpers found in full-featured frameworks, such as automatic binding, content negotiation, file uploads, cookies, etc.

Fox supports mutation on its routing tree while handling requests concurrently. Internally, it uses a Radix Tree that supports lock-free reads while allowing a concurrent writer, and is optimized for high-concurrency reads and low-concurrency writes. The router supports complex routing patterns, enforces clear priority rules, and performs strict validation to prevent misconfigurations.

Disclaimer

The current API is not yet stabilized. Breaking changes may occur before v1.0.0 and will be noted on the release note.

Features

Runtime updates: Register, update and delete route handler safely at any time without impact on performance.

Flexible routing: Fox strikes a balance between routing flexibility, performance and clarity by enforcing clear priority rules, ensuring that there are no unintended matches and maintaining high performance even for complex routing patterns. Supported features include named parameters, suffix and infix catch-all, regexp constraints, hostname matching, method and method-less routes, and route matchers.

Trailing slash handling: Automatically handle trailing slash inconsistencies by either ignoring them, redirecting to the canonical path, or enforcing strict matching based on your needs.

Path normalization: Automatically handle requests with extra slashes or dot segments by either serving the normalized path directly or redirecting to the canonical form.

Automatic OPTIONS replies: Fox has built-in native support for OPTIONS requests.

Client IP Derivation: Accurately determine the "real" client IP address using best practices tailored to your network topology.

Growing middleware ecosystem: Fox's middleware ecosystem is still limited, but most standard http.Handler middleware should be compatible.



Getting started

Install

With a correctly configured Go toolchain:

go get -u github.com/fox-toolkit/fox

This library requires Go 1.26.0 or above. Per the Go release policy, it only supports the two most recent major releases of Go, i.e. 1.26 and 1.25.

Basic example
package main

import (
	"log"
	"net/http"
	"net/http/httputil"
	"net/url"

	"github.com/fox-toolkit/fox"
)

func proxy(backend string) fox.HandlerFunc {
	target, err := url.Parse(backend)
	if err != nil {
		panic(err)
	}
	rp := &httputil.ReverseProxy{
		Rewrite: func(r *httputil.ProxyRequest) {
			r.SetURL(target)
			r.SetXForwarded()
			r.Out.Host = r.In.Host
		},
	}
	return func(c *fox.Context) {
		rp.ServeHTTP(c.Writer(), c.Request())
	}
}

func main() {
	f := fox.MustRouter(fox.WithAllowRegexpParam(true))

	f.MustAdd(fox.MethodAny, "api.example.com/orders/{id:[0-9]+}", proxy("http://orders.internal"))
	f.MustAdd(fox.MethodAny, "api.example.com/orders/{ref}", proxy("http://legacy.internal"))
	f.MustAdd(fox.MethodAny, "api.example.com/files/+{path}/preview", proxy("http://imaging.internal"))
	f.MustAdd(fox.MethodAny, "api.example.com/files/+{path}", proxy("http://storage.internal"))
	f.MustAdd([]string{http.MethodGet, http.MethodHead}, "www.example.com/*{path}", proxy("http://frontend.internal"))

	f.MustAdd(fox.MethodGet, "/health", func(c *fox.Context) {
		_ = c.String(http.StatusOK, "ok")
	})

	log.Fatalln(http.ListenAndServe(":8080", f))
}
Named parameters

Routes can include named parameters using curly braces {name} to match exactly one non-empty route segment. The matching segments are recorded as Param and accessible via the Context. Named parameters are supported anywhere in the route, but only one parameter is allowed per segment (or hostname label) and must appear at the end of the segment.

Pattern /avengers/{name}

/avengers/ironman               matches
/avengers/thor                  matches
/avengers/hulk/angry            no matches
/avengers/                      no matches

Pattern /users/uuid:{id}

/users/uuid:123                 matches
/users/uuid:                    no matches

Pattern /users/uuid:{id}/config

/users/uuid:123/config          matches
/users/uuid:/config             no matches

Pattern {sub}.example.com/avengers

first.example.com/avengers      matches
example.com/avengers           no matches

Named parameters can include regular expression using the syntax {name:regexp}. Regular expressions cannot contain capturing groups, but can use non-capturing groups (?:pattern) instead. Regexp support is opt-in via fox.WithAllowRegexpParam(true) option.

Pattern /products/{name:[A-Za-z]+}

/products/laptop        matches
/products/123           no matches
Named Wildcards (Catch-all)

Named wildcard start with a plus sign + followed by a name {param} and match any sequence of characters including slashes, but cannot match an empty string. The matching segments are also accessible via Context. Catch-all parameters are supported anywhere in the route, but only one parameter is allowed per segment (or hostname label) and must appear at the end of the segment. Consecutive catch-all parameter are not allowed.

Pattern /src/+{filepath}

/src/conf.txt                      matches
/src/dir/config.txt                 matches
/src/                              no matches

Pattern /src/file=+{path}

/src/file=config.txt                 matches
/src/file=/dir/config.txt            matches
/src/file=                          no matches

Pattern: /assets/+{path}/thumbnail

/assets/images/thumbnail           matches
/assets/photos/2021/thumbnail      matches
/assets//thumbnail                 no matches

Pattern +{sub}.example.com/avengers

first.example.com/avengers          matches
first.second.example.com/avengers   matches
example.com/avengers               no matches

Optional named wildcard start with an asterisk * followed by a name {param} and match any sequence of characters including empty strings. Unlike +{param}, optional wildcards can only be used as a suffix.

Pattern /src/*{filepath}

/src/conf.txt                      matches
/src/dir/config.txt                 matches
/src/                              matches

Pattern /src/file=*{path}

/src/file=config.txt                 matches
/src/file=/dir/config.txt            matches
/src/file=                          matches

Named wildcards can include a regular expression constraint using the syntax +{name:regexp}. Regular expressions cannot contain capturing groups, but can use non-capturing groups (?:pattern) instead. Optional wildcards (*{param}) do not support regular expressions. Regexp support is opt-in via fox.WithAllowRegexpParam(true) option.

Pattern /src/+{filepath:[A-Za-z/]+\.json}

/src/dir/config.json            matches
/src/dir/config.txt             no matches
Route matchers

Route matchers enable routing decisions based on request properties beyond methods, hostname and path. Multiple routes can share the same pattern and methods and be differentiated by query parameters, headers, client IP, or custom criteria.

f.MustAdd(fox.MethodAny, "api.example.com/checkout/+{path}", proxy("http://checkout-v2.internal"), fox.WithHeaderMatcher("X-Canary", "on"))
f.MustAdd(fox.MethodAny, "api.example.com/checkout/+{path}", proxy("http://checkout-v2.internal"), fox.WithClientIPMatcher("10.0.0.0/8"))
f.MustAdd(fox.MethodAny, "api.example.com/checkout/+{path}", proxy("http://checkout.internal"))

Built-in matchers include fox.WithQueryMatcher, fox.WithQueryRegexpMatcher, fox.WithHeaderMatcher, fox.WithHeaderRegexpMatcher, fox.WithSchemeMatcher and fox.WithClientIPMatcher. Multiple matchers on a route use AND logic. Routes without matchers serve as fallbacks. For custom matching logic, implement the fox.Matcher interface and use fox.WithMatcher. See Priority rules for matcher evaluation order.

Method-less routes

Routes can be registered without specifying an HTTP method to match any method. The constant fox.MethodAny is a convenience placeholder equivalent to an empty method set (nil or empty slice).

// Handle any method on /health
f.MustAdd(fox.MethodAny, "/health", HealthHandler)
// Forward all requests to a backend service
f.MustAdd(fox.MethodAny, "/api/*{any}", ProxyHandler)

Routes registered with a specific HTTP method always take precedence over method-less routes. This allows defining method-specific behavior while falling back to a generic handler for other methods.

// Specific handler for GET requests
f.MustAdd(fox.MethodGet, "/resource", GetHandler)
// All other methods handled here
f.MustAdd(fox.MethodAny, "/resource", FallbackHandler)
Hostname validation & restrictions

Hostnames are validated to conform to the LDH (letters, digits, hyphens) rule (lowercase only) and SRV-like "underscore labels". Wildcard segments within hostnames, such as {sub}.example.com/, are exempt from LDH validation since they act as placeholders rather than actual domain labels. As such, they do not count toward the hard limit of 63 characters per label, nor the 253-character limit for the full hostname. Internationalized domain names (IDNs) should be specified using an ASCII (Punycode) representation.

Path encoding

Fox matches requests against a canonical routing path. Percent-encoded unreserved characters (A-Z a-z 0-9 - . _ ~) are decoded, while every other escape sequence is normalized to uppercase hex (%2f becomes %2F) and stays distinct from its decoded form, so /foo%2Fbar and /foo/bar are different routing paths. Characters that can never appear unescaped in a path, like é or {, are percent-encoded.

Priority rules

The router is designed to balance routing flexibility, performance, and predictability. Internally, it uses a radix tree to store routes efficiently. When a request arrives, Fox evaluates routes in the following order:

  1. Hostname matching

    • Routes with hostnames are evaluated before path-only routes
  2. Pattern matching (longest match, most specific first)

    • Static segments
    • Named parameters with regex constraints
    • Named parameters without constraints
    • Catch-all parameters with regex constraints
    • Catch-all parameters without constraints
    • Infix catch-all are evaluated before suffix catch-all (e.g., /bucket/+{path}/meta before /bucket/+{path})
    • At the same level, multiple regex-constrained parameters are evaluated in registration order
  3. Method matching

    • Routes with specific methods are evaluated before method-less routes
  4. Matcher evaluation (for routes sharing the same pattern and overlapping methods)

    • Routes with matchers are evaluated before routes without
    • Among routes with matchers, higher priority is evaluated first (configurable via fox.WithMatchersPriority, or defaults to the number of matchers)
    • Routes with equal matchers priority may be evaluated in any order

If a match candidate fails to complete the full route, including matchers, Fox returns to the last decision point and tries the next available alternative following the same priority order.

Hostname routing

The router can transition instantly and transparently from path-only mode to hostname-prioritized mode without any additional configuration or action. If any route with a hostname is registered, the router automatically switches to prioritize hostname matching. Conversely, if no hostname-specific routes are registered, the router reverts to path-priority mode.

  • If the router has no routes registered with hostnames, the router will perform a path-based lookup only.
  • If the router includes at least one route with a hostname, the router will prioritize lookup based on the request host and path. If no match is found, the router will then fall back to a path-only lookup.

Hostname matching is case-insensitive, so requests to Example.COM, example.com, and EXAMPLE.COM will all match a route registered for example.com.

Warning about context

The fox.Context instance is freed once the request handler function returns to optimize resource allocation. If you need to retain fox.Context beyond the scope of the handler, use the fox.Context.Clone methods.

func Hello(c *fox.Context) {
    cc := c.Clone()
    go func() {
        time.Sleep(2 * time.Second)
        log.Println(cc.Param("name")) // Safe
    }()
    _ = c.String(http.StatusOK, "Hello %s\n", c.Param("name"))
}

Concurrency

Fox implements an immutable radix tree with copy-on-write semantics, which support lock-free reads while allowing a single concurrent writer. Mutations follow a three-phase pattern: first, descend recursively through the tree to locate the insertion point; then as the call stack unwinds, copy each node along the modified path back to the root and finally, update the root in a single atomic operation. The result is a shallow copy of the tree, where unmodified branches are shared between the old and new tree. Multiple mutations can be applied in a single transaction, where each cloned node is cached to avoid copying it more than once.

Other key points
  • Routing requests is lock-free (reading thread never block, even while writes are ongoing)
  • The router always sees a consistent version of the tree while routing request
  • Reading threads do not block writing threads (adding, updating or removing a handler can be done concurrently)
  • Writing threads block each other but never block reading threads

As such threads that route requests should never encounter latency due to ongoing writes or other concurrent readers.

Managing routes at runtime
Routing mutation

In this example, the /upstreams endpoints allow to dynamically add, update and delete proxy upstreams, reusing the proxy helper from the basic example. Thanks to Fox's design, those actions are perfectly safe and may be executed concurrently.

package main

import (
	"encoding/json"
	"log"
	"net/http"

	"github.com/fox-toolkit/fox"
)

type Upstream struct {
	Pattern string `json:"pattern"`
	Backend string `json:"backend"`
}

func UpsertUpstream(c *fox.Context) {
	var up Upstream
	if err := json.NewDecoder(c.Request().Body).Decode(&up); err != nil {
		http.Error(c.Writer(), err.Error(), http.StatusBadRequest)
		return
	}

	if err := c.Router().Updates(func(txn *fox.Txn) error {
		if txn.Has(fox.MethodAny, up.Pattern) {
			_, err := txn.Update(fox.MethodAny, up.Pattern, proxy(up.Backend))
			return err
		}
		_, err := txn.Add(fox.MethodAny, up.Pattern, proxy(up.Backend))
		return err
	}); err != nil {
		http.Error(c.Writer(), err.Error(), http.StatusUnprocessableEntity)
		return
	}

	_ = c.String(http.StatusOK, "upstream saved\n")
}

func DeleteUpstream(c *fox.Context) {
	var up Upstream
	if err := json.NewDecoder(c.Request().Body).Decode(&up); err != nil {
		http.Error(c.Writer(), err.Error(), http.StatusBadRequest)
		return
	}

	if _, err := c.Router().Delete(fox.MethodAny, up.Pattern); err != nil {
		http.Error(c.Writer(), err.Error(), http.StatusUnprocessableEntity)
		return
	}

	_ = c.String(http.StatusOK, "upstream deleted\n")
}

func main() {
	f := fox.MustRouter()

	f.MustAdd(fox.MethodPut, "/upstreams", UpsertUpstream)
	f.MustAdd(fox.MethodDelete, "/upstreams", DeleteUpstream)

	log.Fatalln(http.ListenAndServe(":8080", f))
}
ACID Transaction

Fox supports read-write and read-only transactions (with Atomicity, Consistency, and Isolation; Durability is not supported as transactions are in memory). Thread that route requests always see a consistent version of the routing tree and are fully isolated from an ongoing transaction until committed. Read-only transactions capture a point-in-time snapshot of the tree, ensuring they do not observe any ongoing or committed changes made after their creation.

Managed read-write transaction
// Updates executes a function within the context of a read-write managed transaction. If no error is returned
// from the function then the transaction is committed. If an error is returned then the entire transaction is
// aborted.
if err := f.Updates(func(txn *fox.Txn) error {
	if _, err := txn.Update(fox.MethodAny, "api.example.com/checkout/+{path}", proxy("http://checkout-v2.internal")); err != nil {
		return err
	}

	// Iter returns a collection of range iterators for traversing registered routes.
	it := txn.Iter()
	// When Iter() is called on a write transaction, it creates a point-in-time snapshot of the transaction state.
	// It means that writing on the current transaction while iterating is allowed, but the mutation will not be
	// observed in the result returned by PatternPrefix (or any other iterator).
	for route := range it.PatternPrefix("beta.example.com/checkout") {
		if _, err := txn.Delete(slices.Collect(route.Methods()), route.Pattern()); err != nil {
			return err
		}
	}
	return nil
}); err != nil {
	log.Printf("transaction aborted: %s", err)
}
Managed read-only transaction

In this example, a readiness endpoint verifies that critical upstreams are registered. All checks observe the same point-in-time snapshot of the routing tree.

f.MustAdd(fox.MethodGet, "/ready", func(c *fox.Context) {
	if err := c.Router().View(func(txn *fox.Txn) error {
		for _, pattern := range []string{
			"api.example.com/orders/{id:[0-9]+}",
			"api.example.com/checkout/+{path}",
		} {
			if !txn.Has(fox.MethodAny, pattern) {
				return fmt.Errorf("upstream %s is not registered", pattern)
			}
		}
		return nil
	}); err != nil {
		http.Error(c.Writer(), err.Error(), http.StatusServiceUnavailable)
		return
	}
	_ = c.String(http.StatusOK, "ready")
})
Unmanaged read-write transaction
// Txn create an unmanaged read-write or read-only transaction.
txn := f.Txn(true)
defer txn.Abort()

if _, err := txn.Update(fox.MethodAny, "api.example.com/checkout/+{path}", proxy("http://checkout-v2.internal")); err != nil {
	log.Printf("error updating route: %s", err)
	return
}

// Iter returns a collection of range iterators for traversing registered routes.
it := txn.Iter()
// When Iter() is called on a write transaction, it creates a point-in-time snapshot of the transaction state.
// It means that writing on the current transaction while iterating is allowed, but the mutation will not be
// observed in the result returned by PatternPrefix (or any other iterator).
for route := range it.PatternPrefix("beta.example.com/checkout") {
	if _, err := txn.Delete(slices.Collect(route.Methods()), route.Pattern()); err != nil {
		log.Printf("error deleting route: %s", err)
		return
	}
}
// Finalize the transaction
txn.Commit()

Middleware

Middlewares can be registered globally using the fox.WithMiddleware option. The example below demonstrates how to create and apply automatically a simple logging middleware to all routes (including 404, 405, etc...).

package main

import (
	"log"
	"net/http"
	"time"

	"github.com/fox-toolkit/fox"
)

func Logger(next fox.HandlerFunc) fox.HandlerFunc {
	return func(c *fox.Context) {
		start := time.Now()
		next(c)
		log.Printf("route: %s, latency: %s, status: %d, size: %d",
			c.Pattern(),
			time.Since(start),
			c.Writer().Status(),
			c.Writer().Size(),
		)
	}
}

func main() {
	f := fox.MustRouter(fox.WithMiddleware(Logger))

	f.MustAdd(fox.MethodGet, "/", func(c *fox.Context) {
		_ = c.String(http.StatusOK, "Hello World")
	})

	log.Fatalln(http.ListenAndServe(":8080", f))
}

Additionally, fox.WithMiddlewareFor option provide a more fine-grained control over where a middleware is applied, such as only for 404 or 405 handlers. Possible scopes include fox.RouteHandler (regular routes), fox.NoRouteHandler, fox.NoMethodHandler, fox.RedirectSlashHandler, fox.RedirectPathHandler, fox.RejectPathHandler, fox.OptionsHandler and any combination of these.

f  := fox.MustRouter(
	fox.WithMiddlewareFor(fox.RouteHandler, Logger),
	fox.WithMiddlewareFor(fox.NoRouteHandler|fox.NoMethodHandler, SpecialLogger),
)

Finally, it's also possible to attaches middleware on a per-route basis. Note that route-specific middleware must be explicitly reapplied when updating a route. If not, any middleware will be removed, and the route will fall back to using only global middleware (if any).

f := fox.MustRouter(
	fox.WithMiddleware(fox.Logger(slog.NewTextHandler(os.Stdout, nil))),
)
f.MustAdd(fox.MethodGet, "/", SomeHandler, fox.WithMiddleware(foxtimeout.Middleware(2*time.Second)))
f.MustAdd(fox.MethodGet, "/foo", SomeOtherHandler)
Official middlewares

Working with http.Handler

Fox itself implements the http.Handler interface which make easy to chain any compatible middleware before the router. Moreover, the router provides convenient fox.WrapF, fox.WrapH and fox.WrapM adapter to be use with http.Handler.

The route parameters can be accessed by the wrapped handler through the request context.Context when the adapters are used.

Wrapping an http.Handler

articles := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
	params := fox.ParamsFromContext(r.Context())
	// Article id: 80
	// Matched route: /articles/{id}
	_, _ = fmt.Fprintf(w, "Article id: %s\nMatched route: %s\n", params.Get("id"), r.Pattern)
})

f := fox.MustRouter()
f.MustAdd(fox.MethodGet, "/articles/{id}", fox.WrapH(articles))

Wrapping any standard http.Handler middleware

corsMw, _ := cors.NewMiddleware(cors.Config{
	Origins:        []string{"https://example.com"},
	Methods:        []string{http.MethodGet, http.MethodPost, http.MethodPut},
	RequestHeaders: []string{"Authorization"},
})

f := fox.MustRouter(
	fox.WithMiddlewareFor(fox.RouteHandler|fox.OptionsHandler, fox.WrapM(corsMw.Wrap)),
)

Handling OPTIONS Requests and CORS Automatically

The WithAutoOptions setting or the WithOptionsHandler registration enable automatic responses to OPTIONS requests. This feature is particularly useful for handling Cross-Origin Resource Sharing (CORS) preflight requests.

When automatic OPTIONS responses is enabled, Fox distinguishes between regular OPTIONS requests and CORS preflight requests:

  • Regular OPTIONS requests: The router responds with the Allow header populated with all HTTP methods registered for the matched resource. If no route matches, the NoRoute handler is called.
  • CORS preflight requests: The router responds to every preflight request by calling the OPTIONS handler, regardless of whether the resource exists.

To customize how OPTIONS requests are handled (e.g. adding CORS headers), you may register a middleware for the fox.OptionsHandler scope or provide a custom handler via WithOptionsHandler. Note that registered routes with the OPTIONS method always take precedence over automatic replies.

package main

import (
	"errors"
	"log"
	"net/http"

	"github.com/jub0bs/cors"
	"github.com/fox-toolkit/fox"
)

func main() {
	corsMw, err := cors.NewMiddleware(cors.Config{
		Origins:        []string{"https://example.com"},
		Methods:        []string{http.MethodGet, http.MethodPost},
		RequestHeaders: []string{"Authorization"},
	})
	if err != nil {
		log.Fatal(err)
	}
	corsMw.SetDebug(true) // turn debug mode on (optional)

	f := fox.MustRouter(
		fox.WithAutoOptions(true), // let Fox automatically handle OPTIONS requests
		fox.WithMiddlewareFor(fox.RouteHandler|fox.OptionsHandler, fox.WrapM(corsMw.Wrap)),
	)

	f.MustAdd(fox.MethodGet, "/api/users", ListUsers)
	f.MustAdd(fox.MethodPost, "/api/users", CreateUsers)

	if err := http.ListenAndServe(":8080", f); !errors.Is(err, http.ErrServerClosed) {
		log.Fatal(err)
	}
}

The CORS protocol is complex and security-sensitive. We do NOT recommend implementing CORS handling manually. Instead, consider using jub0bs/cors, which performs extensive validation before allowing middleware creation, helping you avoid common pitfalls.

Resolving Client IP

The WithClientIPResolver option allows you to set up strategies to resolve the client IP address based on your use case and network topology. Accurately determining the client IP is hard, particularly in environments with proxies or load balancers. For example, the leftmost IP in the X-Forwarded-For header is commonly used and is often regarded as the "closest to the client" and "most real," but it can be easily spoofed. Therefore, you should absolutely avoid using it for any security-related purposes, such as request throttling.

The resolver used must be chosen and tuned for your network configuration. This should result in a resolver never returning an error and if it does, it should be treated as an application issue or a misconfiguration, rather than defaulting to an untrustworthy IP.

The sub-package github.com/fox-toolkit/fox/clientip provides a set of best practices resolvers that should cover most use cases.

package main

import (
	"fmt"

	"github.com/fox-toolkit/fox"
	"github.com/fox-toolkit/fox/clientip"
)

func main() {
	resolver, err := clientip.NewRightmostNonPrivate(clientip.XForwardedForKey)
	if err != nil {
		panic(err)
	}
	f := fox.MustRouter(
		fox.WithClientIPResolver(
			resolver,
		),
	)

	f.MustAdd(fox.MethodGet, "/foo/bar", func(c *fox.Context) {
		ipAddr, err := c.ClientIP()
		if err != nil {
			// If the current resolver is not able to derive the client IP, an error
			// will be returned rather than falling back on an untrustworthy IP. It
			// should be treated as an application issue or a misconfiguration.
			panic(err)
		}
		fmt.Println(ipAddr.String())
	})
}

It is also possible to create a chain with multiple resolvers that attempt to derive the client IP, stopping when the first one succeeds.

resolver, _ := clientip.NewLeftmostNonPrivate(clientip.ForwardedKey, 10)
f := fox.MustRouter(
	fox.WithClientIPResolver(
		// A common use for this is if a server is both directly connected to the
		// internet and expecting a header to check.
		clientip.NewChain(
			resolver,
			clientip.NewRemoteAddr(),
		),
	),
)

Note that there is no "sane" default strategy, so calling Context.ClientIP without a resolver configured will return an ErrNoClientIPResolver.

See this blog post for general guidance on choosing a strategy that fit your needs.

Benchmark

The primary goal of Fox is to be a lightweight, high performance router which allow routes modification at runtime. The following benchmarks attempt to compare Fox to various popular alternatives, including both fully-featured web frameworks and lightweight request routers. These benchmarks are based on the julienschmidt/go-http-routing-benchmark repository.

Please note that these benchmarks should not be taken too seriously, as the comparison may not be entirely fair due to the differences in feature sets offered by each framework. Performance should be evaluated in the context of your specific use case and requirements. While Fox aims to excel in performance, it's important to consider the trade-offs and functionality provided by different web frameworks and routers when making your selection.

Config
GOOS:   Darwin
GOARCH: arm64
GO:     1.26
CPU:    Apple M4 Max
Static Routes

It is just a collection of random static paths inspired by the structure of the Go directory. It might not be a realistic URL-structure.

GOMAXPROCS: 1

BenchmarkHttpRouter_StaticAll  	  606360	      3863 ns/op	       0 B/op	       0 allocs/op
BenchmarkHttpTreeMux_StaticAll 	  486656	      5006 ns/op	       0 B/op	       0 allocs/op
BenchmarkGin_StaticAll         	  417074	      5522 ns/op	       0 B/op	       0 allocs/op
BenchmarkFox_StaticAll         	  299235	      7343 ns/op	       0 B/op	       0 allocs/op
BenchmarkEcho_StaticAll        	  296577	      7812 ns/op	       0 B/op	       0 allocs/op
BenchmarkStdMux_StaticAll      	  183643	     12990 ns/op	       0 B/op	       0 allocs/op
BenchmarkChi_StaticAll         	   61158	     39326 ns/op	   57776 B/op	     314 allocs/op
BenchmarkBeego_StaticAll       	   43267	     57609 ns/op	   55264 B/op	     471 allocs/op
BenchmarkGorillaMux_StaticAll  	    8713	    287557 ns/op	  133137 B/op	    1099 allocs/op
BenchmarkPat_StaticAll         	    5646	    412932 ns/op	  602832 B/op	   12559 allocs/op
BenchmarkMartini_StaticAll     	    4366	    558532 ns/op	  129210 B/op	    2031 allocs/op
BenchmarkTraffic_StaticAll     	    3822	    632259 ns/op	  749842 B/op	   14444 allocs/op

In this benchmark, Fox performs as well as Gin and Echo which are both Radix Tree based routers. An interesting fact is that HttpTreeMux also support adding route while serving request concurrently. However, it takes a slightly different approach, by using an optional RWMutex that may not scale as well as Fox under heavy load. The next test compare HttpTreeMux with and without the *SafeAddRouteFlag (concurrent reads and writes) and Fox in parallel benchmark.

GOMAXPROCS: 16

BenchmarkFox_StaticAllParallel-16            	 2833873	     847.5 ns/op	       0 B/op	       0 allocs/op
BenchmarkHttpTreeMux_StaticAllParallel-16    	  100148	     23987 ns/op	       0 B/op	       0 allocs/op
Micro Benchmarks

The following benchmarks measure the cost of some very basic operations.

In the first benchmark, only a single route, containing a parameter, is loaded into the routers. Then a request for a URL matching this pattern is made and the router has to call the respective registered handler function. End.

GOMAXPROCS: 1

BenchmarkEcho_Param        	100000000	       22.79 ns/op	       0 B/op	       0 allocs/op
BenchmarkGin_Param         	100000000	       22.92 ns/op	       0 B/op	       0 allocs/op
BenchmarkFox_Param         	90499582	       25.95 ns/op	       0 B/op	       0 allocs/op
BenchmarkHttpRouter_Param  	79082857	       30.37 ns/op	      32 B/op	       1 allocs/op
BenchmarkHttpTreeMux_Param 	15741889	       150.6 ns/op	     352 B/op	       3 allocs/op
BenchmarkPat_Param         	 7845747	       306.8 ns/op	     472 B/op	       8 allocs/op
BenchmarkBeego_Param       	 7712055	       318.9 ns/op	     352 B/op	       3 allocs/op
BenchmarkChi_Param         	 7354921	       325.3 ns/op	     704 B/op	       4 allocs/op
BenchmarkGorillaMux_Param  	 4130534	       576.6 ns/op	    1152 B/op	       8 allocs/op
BenchmarkTraffic_Param     	 2500653	       966.0 ns/op	    1808 B/op	      19 allocs/op
BenchmarkMartini_Param     	 1983092	        1212 ns/op	    1096 B/op	      12 allocs/op

Same as before, but now with multiple parameters, all in the same single route. The intention is to see how the routers scale with the number of parameters.

GOMAXPROCS: 1

BenchmarkGin_Param5         	59596666	       40.36 ns/op	       0 B/op	       0 allocs/op
BenchmarkEcho_Param5        	52619029	       45.21 ns/op	       0 B/op	       0 allocs/op
BenchmarkFox_Param5         	44117173	       54.13 ns/op	       0 B/op	       0 allocs/op
BenchmarkHttpRouter_Param5  	29107461	       79.10 ns/op	     160 B/op	       1 allocs/op
BenchmarkHttpTreeMux_Param5 	 7148354	       338.0 ns/op	     576 B/op	       6 allocs/op
BenchmarkBeego_Param5       	 5843640	       408.6 ns/op	     352 B/op	       3 allocs/op
BenchmarkChi_Param5         	 5100630	       471.6 ns/op	     704 B/op	       4 allocs/op
BenchmarkPat_Param5         	 3465811	       694.2 ns/op	     776 B/op	      23 allocs/op
BenchmarkGorillaMux_Param5  	 2589742	       928.7 ns/op	    1216 B/op	       8 allocs/op
BenchmarkTraffic_Param5     	 1688236	        1411 ns/op	    2176 B/op	      26 allocs/op
BenchmarkMartini_Param5     	 1703668	        1416 ns/op	    1256 B/op	      13 allocs/op

BenchmarkGin_Param20         	22343126	       113.0 ns/op	       0 B/op	       0 allocs/op
BenchmarkEcho_Param20        	20022211	       119.2 ns/op	       0 B/op	       0 allocs/op
BenchmarkFox_Param20         	12613239	       192.6 ns/op	       0 B/op	       0 allocs/op
BenchmarkHttpRouter_Param20  	 8977975	       264.9 ns/op	     704 B/op	       1 allocs/op
BenchmarkBeego_Param20       	 2420388	       995.5 ns/op	     352 B/op	       3 allocs/op
BenchmarkHttpTreeMux_Param20 	 1371654	        1784 ns/op	    3144 B/op	      13 allocs/op
BenchmarkChi_Param20         	 1383187	        1791 ns/op	    2504 B/op	       9 allocs/op
BenchmarkGorillaMux_Param20  	 1000000	        2076 ns/op	    3272 B/op	      13 allocs/op
BenchmarkMartini_Param20     	  870381	        2747 ns/op	    3568 B/op	      18 allocs/op
BenchmarkPat_Param20         	  670797	        3483 ns/op	    3992 B/op	      75 allocs/op
BenchmarkTraffic_Param20     	  464222	        5124 ns/op	    7664 B/op	      52 allocs/op

Now let's see how expensive it is to access a parameter. The handler function reads the value (by the name of the parameter, e.g. with a map lookup; depends on the router) and writes it to /dev/null

GOMAXPROCS: 1

BenchmarkGin_ParamWrite         	78344110	        28.05 ns/op	       0 B/op	       0 allocs/op
BenchmarkHttpRouter_ParamWrite  	73665955	        32.96 ns/op	      32 B/op	       1 allocs/op
BenchmarkFox_ParamWrite         	72707535	        33.14 ns/op	       0 B/op	       0 allocs/op
BenchmarkEcho_ParamWrite        	48205112	        50.34 ns/op	       8 B/op	       1 allocs/op
BenchmarkHttpTreeMux_ParamWrite 	15610106	        154.7 ns/op	     352 B/op	       3 allocs/op
BenchmarkChi_ParamWrite         	 7585606	        319.6 ns/op	     704 B/op	       4 allocs/op
BenchmarkBeego_ParamWrite       	 7509182	        327.0 ns/op	     360 B/op	       4 allocs/op
BenchmarkPat_ParamWrite         	 4885155	        492.1 ns/op	     896 B/op	      12 allocs/op
BenchmarkGorillaMux_ParamWrite  	 4155492	        579.3 ns/op	    1152 B/op	       8 allocs/op
BenchmarkTraffic_ParamWrite     	 2133512	         1118 ns/op	    2232 B/op	      23 allocs/op
BenchmarkMartini_ParamWrite     	 1796299	         1330 ns/op	    1144 B/op	      15 allocs/op
Github

Finally, this benchmark executes a request for each GitHub API route (203 routes).

GOMAXPROCS: 1

BenchmarkGin_GithubAll         	  269502	      8684 ns/op	       0 B/op	       0 allocs/op
BenchmarkEcho_GithubAll        	  201560	     11686 ns/op	       0 B/op	       0 allocs/op
BenchmarkFox_GithubAll         	  183614	     13102 ns/op	       0 B/op	       0 allocs/op
BenchmarkHttpRouter_GithubAll  	  177540	     13431 ns/op	   14240 B/op	     171 allocs/op
BenchmarkHttpTreeMux_GithubAll 	   57018	     42361 ns/op	   67648 B/op	     691 allocs/op
BenchmarkBeego_GithubAll       	   30009	     80380 ns/op	   73121 B/op	     629 allocs/op
BenchmarkChi_GithubAll         	   28396	     85895 ns/op	  130817 B/op	     740 allocs/op
BenchmarkTraffic_GithubAll     	    2211	   1086056 ns/op	  837296 B/op	   14315 allocs/op
BenchmarkPat_GithubAll         	    2144	   1112931 ns/op	 1834945 B/op	   28773 allocs/op
BenchmarkGorillaMux_GithubAll  	    1870	   1288365 ns/op	  230339 B/op	    1620 allocs/op
BenchmarkMartini_GithubAll     	    1711	   1401655 ns/op	  236944 B/op	    2805 allocs/op

Road to v1

Contributions

This project aims to provide a lightweight, high-performance router that is easy to use and hard to misuse, designed for building API gateways and reverse proxies. Features are chosen carefully with an emphasis on composability, and each addition is evaluated against this core mission. The router exposes a relatively low-level API, allowing it to serve as a building block for implementing your own "batteries included" frameworks. Feature requests along these lines are welcome.

License

Fox is licensed under the Apache License 2.0. See LICENSE.txt for details.

The Fox logo is licensed separately under CC BY-NC-ND 4.0. See LICENSE-fox-logo.txt for details.

Acknowledgements

  • hashicorp/go-immutable-radix: Fox Tree design is inspired by Hashicorp's Immutable Radix Tree.
  • realclientip/realclientip-go: Fox uses a derivative version of Adam Pritchard's realclientip-go library. See his insightful blog post on the topic for more details.
  • The router API is influenced by popular routers such as Gin and Echo.
  • Thanks to pawndev for development help and the many thoughtful PR reviews.

Documentation

Index

Examples

Constants

View Source
const (
	MIMEApplicationJSON                  = "application/json"
	MIMEApplicationJSONCharsetUTF8       = MIMEApplicationJSON + "; " + charsetUTF8
	MIMEApplicationJavaScript            = "application/javascript"
	MIMEApplicationJavaScriptCharsetUTF8 = MIMEApplicationJavaScript + "; " + charsetUTF8
	MIMEApplicationXML                   = "application/xml"
	MIMEApplicationXMLCharsetUTF8        = MIMEApplicationXML + "; " + charsetUTF8
	MIMETextXML                          = "text/xml"
	MIMETextXMLCharsetUTF8               = MIMETextXML + "; " + charsetUTF8
	MIMEApplicationForm                  = "application/x-www-form-urlencoded"
	MIMEApplicationProtobuf              = "application/protobuf"
	MIMEApplicationMsgpack               = "application/msgpack"
	MIMETextHTML                         = "text/html"
	MIMETextHTMLCharsetUTF8              = MIMETextHTML + "; " + charsetUTF8
	MIMETextPlain                        = "text/plain"
	MIMETextPlainCharsetUTF8             = MIMETextPlain + "; " + charsetUTF8
	MIMEMultipartForm                    = "multipart/form-data"
	MIMEOctetStream                      = "application/octet-stream"
)

MIME types

View Source
const (
	HeaderAccept              = "Accept"
	HeaderAcceptEncoding      = "Accept-Encoding"
	HeaderAllow               = "Allow"
	HeaderAuthorization       = "Authorization"
	HeaderProxyAuthorization  = "Proxy-Authorization"
	HeaderContentDisposition  = "Content-Disposition"
	HeaderContentEncoding     = "Content-Encoding"
	HeaderContentLength       = "Content-Length"
	HeaderContentType         = "Content-Type"
	HeaderCookie              = "Cookie"
	HeaderSetCookie           = "Set-Cookie"
	HeaderIfModifiedSince     = "If-Modified-Since"
	HeaderLastModified        = "Last-Modified"
	HeaderLocation            = "Location"
	HeaderRetryAfter          = "Retry-After"
	HeaderUpgrade             = "Upgrade"
	HeaderVary                = "Vary"
	HeaderWWWAuthenticate     = "WWW-Authenticate"
	HeaderXForwardedFor       = "X-Forwarded-For"
	HeaderForwarded           = "Forwarded"
	HeaderXForwardedProto     = "X-Forwarded-Proto"
	HeaderXForwardedProtocol  = "X-Forwarded-Protocol"
	HeaderXForwardedSSL       = "X-Forwarded-Ssl"
	HeaderXURLScheme          = "X-Url-Scheme"
	HeaderXHTTPMethodOverride = "X-HTTP-Method-Override"
	HeaderXRequestID          = "X-Request-Id"
	HeaderXCorrelationID      = "X-Correlation-Id"
	HeaderXRequestedWith      = "X-Requested-With"
	HeaderServer              = "Server"
	HeaderOrigin              = "Origin"
	HeaderHost                = "Host"
	HeaderCacheControl        = "Cache-Control"
	HeaderConnection          = "Connection"
	HeaderETag                = "ETag"

	// Access control
	HeaderAccessControlRequestMethod    = "Access-Control-Request-Method"
	HeaderAccessControlRequestHeaders   = "Access-Control-Request-Headers"
	HeaderAccessControlAllowOrigin      = "Access-Control-Allow-Origin"
	HeaderAccessControlAllowMethods     = "Access-Control-Allow-Methods"
	HeaderAccessControlAllowHeaders     = "Access-Control-Allow-Headers"
	HeaderAccessControlAllowCredentials = "Access-Control-Allow-Credentials"
	HeaderAccessControlExposeHeaders    = "Access-Control-Expose-Headers"
	HeaderAccessControlMaxAge           = "Access-Control-Max-Age"

	// Security
	HeaderStrictTransportSecurity         = "Strict-Transport-Security"
	HeaderXContentTypeOptions             = "X-Content-Type-Options"
	HeaderXXSSProtection                  = "X-XSS-Protection"
	HeaderXFrameOptions                   = "X-Frame-Options"
	HeaderContentSecurityPolicy           = "Content-Security-Policy"
	HeaderContentSecurityPolicyReportOnly = "Content-Security-Policy-Report-Only"
	// nolint:gosec
	HeaderXCSRFToken     = "X-CSRF-Token"
	HeaderReferrerPolicy = "Referrer-Policy"

	// Platform Header for single IP
	HeaderCFConnectionIP       = "CF-Connecting-IP"
	HeaderTrueClientIP         = "True-Client-IP"
	HeaderFastClientIP         = "Fastly-Client-IP"
	HeaderXAzureClientIP       = "X-Azure-ClientIP"
	HeaderXAzureSocketIP       = "X-Azure-SocketIP"
	HeaderXAppengineRemoteAddr = "X-Appengine-Remote-Addr"
	HeaderFlyClientIP          = "Fly-Client-IP"
	HeaderXRealIP              = "X-Real-Ip"
)

Headers

View Source
const (
	// LoggerStatusKey is the key used by the built-in logger middleware for the HTTP response status code
	// when the log method is called. The associated [slog.Value] is an int.
	LoggerStatusKey = "status"
	// LoggerMethodKey is the key used by the built-in logger middleware for the HTTP request method.
	// The associated [slog.Value] is a string.
	LoggerMethodKey = "method"
	// LoggerHostKey is the key used by the built-in logger middleware for the request host.
	// The associated [slog.Value] is a string.
	LoggerHostKey = "host"
	// LoggerPathKey is the key used by the built-in logger middleware for the canonical
	// request path the router routed on (see [Context.RoutingPath]).
	// The associated [slog.Value] is a string.
	LoggerPathKey = "path"
	// LoggerLatencyKey is the key used by the built-in logger middleware for the request processing duration.
	// The associated [slog.Value] is a time.Duration.
	LoggerLatencyKey = "latency"
	// LoggerSizeKey is the key used by the built-in logger middleware for the response body size.
	// The associated [slog.Value] is an int.
	LoggerSizeKey = "size"
	// LoggerLocationKey is the key used by the built-in logger middleware for redirect location header.
	// The associated [slog.Value] is a string.
	LoggerLocationKey = "location"
)

Keys for "built-in" logger attribute for the logger middleware.

View Source
const (
	// LoggerRouteKey is the key used by the built-in recovery middleware for the matched route
	// when the log method is called. The associated [slog.Value] is a string.
	LoggerRouteKey = "route"
	// LoggerParamsKey is the key used by the built-in recovery middleware for route parameters
	// when the log method is called. The associated [slog.Value] is a [slog.GroupValue] containing parameter
	// key-value pairs.
	LoggerParamsKey = "params"
	// LoggerPanicKey is the key used by the built-in recovery middleware for the panic value
	// when the log method is called. The associated [slog.Value] is any.
	LoggerPanicKey = "panic"
)

Keys for "built-in" logger attributes used by the recovery middleware.

Variables

View Source
var (
	ErrRouteNotFound           = errors.New("route not found")
	ErrInvalidRoute            = errors.New("invalid route")
	ErrDiscardedResponseWriter = errors.New("discarded response writer")
	ErrNoClientIPResolver      = errors.New("no client ip resolver")
	ErrReadOnlyTxn             = errors.New("write on read-only transaction")
	ErrSettledTxn              = errors.New("transaction settled")
	ErrTooManyMatchers         = errors.New("too many matchers")
	ErrInvalidConfig           = errors.New("invalid config")
	ErrInvalidMatcher          = errors.New("invalid matcher")
)
View Source
var (
	MethodGet     = []string{http.MethodGet}
	MethodHead    = []string{http.MethodHead}
	MethodPost    = []string{http.MethodPost}
	MethodPut     = []string{http.MethodPut}
	MethodPatch   = []string{http.MethodPatch}
	MethodDelete  = []string{http.MethodDelete}
	MethodConnect = []string{http.MethodConnect}
	MethodOptions = []string{http.MethodOptions}
	MethodTrace   = []string{http.MethodTrace}
	MethodQuery   = []string{"QUERY"} // RFC 10008
)

Common HTTP methods.

View Source
var MethodAny []string

Functions

func CollapseDotSegments added in v0.31.0

func CollapseDotSegments(path string) (_ string, ok bool)

CollapseDotSegments removes "." and ".." path segments as defined by RFC 3986 section 5.2.4. Unlike path.Clean, consecutive slashes are preserved and delimit empty segments that a ".." may consume. It returns ok=false when a ".." segment would escape above the root.

func DefaultHandleRecovery

func DefaultHandleRecovery(c *Context, _ any)

DefaultHandleRecovery is a default implementation of the RecoveryFunc. It responds with a status code 500 and writes a generic error message.

func DefaultNoMethodHandler added in v0.31.0

func DefaultNoMethodHandler(c *Context)

DefaultNoMethodHandler is a simple HandlerFunc that replies to each request with a “405 Method Not Allowed” reply.

func DefaultNoRouteHandler added in v0.31.0

func DefaultNoRouteHandler(c *Context)

DefaultNoRouteHandler is a simple HandlerFunc that replies to each request with a “404 page not found” reply.

func DefaultOptionsHandler

func DefaultOptionsHandler(c *Context)

DefaultOptionsHandler is a simple HandlerFunc that replies to each request with a "200 OK" reply.

func DefaultRejectPathHandler added in v0.31.0

func DefaultRejectPathHandler(c *Context)

DefaultRejectPathHandler is a simple HandlerFunc that replies to each request with a “400 Bad Request” reply.

func ErrNotSupported

func ErrNotSupported() error

ErrNotSupported returns an error that Is http.ErrNotSupported, but is not == to it.

func MergeSlashes added in v0.31.0

func MergeSlashes(path string) string

MergeSlashes merges consecutive slashes in path into a single slash. Only raw slashes are merged, an encoded %2F is left untouched.

func NewTestContext

func NewTestContext(w http.ResponseWriter, r *http.Request, opts ...GlobalOption) (*Router, *Context)

NewTestContext returns a new Router and its associated Context, designed only for testing purpose.

func WithClientIPMatcher

func WithClientIPMatcher(ip string) interface {
	RouteOption
	MatcherOption
}

WithClientIPMatcher attaches a client IP address matcher to a route. The matcher ensures that requests are only routed to the handler if the client IP address matches the specified CIDR notation or IP address. The ip parameter accepts both single IP addresses (e.g., "192.168.1.1") and CIDR ranges (e.g., "192.168.1.0/24"). Multiple matchers can be attached to the same route. All matchers must match for the route to be eligible. See WithClientIPResolver to configure a resolver for obtaining the "real" client IP.

func WithClientIPResolver

func WithClientIPResolver(resolver ClientIPResolver) interface {
	GlobalOption
	RouteOption
}

WithClientIPResolver sets the resolver for obtaining the "real" client IP address from HTTP requests. This resolver is used by the Context.ClientIP method. The resolver must be chosen and tuned for your network configuration to ensure it never returns an error -- i.e., never fails to find a candidate for the "real" IP. Consequently, getting an error result should be treated as an application error, perhaps even worthy of panicking. There is no sane default, so if no resolver is configured, Context.ClientIP returns ErrNoClientIPResolver.

This option can be applied on a per-route basis or globally:

  • If applied globally, it affects all routes by default.
  • If applied to a specific route, it will override the global setting for that route.
  • Setting the resolver to nil is equivalent to no resolver configured.

func WithHeaderMatcher

func WithHeaderMatcher(key, value string) interface {
	RouteOption
	MatcherOption
}

WithHeaderMatcher attaches an HTTP header matcher to a route. The matcher ensures that requests are only routed to the handler if the specified header matches the given value. Multiple matchers can be attached to the same route. All matchers must match for the route to be eligible.

func WithHeaderRegexpMatcher

func WithHeaderRegexpMatcher(key, expr string) interface {
	RouteOption
	MatcherOption
}

WithHeaderRegexpMatcher attaches an HTTP header matcher with regular expression support to a route. The matcher ensures that requests are only routed to the handler if the specified header value matches the given regular expression. The expression is automatically anchored at both ends, requiring a full match of the header value. Multiple matchers can be attached to the same route. All matchers must match for the route to be eligible.

func WithMatcher

func WithMatcher(matchers ...Matcher) interface {
	RouteOption
	MatcherOption
}

WithMatcher attaches a custom matcher to a route. Matchers allow for advanced request routing based on conditions beyond the request host, path and method. Multiple matchers can be attached to the same route. All matchers must match for the route to be eligible.

func WithMiddleware

func WithMiddleware(m ...MiddlewareFunc) interface {
	GlobalOption
	RouteOption
}

WithMiddleware attaches middleware to the router or to a specific route. The middlewares are executed in the order they are added. When applied globally, the middleware affects all handlers, including special handlers such as NotFound, MethodNotAllowed, AutoOption, and the internal redirect handler.

This option can be applied on a per-route basis or globally: - If applied globally, the middleware will be applied to all routes and handlers by default. - If applied to a specific route, the middleware will only apply to that route and will be chained after any global middleware.

Example

This example demonstrates how to register a global middleware that will be applied to all routes.

// Define a custom middleware to measure the time taken for request processing and
// log the URL, route, time elapsed, and status code.
metrics := func(next HandlerFunc) HandlerFunc {
	return func(c *Context) {
		start := time.Now()
		next(c)
		log.Printf(
			"url=%s; route=%s; time=%d; status=%d",
			c.Request().URL,
			c.Pattern(),
			time.Since(start),
			c.Writer().Status(),
		)
	}
}

f, _ := NewRouter(WithMiddleware(metrics))

f.MustAdd([]string{http.MethodGet, http.MethodHead}, "/hello/{name}", func(c *Context) {
	_ = c.String(200, fmt.Sprintf("Hello %s\n", c.Param("name")))
})

func WithQueryMatcher

func WithQueryMatcher(key, value string) interface {
	RouteOption
	MatcherOption
}

WithQueryMatcher attaches a query parameter matcher to a route. The matcher ensures that requests are only routed to the handler if the specified query parameter matches the given value. Multiple matchers can be attached to the same route. All matchers must match for the route to be eligible.

func WithQueryRegexpMatcher

func WithQueryRegexpMatcher(key, expr string) interface {
	RouteOption
	MatcherOption
}

WithQueryRegexpMatcher attaches a query parameter matcher with regular expression support to a route. The matcher ensures that requests are only routed to the handler if the specified query parameter value matches the given regular expression. The expression is automatically anchored at both ends, requiring a full match of the parameter value. Multiple matchers can be attached to the same route. All matchers must match for the route to be eligible.

func WithSchemeMatcher added in v0.29.0

func WithSchemeMatcher(scheme string) interface {
	RouteOption
	MatcherOption
}

WithSchemeMatcher attaches a URL scheme matcher to a route. The matcher ensures that requests are only routed to the handler if the connection scheme matches the given value. Only "http" and "https" are accepted. The scheme is ONLY derived from the TLS state of the connection between the client and the server. Behind a TLS-terminating reverse proxy, this matcher therefore reflects the proxy-to-server hop, not the original client connection; to route on the original client scheme (e.g. via X-Forwarded-Proto), implement a custom Matcher that reads the header within a trust boundary you control. Note that r.URL.Scheme is intentionally ignored: it can be set by an attacker via HTTP/1.1 absolute-form requests. Multiple matchers can be attached to the same route. All matchers must match for the route to be eligible.

func WithTrailingSlash added in v0.31.0

func WithTrailingSlash(opt TrailingSlashOption) interface {
	GlobalOption
	RouteOption
}

WithTrailingSlash configures how the router handles trailing slashes in request paths.

Available slash handling modes:

  • ExactSlash: Routes are matched exactly as registered. /foo/bar and /foo/bar/ are treated as different routes.
  • RelaxedSlash: Routes match regardless of trailing slash. Both /foo/bar and /foo/bar/ match the same route.
  • RedirectSlash: When a route is not found, but exists with/without a trailing slash, issues a redirect to the correct path.

Redirects use the canonical path that the router routed on (see Context.RoutingPath).

This option can be applied on a per-route basis or globally:

  • If applied globally, it affects all routes by default.
  • If applied to a specific route, it will override the global setting for that route.

If both /foo/bar and /foo/bar/ are explicitly registered, the exact match always takes precedence. Otherwise a trailing slash match keeps the route's normal priority, like a direct match.

Types

type ClientIPMatcher added in v0.29.0

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

ClientIPMatcher matches a request when the resolved client IP belongs to the configured CIDR range.

func MatchClientIP

func MatchClientIP(ip string) (ClientIPMatcher, error)

MatchClientIP returns a ClientIPMatcher that matches when the resolved client IP belongs to the given CIDR range. The ip parameter accepts both single IP addresses (e.g., "192.168.1.1") and CIDR notation (e.g., "192.168.1.0/24"). An invalid value returns an error. The client IP is resolved via the configured ClientIPResolver; without one, the matcher always fails. See WithClientIPMatcher for the option counterpart and WithClientIPResolver to configure the resolver.

func (ClientIPMatcher) Equal added in v0.29.0

func (m ClientIPMatcher) Equal(matcher Matcher) bool

Equal reports whether matcher is a ClientIPMatcher with the same network range.

func (ClientIPMatcher) Match added in v0.29.0

func (m ClientIPMatcher) Match(c RequestContext) bool

Match reports whether the resolved client IP belongs to the configured CIDR range. It returns false if the resolver returns an error or no resolver is configured. The resolved address is unmapped and stripped of any zone before the range check.

func (ClientIPMatcher) Prefix added in v0.31.0

func (m ClientIPMatcher) Prefix() netip.Prefix

Prefix returns the network range tested by this matcher.

func (ClientIPMatcher) String added in v0.29.0

func (m ClientIPMatcher) String() string

String returns a textual representation of the matcher in the form "ip:CIDR".

type ClientIPResolver

type ClientIPResolver interface {
	// ClientIP returns the "real" client IP according to the implemented resolver. It returns an error if no valid IP
	// address can be derived. This is typically considered a misconfiguration error, unless the resolver involves
	// obtaining an untrustworthy or optional value.
	ClientIP(c RequestContext) (netip.Addr, error)
}

ClientIPResolver define a resolver for obtaining the "real" client IP from HTTP requests. The resolver used must be chosen and tuned for your network configuration. This should result in a resolver never returning an error i.e., never failing to find a candidate for the "real" IP. Consequently, getting an error result should be treated as an application error, perhaps even worthy of panicking. Builtin best practices resolver can be found in the github.com/fox-toolkit/fox/clientip package.

type ClientIPResolverFunc

type ClientIPResolverFunc func(c RequestContext) (netip.Addr, error)

The ClientIPResolverFunc type is an adapter to allow the use of ordinary functions as ClientIPResolver. If f is a function with the appropriate signature, ClientIPResolverFunc(f) is a ClientIPResolverFunc that calls f.

func (ClientIPResolverFunc) ClientIP

ClientIP calls f(c).

type Context

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

Context represents the context of the current HTTP request. It provides methods to access request data and to write a response. Be aware that the Context API is not thread-safe and its lifetime should be limited to the duration of the HandlerFunc execution, as the Context may be reused as soon as the handler returns.

func NewTestContextOnly

func NewTestContextOnly(w http.ResponseWriter, r *http.Request, opts ...GlobalOption) *Context

NewTestContextOnly returns a new Context designed only for testing purpose.

func (*Context) AddHeader

func (c *Context) AddHeader(key, value string)

AddHeader add the response header for the given key to the specified value.

func (*Context) Blob

func (c *Context) Blob(code int, contentType string, buf []byte) (err error)

Blob sends a byte slice with the specified status code and content type.

func (*Context) ClientIP

func (c *Context) ClientIP() (netip.Addr, error)

ClientIP returns the "real" client IP address based on the configured ClientIPResolver. The resolver is set using the WithClientIPResolver option. If no resolver is configured, the method returns error ErrNoClientIPResolver.

The resolver used must be chosen and tuned for your network configuration. This should result in a resolver never returning an error -- i.e., never failing to find a candidate for the "real" IP. Consequently, getting an error result should be treated as an application error, perhaps even worthy of panicking.

func (*Context) Clone

func (c *Context) Clone() *Context

Clone returns a deep copy of the Context that is safe to use after the HandlerFunc returns. Any attempt to write on the ResponseWriter will panic with the error ErrDiscardedResponseWriter.

func (*Context) CloneWith

func (c *Context) CloneWith(w ResponseWriter, r *http.Request) *Context

CloneWith returns a shallow copy of the current Context, substituting its ResponseWriter and http.Request with the provided ones. The method is designed for zero allocation during the copy process. The caller is responsible for closing the returned Context by calling Context.Close when it is no longer needed. This functionality is particularly beneficial for middlewares that need to wrap their custom ResponseWriter while preserving the state of the original Context.

func (*Context) Close

func (c *Context) Close()

Close releases the context to be reused later. This method must be called for contexts obtained via Context.CloneWith, Router.Lookup, or Txn.Lookup. Contexts passed to a HandlerFunc are managed automatically by the router and should not be closed manually. See also Context for more details.

func (*Context) Header

func (c *Context) Header(key string) string

Header retrieves the value of the request header for the given key.

func (*Context) Host

func (c *Context) Host() string

Host returns the request host.

func (*Context) Method

func (c *Context) Method() string

Method returns the request method.

func (*Context) Param

func (c *Context) Param(name string) string

Param retrieve a matching wildcard segment by name.

func (*Context) Params

func (c *Context) Params() iter.Seq[Param]

Params returns an iterator over the matched wildcard parameters for the current route.

func (*Context) Pattern

func (c *Context) Pattern() string

Pattern returns the registered route pattern or an empty string if the handler is called in a scope other than RouteHandler.

func (*Context) QueryParam

func (c *Context) QueryParam(name string) string

QueryParam returns the first query value associated with the given key. The query parameters are parsed and cached on first access.

func (*Context) QueryParams

func (c *Context) QueryParams() url.Values

QueryParams parses the http.Request raw query and returns the corresponding values. The result is cached after the first call.

func (*Context) RemoteIP

func (c *Context) RemoteIP() netip.Addr

RemoteIP parses the IP from http.Request.RemoteAddr, normalizes it, and returns a netip.Addr. The returned address may contain a zone identifier. If parsing fails (e.g. the server accepts connections on a Unix domain socket, or the remote address was overwritten), the zero netip.Addr is returned.

func (*Context) Request

func (c *Context) Request() *http.Request

Request returns the http.Request.

func (*Context) Route

func (c *Context) Route() *Route

Route returns the registered Route or nil if the handler is called in a scope other than RouteHandler.

func (*Context) Router

func (c *Context) Router() *Router

Router returns the Router instance.

func (*Context) RoutingPath added in v0.31.0

func (c *Context) RoutingPath() string

RoutingPath returns the canonical path that the router uses for matching. Percent-encoded RFC 3986 unreserved characters (A-Z a-z 0-9 - . _ ~) are decoded while the remaining escape sequences stay encoded, normalized to uppercase hex and distinct from their decoded form (%2F never matches /). Bytes that can never appear unescaped in a path are percent-encoded in place (e.g. é becomes %C3%A9). A typical use is when you need the exact path the router matched on, for example as a cache key.

func (*Context) Scope

func (c *Context) Scope() HandlerScope

Scope returns the HandlerScope associated with the current Context. This indicates the scope in which the handler is being executed, such as RouteHandler, NoRouteHandler, etc.

func (*Context) SetHeader

func (c *Context) SetHeader(key, value string)

SetHeader sets the response header for the given key to the specified value.

func (*Context) SetRequest

func (c *Context) SetRequest(r *http.Request)

SetRequest sets the http.Request.

func (*Context) SetWriter

func (c *Context) SetWriter(w ResponseWriter)

SetWriter sets the ResponseWriter.

func (*Context) Stream

func (c *Context) Stream(code int, contentType string, r io.Reader) (err error)

Stream sends data from an io.Reader with the specified status code and content type.

func (*Context) String

func (c *Context) String(code int, s string) (err error)

String sends a formatted string with the specified status code.

func (*Context) Writer

func (c *Context) Writer() ResponseWriter

Writer returns the ResponseWriter.

type GlobalOption

type GlobalOption interface {
	// contains filtered or unexported methods
}

func WithAllowRegexpParam added in v0.31.0

func WithAllowRegexpParam(enable bool) GlobalOption

WithAllowRegexpParam enables support for regular expressions in route parameters. When enabled, parameters can include regex patterns (e.g., {id:[0-9]+}). When disabled, routes containing regex patterns will fail with a *PatternError.

func WithAutoOptions

func WithAutoOptions(enable bool) GlobalOption

WithAutoOptions enables automatic responses to OPTIONS requests with, by default, a 204 status code. For regular OPTIONS requests, the router responds with the "Allow" header listing methods registered for the matched routes, or calls the NoRoute handler if no route matches. For CORS preflight requests, the router always responds to all request by calling the OPTIONS handler. Note that custom OPTIONS handler take priority over automatic replies. This option is automatically enabled when providing a custom handler with the option WithOptionsHandler.

func WithCollapseDotSegments added in v0.31.0

func WithCollapseDotSegments(opt NormalizeOption) GlobalOption

WithCollapseDotSegments configures how the router handles "." and ".." segments in request paths. Dot segments are removed as defined by RFC 3986 (see CollapseDotSegments). A ".." escaping above the root is rejected with a 400 (see WithRejectPathHandler) before lookup.

Available modes:

  • ExactPath: Dot segments are matched as-is (default).
  • NormalizePath: Dot segments are collapsed before lookup.
  • RedirectPath: When collapsing changes the path, issues a redirect to the collapsed path if it matches a route.

Redirects use the canonical path that the router routed on (see Context.RoutingPath).

When combined with WithMergeSlashes, slashes are merged before dot segments are collapsed.

This option applies globally to all routes.

func WithMaxRouteMatchers

func WithMaxRouteMatchers(max uint16) GlobalOption

WithMaxRouteMatchers set the maximum number of matchers allowed in a route. The default max is math.MaxUint8. Routes exceeding this limit will fail with an error that Is ErrInvalidRoute and ErrTooManyMatchers.

func WithMaxRouteParamKeyBytes

func WithMaxRouteParamKeyBytes(max uint16) GlobalOption

WithMaxRouteParamKeyBytes set the maximum number of bytes allowed per parameter key in a route. The default max is math.MaxUint8. Routes with parameter keys exceeding this limit will fail with a *PatternError.

func WithMaxRouteParams

func WithMaxRouteParams(max uint16) GlobalOption

WithMaxRouteParams set the maximum number of parameters allowed in a route. The default max is math.MaxUint8. Routes exceeding this limit will fail with a *PatternError.

func WithMergeSlashes added in v0.31.0

func WithMergeSlashes(opt NormalizeOption) GlobalOption

WithMergeSlashes configures how the router handles consecutive slashes in request paths.

Available modes:

  • ExactPath: Consecutive slashes are matched as-is (default).
  • NormalizePath: Slashes are merged before lookup.
  • RedirectPath: When merging changes the path, issues a redirect to the merged path if it matches a route.

Redirects use the canonical path that the router routed on (see Context.RoutingPath).

Only raw slashes are merged, an encoded %2F never matches a slash (see MergeSlashes). When combined with WithCollapseDotSegments, slashes are merged before dot segments are collapsed.

This option applies globally to all routes.

func WithMiddlewareFor

func WithMiddlewareFor(scope HandlerScope, m ...MiddlewareFunc) GlobalOption

WithMiddlewareFor attaches middleware to the router for a specified scope. Middlewares provided will be chained in the order they were added. The scope parameter determines which types of handlers the middleware will be applied to. Possible scopes include RouteHandler (regular routes), NoRouteHandler, NoMethodHandler, RedirectSlashHandler, RedirectPathHandler, RejectPathHandler, OptionsHandler, and any combination of these. Use this option when you need fine-grained control over where the middleware is applied.

func WithNoMethod

func WithNoMethod(enable bool) GlobalOption

WithNoMethod enable to returns 405 Method Not Allowed instead of 404 Not Found when the route exist for another http verb. The "Allow" header it automatically set before calling the handler. Note that this option is automatically enabled when providing a custom handler with the option WithNoMethodHandler.

func WithNoMethodHandler

func WithNoMethodHandler(handler HandlerFunc) GlobalOption

WithNoMethodHandler register an HandlerFunc which is called when the request cannot be routed, but the same route exist for other methods. The "Allow" header it automatically set before calling the handler. By default, the DefaultNoMethodHandler is used. Note that this option automatically enable WithNoMethod.

func WithNoRouteHandler

func WithNoRouteHandler(handler HandlerFunc) GlobalOption

WithNoRouteHandler register an HandlerFunc which is called when no matching route is found. By default, the DefaultNoRouteHandler is used.

func WithOptionsHandler

func WithOptionsHandler(handler HandlerFunc) GlobalOption

WithOptionsHandler register an HandlerFunc which is called on automatic OPTIONS requests. By default, the router respond with a 204 status code. The "Allow" header it automatically set before calling the handler (except for CORS preflight request). Note that custom OPTIONS handler take priority over automatic replies. By default, DefaultOptionsHandler is used. Note that this option automatically enable WithAutoOptions.

func WithPrettyLogs

func WithPrettyLogs() GlobalOption

WithPrettyLogs configures the router with human-readable, colorized logging optimized for terminal output. It registers the following middleware at the front of the chain:

This option prioritizes readability over performance and is not recommended for high-throughput applications. For production workloads, prefer structured logging with a performance-oriented slog.Handler such as zerolog or zap.

func WithRejectPathHandler added in v0.31.0

func WithRejectPathHandler(handler HandlerFunc) GlobalOption

WithRejectPathHandler register an HandlerFunc which is called when path normalization rejects the request, e.g. a ".." segment escaping above the root with WithCollapseDotSegments enabled. By default, the DefaultRejectPathHandler replies with a 400 Bad Request.

func WithStrictPathEncoding added in v0.31.0

func WithStrictPathEncoding(enable bool) GlobalOption

WithStrictPathEncoding rejects with a 400 response (see WithRejectPathHandler) any request path containing an unescaped character that RFC 3986 forbids in a path, like é or {, a malformed escape sequence, or a URL.RawPath that is not an encoding of URL.Path. The net/http parser never produces the last two cases, they only appear in manually built requests. When disabled, such requests are matched on their routing path (see Context.RoutingPath). This option is disabled by default.

func WithSystemWideOptions

func WithSystemWideOptions(enable bool) GlobalOption

WithSystemWideOptions enable automatic response for system-wide OPTIONS request (OPTIONS *). When this option is enabled, the router responds with a 200 OK status code and the "Allow" header listing all HTTP methods used across registered routes. Note that to let Fox handle system-wide OPTIONS requests, http.Server.DisableGeneralOptionsHandler must be set to true. This option is enabled by default.

type HandlerFunc

type HandlerFunc func(c *Context)

HandlerFunc is a function type that responds to an HTTP request. It enforces the same contract as http.Handler but provides additional feature like matched wildcard route segments via the Context type. The Context is freed once the HandlerFunc returns and may be reused later to save resources. If you need to hold the context longer, you have to copy it (see Context.Clone method).

Similar to http.Handler, to abort a HandlerFunc so the client sees an interrupted response, panic with the value http.ErrAbortHandler.

HandlerFunc functions should be thread-safe, as they will be called concurrently.

func WrapF

func WrapF(f http.HandlerFunc) HandlerFunc

WrapF is an adapter for wrapping http.HandlerFunc and returns a HandlerFunc function. The route parameters are being accessed by the wrapped handler through the context.

func WrapH

func WrapH(h http.Handler) HandlerFunc

WrapH is an adapter for wrapping http.Handler and returns a HandlerFunc function. The route parameters are being accessed by the wrapped handler through the context.

type HandlerScope

type HandlerScope uint8

HandlerScope represents different scopes where a handler may be called. It also allows for fine-grained control over where middleware is applied.

const (
	// RouteHandler scope applies to regular routes registered in the router.
	RouteHandler HandlerScope = 1 << (8 - 1 - iota)
	// NoRouteHandler scope applies to the NoRoute handler, which is invoked when no route matches the request.
	NoRouteHandler
	// NoMethodHandler scope applies to the NoMethod handler, which is invoked when a route exists, but the method is not allowed.
	NoMethodHandler
	// RedirectSlashHandler scope applies to the internal redirect trailing slash handler, used for handling requests with trailing slashes.
	RedirectSlashHandler
	// RedirectPathHandler scope applies to the internal redirect fixed path handler, used for handling requests that need path cleaning.
	RedirectPathHandler
	// OptionsHandler scope applies to the automatic OPTIONS handler, which handles pre-flight or cross-origin requests.
	OptionsHandler
	// RejectPathHandler scope applies to the internal reject path handler, invoked when path normalization rejects a request.
	RejectPathHandler
	// AllHandlers is a combination of all the above scopes, which can be used to apply middlewares to all types of handlers.
	AllHandlers = RouteHandler | NoRouteHandler | NoMethodHandler | RedirectSlashHandler | RedirectPathHandler | OptionsHandler | RejectPathHandler
)

type HeaderMatcher

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

HeaderMatcher matches a request when the configured header is present with any value equal to the configured one. Multiple values for the same header are evaluated independently and the matcher succeeds on the first match.

func MatchHeader

func MatchHeader(key, value string) (HeaderMatcher, error)

MatchHeader returns a HeaderMatcher that matches when the request contains an HTTP header with the given key and a value equal to the given one. The key is canonicalized via http.CanonicalHeaderKey. An empty key returns an error. See WithHeaderMatcher for the option counterpart.

func (HeaderMatcher) Equal

func (m HeaderMatcher) Equal(matcher Matcher) bool

Equal reports whether matcher is a HeaderMatcher with the same key and value.

func (HeaderMatcher) Key

func (m HeaderMatcher) Key() string

Key returns the canonicalized header key tested by this matcher.

func (HeaderMatcher) Match

func (m HeaderMatcher) Match(c RequestContext) bool

Match reports whether the request contains the configured header with any value equal to the configured one.

func (HeaderMatcher) String

func (m HeaderMatcher) String() string

String returns a textual representation of the matcher in the form "h:key=value".

func (HeaderMatcher) Value

func (m HeaderMatcher) Value() string

Value returns the expected header value.

type HeaderRegexpMatcher

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

HeaderRegexpMatcher matches a request when the configured header is present with any value matching the configured regular expression. Multiple values for the same header are evaluated independently and the matcher succeeds on the first match.

func MatchHeaderRegexp

func MatchHeaderRegexp(key, expr string) (HeaderRegexpMatcher, error)

MatchHeaderRegexp returns a HeaderRegexpMatcher that matches when the request contains an HTTP header with the given key whose value matches the given regular expression. The expression is auto anchored at both ends, requiring a full match of the header value. The key is canonicalized via http.CanonicalHeaderKey. An empty key or invalid regular expression returns an error. See WithHeaderRegexpMatcher for the option counterpart.

func (HeaderRegexpMatcher) Equal

func (m HeaderRegexpMatcher) Equal(matcher Matcher) bool

Equal reports whether matcher is a HeaderRegexpMatcher with the same key and regular expression source.

func (HeaderRegexpMatcher) Key

func (m HeaderRegexpMatcher) Key() string

Key returns the canonicalized header key tested by this matcher.

func (HeaderRegexpMatcher) Match

Match reports whether the request contains the configured header with any value matching the configured regular expression.

func (HeaderRegexpMatcher) String

func (m HeaderRegexpMatcher) String() string

String returns a textual representation of the matcher in the form "hx:key=expr".

func (HeaderRegexpMatcher) Value added in v0.29.4

func (m HeaderRegexpMatcher) Value() string

Value returns the regular expression matching the header.

type Iter

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

Iter provide a set of range iterators for traversing registered methods and routes. Iter capture a point-in-time snapshot of the routing tree. Therefore, all iterators returned by Iter will not observe subsequent write on the router or on the transaction from which the Iter is created.

func (Iter) All

func (it Iter) All() iter.Seq[*Route]

All returns a range iterator over all routes registered in the routing tree. The iterator reflect a snapshot of the routing tree at the time Iter is created. This function is safe for concurrent use by multiple goroutine and while mutation on routes are ongoing. See also Iter.PatternPrefix as an alternative.

Example
f, _ := NewRouter()
it := f.Iter()
for route := range it.All() {
	fmt.Println(slices.Collect(route.Methods()), route.Pattern())
}

func (Iter) Methods

func (it Iter) Methods() iter.Seq[string]

Methods returns a range iterator over all HTTP methods registered in the routing tree. The iterator reflect a snapshot of the routing tree at the time Iter is created. This function is safe for concurrent use by multiple goroutine and while mutation on routes are ongoing.

Example
f, _ := NewRouter()
it := f.Iter()
for method := range it.Methods() {
	fmt.Println(method)
}

func (Iter) NamePrefix

func (it Iter) NamePrefix(prefix string) iter.Seq[*Route]

NamePrefix returns a range iterator over all routes in the routing tree that match a given name prefix. The iterator reflect a snapshot of the routing tree at the time Iter is created. This function is safe for concurrent use by multiple goroutine and while mutation on routes are ongoing.

Example
f, _ := NewRouter()
it := f.Iter()
for route := range it.NamePrefix("ns:default/") {
	fmt.Println(slices.Collect(route.Methods()), route.Name())
}

func (Iter) Names

func (it Iter) Names() iter.Seq[*Route]

Names returns a range iterator over all routes registered in the routing tree with a name. The iterator reflect a snapshot of the routing tree at the time Iter is created. This function is safe for concurrent use by multiple goroutine and while mutation on routes are ongoing. See also Iter.NamePrefix as an alternative.

Example
f, _ := NewRouter()
it := f.Iter()
for route := range it.Names() {
	fmt.Println(slices.Collect(route.Methods()), route.Pattern())
}

func (Iter) PatternPrefix

func (it Iter) PatternPrefix(prefix string) iter.Seq[*Route]

PatternPrefix returns a range iterator over all routes in the routing tree that match a given pattern prefix. The iterator reflect a snapshot of the routing tree at the time Iter is created. This function is safe for concurrent use by multiple goroutine and while mutation on routes are ongoing. Note: Partial parameter syntax (e.g., /users/{name:) is not supported and will not match any routes.

Example
f, _ := NewRouter()
it := f.Iter()
for route := range it.PatternPrefix("/v1/") {
	fmt.Println(slices.Collect(route.Methods()), route.Pattern())
}

func (Iter) Routes

func (it Iter) Routes(pattern string) iter.Seq[*Route]

Routes returns a range iterator over all registered routes in the routing tree that exactly match the provided route pattern. The iterator reflect a snapshot of the routing tree at the time Iter is created. This function is safe for concurrent use by multiple goroutine and while mutation on routes are ongoing.

Example
f, _ := NewRouter()
it := f.Iter()
for route := range it.Routes("/hello/{name}") {
	fmt.Println(slices.Collect(route.Methods()), route.Pattern())
}

type Matcher

type Matcher interface {
	// Match evaluates if the [RequestContext] satisfies this matcher.
	Match(c RequestContext) bool
	// Equal reports whether this matcher is semantically equivalent to another. Implementation must
	// - Handle type checking: matchers of different types are not equal
	// - Be reflexive: m.Equal(m) == true
	// - Be symmetric: m.Equal(n) == n.Equal(m)
	Equal(m Matcher) bool
}

Matcher evaluates if an HTTP request satisfies specific conditions. Matchers are evaluated after hostname and path matching succeeds. All matchers associated with a route must match for the route to be selected. Matcher implementations must be safe for concurrent use by multiple goroutines.

type MatcherOption

type MatcherOption interface {
	// contains filtered or unexported methods
}

type MiddlewareFunc

type MiddlewareFunc func(next HandlerFunc) HandlerFunc

MiddlewareFunc is a function type for implementing HandlerFunc middleware. The returned HandlerFunc usually wraps the input HandlerFunc, allowing you to perform operations before and/or after the wrapped HandlerFunc is executed. MiddlewareFunc functions should be thread-safe, as they will be called concurrently.

func Logger

func Logger(handler slog.Handler) MiddlewareFunc

Logger returns a middleware that logs request information using the provided slog.Handler. It logs details such as the remote or client IP, HTTP method, request path, status code and latency. Status codes are logged at different levels: 2xx at INFO, 3xx at DEBUG (with Location header if present), 4xx at WARN, and 5xx at ERROR.

func Recovery

func Recovery(handler slog.Handler) MiddlewareFunc

Recovery returns a middleware that recovers from any panics, logs the error, request details, and stack trace using the provided slog.Handler and writes a 500 status code response if a panic occurs.

func RecoveryWithFunc

func RecoveryWithFunc(handler slog.Handler, handle RecoveryFunc) MiddlewareFunc

RecoveryWithFunc returns a middleware that recovers from any panics, logs the error, request details, and stack trace using the provided slog.Handler and then calls the handle function to handle the recovery.

func WrapM

func WrapM(m func(http.Handler) http.Handler) MiddlewareFunc

WrapM is an adapter for wrapping http.Handler middleware and returns a MiddlewareFunc function. The route parameters are being accessed by the wrapped handler through the context.

Note that m is invoked on every request, so any setup it does before returning its handler runs per request.

type NormalizeOption added in v0.31.0

type NormalizeOption uint8
const (
	ExactPath NormalizeOption = iota
	NormalizePath
	RedirectPath
)

type Param

type Param struct {
	Key   string
	Value string
}

Param is a single URL parameter captured during routing. Key is the parameter name as declared in the route pattern and Value is the segment captured from the request.

type Params

type Params []Param

Params is the list of URL parameters captured by a route. The order matches the declaration order of parameters in the route pattern.

func ParamsFromContext

func ParamsFromContext(ctx context.Context) Params

ParamsFromContext retrieves the Params captured by the router from a context.Context. It returns nil if no parameters are stored in ctx.

func (Params) Get

func (p Params) Get(name string) string

Get returns the value of the parameter with the given name, or an empty string if no such parameter is present.

func (Params) Has

func (p Params) Has(name string) bool

Has reports whether a parameter with the given name is present.

type PatternError added in v0.28.0

type PatternError struct {
	Pattern string // provided pattern
	Type    string // hostname | path
	Reason  string // syntax | parameter | regexp | constraint
	Hint    string // hint
	Start   int    // start offset of the offending segment
	End     int    // end offset of the offending segment
	// contains filtered or unexported fields
}

func (*PatternError) Error added in v0.28.0

func (e *PatternError) Error() string

Error returns a human-readable error message with a visual pointer to the offending segment.

func (*PatternError) Unwrap added in v0.28.0

func (e *PatternError) Unwrap() error

Unwrap returns the underlying error, if any.

type QueryMatcher

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

QueryMatcher matches a request when the URL query contains the configured key with any value equal to the configured one. Multiple values for the same key are evaluated independently and the matcher succeeds on the first match.

func MatchQuery

func MatchQuery(key, value string) (QueryMatcher, error)

MatchQuery returns a QueryMatcher that matches when the request URL contains a query parameter with the given key and a value equal to the given one. An empty key returns an error. See WithQueryMatcher for the option counterpart.

func (QueryMatcher) Equal

func (m QueryMatcher) Equal(matcher Matcher) bool

Equal reports whether matcher is a QueryMatcher with the same key and value.

func (QueryMatcher) Key

func (m QueryMatcher) Key() string

Key returns the query parameter key tested by this matcher.

func (QueryMatcher) Match

func (m QueryMatcher) Match(c RequestContext) bool

Match reports whether the request URL contains the configured key with any value equal to the configured one.

func (QueryMatcher) String

func (m QueryMatcher) String() string

String returns a textual representation of the matcher in the form "q:key=value".

func (QueryMatcher) Value

func (m QueryMatcher) Value() string

Value returns the expected query parameter value.

type QueryRegexpMatcher

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

QueryRegexpMatcher matches a request when the URL query contains the configured key with any value matching the configured regular expression. Multiple values for the same key are evaluated independently and the matcher succeeds on the first match.

func MatchQueryRegexp

func MatchQueryRegexp(key, expr string) (QueryRegexpMatcher, error)

MatchQueryRegexp returns a QueryRegexpMatcher that matches when the request URL contains a query parameter with the given key whose value matches the given regular expression. The expression is auto anchored at both ends, requiring a full match of the parameter value. An empty key or invalid regular expression returns an error. See WithQueryRegexpMatcher for the option counterpart.

func (QueryRegexpMatcher) Equal

func (m QueryRegexpMatcher) Equal(matcher Matcher) bool

Equal reports whether matcher is a QueryRegexpMatcher with the same key and regular expression source.

func (QueryRegexpMatcher) Key

func (m QueryRegexpMatcher) Key() string

Key returns the query parameter key tested by this matcher.

func (QueryRegexpMatcher) Match

Match reports whether the request URL contains the configured key with any value matching the configured regular expression.

func (QueryRegexpMatcher) String

func (m QueryRegexpMatcher) String() string

String returns a textual representation of the matcher in the form "qx:key=expr".

func (QueryRegexpMatcher) Value added in v0.29.4

func (m QueryRegexpMatcher) Value() string

Value returns the regular expression matching the query parameter.

type RecoveryFunc

type RecoveryFunc func(c *Context, err any)

RecoveryFunc is a function type that defines how to handle panics that occur during the handling of an HTTP request.

type RequestContext

type RequestContext interface {
	// Request returns the current [http.Request].
	Request() *http.Request
	// RemoteIP parses the IP from [http.Request.RemoteAddr], normalizes it, and returns a [netip.Addr].
	// The returned address may contain a zone identifier. If parsing fails (e.g. the server accepts
	// connections on a Unix domain socket, or the remote address was overwritten), the zero [netip.Addr]
	// is returned.
	RemoteIP() netip.Addr
	// ClientIP returns the "real" client IP address based on the configured [ClientIPResolver].
	// The resolver is set using the [WithClientIPResolver] option. There is no sane default, so if no resolver is configured,
	// the method returns [ErrNoClientIPResolver].
	//
	// The resolver used must be chosen and tuned for your network configuration. This should result
	// in a resolver never returning an error -- i.e., never failing to find a candidate for the "real" IP.
	// Consequently, getting an error result should be treated as an application error, perhaps even
	// worthy of panicking.
	//
	// The returned [netip.Addr] may contain a zone identifier.
	ClientIP() (netip.Addr, error)
	// Method returns the request method.
	Method() string
	// RoutingPath returns the canonical path that the router uses for matching. Percent-encoded
	// RFC 3986 unreserved characters (A-Z a-z 0-9 - . _ ~) are decoded while the remaining escape
	// sequences stay encoded, normalized to uppercase hex and distinct from their decoded form
	// (%2F never matches /). Bytes that can never appear unescaped in a path are percent-encoded in
	// place (e.g. é becomes %C3%A9). A typical use is when you need the exact path the router matched on, for example
	// as a cache key.
	RoutingPath() string
	// Host returns the request host.
	Host() string
	// QueryParams parses the [http.Request] raw query and returns the corresponding values. The result is cached after
	// the first call.
	QueryParams() url.Values
	// QueryParam returns the first query value associated with the given key. The query parameters are parsed and
	// cached on first access.
	QueryParam(name string) string
	// Header retrieves the value of the request header for the given key.
	Header(key string) string
}

RequestContext provides read-only access to incoming HTTP request data, including request properties, headers, query parameters, and client IP information. It is implemented by Context.

The RequestContext API is not thread-safe. Its lifetime is limited to the scope of its caller: within a Matcher, it is valid only for the duration of the Matcher.Match call; within a HandlerFunc, it is valid only for the duration of the handler execution. The underlying context may be reused after the call returns.

type ResponseWriter

type ResponseWriter interface {
	http.ResponseWriter
	io.StringWriter
	io.ReaderFrom
	// Status recorded after Write and WriteHeader.
	Status() int
	// Written returns true if the response has been written.
	Written() bool
	// Size returns the size of the written response.
	Size() int
	// FlushError flushes buffered data to the client. If flush is not supported, FlushError returns an error
	// matching [http.ErrNotSupported]. See [http.Flusher] for more details.
	FlushError() error
	// Hijack lets the caller take over the connection. If hijacking the connection is not supported, Hijack returns
	// an error matching [http.ErrNotSupported]. See [http.Hijacker] for more details.
	Hijack() (net.Conn, *bufio.ReadWriter, error)
	// Push initiates an HTTP/2 server push. Push returns [http.ErrNotSupported] if the client has disabled push or if push
	// is not supported on the underlying connection. See [http.Pusher] for more details.
	Push(target string, opts *http.PushOptions) error
	// SetReadDeadline sets the deadline for reading the entire request, including the body. Reads from the request
	// body after the deadline has been exceeded will return an error. A zero value means no deadline. Setting the read
	// deadline after it has been exceeded will not extend it. If SetReadDeadline is not supported, it returns
	// an error matching [http.ErrNotSupported].
	SetReadDeadline(deadline time.Time) error
	// SetWriteDeadline sets the deadline for writing the response. Writes to the response body after the deadline has
	// been exceeded will not block, but may succeed if the data has been buffered. A zero value means no deadline.
	// Setting the write deadline after it has been exceeded will not extend it. If SetWriteDeadline is not supported,
	// it returns an error matching [http.ErrNotSupported].
	SetWriteDeadline(deadline time.Time) error
	// EnableFullDuplex indicates that the request handler will interleave reads from [http.Request.Body]
	// with writes to the [ResponseWriter].
	//
	// For HTTP/1 requests, the Go HTTP server by default consumes any unread portion of
	// the request body before beginning to write the response, preventing handlers from
	// concurrently reading from the request and writing the response.
	// Calling EnableFullDuplex disables this behavior and permits handlers to continue to read
	// from the request while concurrently writing the response.
	//
	// For HTTP/2 requests, the Go HTTP server always permits concurrent reads and responses.
	// If EnableFullDuplex is not supported, it returns an error matching [http.ErrNotSupported].
	EnableFullDuplex() error
}

ResponseWriter extends http.ResponseWriter and provides methods to retrieve the recorded status code, written state, and response size.

type Route

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

Route represents an immutable HTTP route with associated handlers and settings.

func (*Route) Annotation

func (r *Route) Annotation(key any) any

Annotation returns the value associated with this Route for key, or nil if no value is associated with key. Successive calls to Annotation with the same key returns the same result.

func (*Route) ClientIPResolver

func (r *Route) ClientIPResolver() ClientIPResolver

ClientIPResolver returns the ClientIPResolver configured for this Route, if any.

func (*Route) Handle

func (r *Route) Handle(c *Context)

Handle calls the handler with the provided Context. See also Route.HandleMiddleware.

func (*Route) HandleMiddleware

func (r *Route) HandleMiddleware(c *Context, _ ...struct{})

HandleMiddleware calls the handler with route-specific middleware applied, using the provided Context. This method is not intended to be used as the handler for another route, as the middleware chain would be duplicated when registered. To reuse a route's handler, use Route.Handle directly.

func (*Route) Hostname

func (r *Route) Hostname() string

Hostname returns the hostname part of the registered pattern if any.

func (*Route) Matchers

func (r *Route) Matchers() iter.Seq[Matcher]

Matchers returns an iterator over all matchers attached to this Route.

func (*Route) MatchersLen

func (r *Route) MatchersLen() int

MatchersLen returns the number of matchers for this Route.

func (*Route) MatchersPriority

func (r *Route) MatchersPriority() uint

MatchersPriority returns the matchers priority for this Route.

func (*Route) Methods

func (r *Route) Methods() iter.Seq[string]

Methods returns an iterator over all HTTP methods this route responds to (if any), in lexicographical order.

func (*Route) Name

func (r *Route) Name() string

Name returns the name of this Route.

func (*Route) Params

func (r *Route) Params() iter.Seq[string]

Params returns an iterator over all parameters name for this Route.

func (*Route) ParamsLen

func (r *Route) ParamsLen() int

ParamsLen returns the number of parameters for this Route.

func (*Route) Path

func (r *Route) Path() string

Path returns the path part of the registered pattern.

func (*Route) Pattern

func (r *Route) Pattern() string

Pattern returns the registered route pattern.

func (*Route) String

func (r *Route) String() string

func (*Route) TrailingSlashOption

func (r *Route) TrailingSlashOption() TrailingSlashOption

TrailingSlashOption returns the configured TrailingSlashOption for this Route.

type RouteConflictError

type RouteConflictError struct {
	// New is the route that was being registered when the conflict was detected.
	New *Route
	// Conflicts contains the previously registered routes that conflict with New.
	Conflicts []*Route
	// contains filtered or unexported fields
}

RouteConflictError represents a conflict that occurred during route registration. It contains the route being registered, and the existing routes that caused the conflict.

func (*RouteConflictError) Error

func (e *RouteConflictError) Error() string

type RouteNameConflictError

type RouteNameConflictError struct {
	// New is the route that was being registered when the conflict was detected.
	New *Route
	// Conflict is the previously registered route that conflict with New.
	Conflict *Route
}

RouteNameConflictError represents a conflict that occurred during route name registration. It contains the route being registered, and the existing route that caused the conflict.

func (*RouteNameConflictError) Error

func (e *RouteNameConflictError) Error() string

type RouteOption

type RouteOption interface {
	// contains filtered or unexported methods
}

func WithAnnotation

func WithAnnotation(key, value any) RouteOption

WithAnnotation attach arbitrary metadata to routes. Annotations are key-value pairs that allow middleware, handler or any other components to modify behavior based on the attached metadata. Unlike context-based metadata, which is tied to the request lifetime, annotations are bound to the route's lifetime and remain static across all requests for that route. The provided key must be comparable and should not be of type string or any other built-in type to avoid collisions between packages that use route annotation.

func WithMatchersPriority added in v0.31.0

func WithMatchersPriority(priority uint) RouteOption

WithMatchersPriority sets the priority for a route with matchers. When multiple routes share the same pattern (regardless of param names) and have overlapping methods, matchers are evaluated by priority (highest first). Routes with equal priority may be evaluated in any order. Routes without matchers are always evaluated last. If unset or 0, the priority defaults to the number of matchers. Note that routes with specific methods are always evaluated before method-less routes, regardless of priority.

func WithName

func WithName(name string) RouteOption

WithName assigns a name to a route for identification and lookup purposes. The name must be unique among all other routes registered.

type Router

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

Router is a lightweight high performance HTTP request router that support mutation on its routing tree while handling request concurrently.

func MustRouter

func MustRouter(opts ...GlobalOption) *Router

MustRouter returns a ready to use instance of Fox router. This function is a convenience wrapper for NewRouter and panics on error.

func NewRouter

func NewRouter(opts ...GlobalOption) (*Router, error)

NewRouter returns a ready to use instance of Fox router.

Example

This example demonstrates how to create a simple router with pretty logging, which registers the Recovery and Logger middleware.

// Create a new router with pretty logging (Recovery + Logger middleware).
r, _ := NewRouter(WithPrettyLogs())

// Define a route with the path "/hello/{name}", and set a simple handler that greets the
// user by their name.
r.MustAdd([]string{http.MethodGet, http.MethodHead}, "/hello/{name}", func(c *Context) {
	_ = c.String(200, fmt.Sprintf("Hello %s\n", c.Param("name")))
})

// Start the HTTP server using fox router and listen on port 8080
log.Fatalln(http.ListenAndServe(":8080", r))

func (*Router) Add

func (fox *Router) Add(methods []string, pattern string, handler HandlerFunc, opts ...RouteOption) (*Route, error)

Add registers a new route for the given methods, pattern and matchers. On success, it returns the newly registered Route. If an error occurs, it returns one of the following:

It's safe to add a new handler while the router is serving requests. This function is safe for concurrent use by multiple goroutine. To override an existing handler, use Router.Update.

func (*Router) AddRoute

func (fox *Router) AddRoute(route *Route) error

AddRoute registers a new Route. If an error occurs, it returns one of the following:

It's safe to add a new route while the router is serving requests. This function is safe for concurrent use by multiple goroutine. To override an existing route, use Router.UpdateRoute.

func (*Router) Delete

func (fox *Router) Delete(methods []string, pattern string, opts ...MatcherOption) (*Route, error)

Delete deletes an existing route for the given methods, pattern and matchers. On success, it returns the deleted Route. If an error occurs, it returns one of the following:

It's safe to delete a handler while the router is serving requests. This function is safe for concurrent use by multiple goroutine.

func (*Router) DeleteRoute

func (fox *Router) DeleteRoute(route *Route) (*Route, error)

DeleteRoute deletes an existing route that match the provided Route pattern and matchers. On success, it returns the deleted Route. If an error occurs, it returns one of the following:

It's safe to delete a handler while the router is serving requests. This function is safe for concurrent use by multiple goroutine.

func (*Router) Has

func (fox *Router) Has(methods []string, pattern string, matchers ...Matcher) bool

Has allows to check if the given methods, pattern and matchers exactly match a registered route. This function is safe for concurrent use by multiple goroutine and while mutation on routes are ongoing. See also Router.Route as an alternative.

Example
f, _ := NewRouter()
f.MustAdd([]string{http.MethodGet, http.MethodHead}, "/hello/{name}", emptyHandler)
exist := f.Has([]string{http.MethodGet, http.MethodHead}, "/hello/{name}")
fmt.Println(exist) // true

func (*Router) Info added in v0.31.0

func (fox *Router) Info() RouterInfo

Info returns information on the configured global option.

func (*Router) Iter

func (fox *Router) Iter() Iter

Iter returns a collection of range iterators for traversing registered methods and routes. It creates a point-in-time snapshot of the routing tree. Therefore, all iterators returned by Iter will not observe subsequent write on the router. This function is safe for concurrent use by multiple goroutine and while mutation on routes are ongoing.

func (*Router) Len

func (fox *Router) Len() int

Len returns the number of registered route.

func (*Router) Lookup

func (fox *Router) Lookup(w ResponseWriter, r *http.Request) (route *Route, cc *Context, tsr bool)

Lookup performs a manual route lookup for a given http.Request, returning the matched Route along with a Context, and a boolean indicating if the route was matched by adding or removing a trailing slash (trailing slash action recommended). If there is a direct match or a tsr is possible, Lookup always return a Route and a Context. The Context should always be closed if non-nil. This function is safe for concurrent use by multiple goroutine and while mutation on routes are ongoing. See also Router.Match as an alternative.

func (*Router) Match

func (fox *Router) Match(method string, r *http.Request) (route *Route, tsr bool)

Match performs a route lookup for the given method and http.Request. It returns the matching registered Route (if any) along with a boolean indicating if the route was matched by adding or removing a trailing slash (trailing slash action recommended). This function is safe for concurrent use by multiple goroutine and while mutation on routes are ongoing. See also Router.Lookup as an alternative.

Example
f, _ := NewRouter()
f.MustAdd([]string{http.MethodGet, http.MethodHead}, "example.com/hello/{name}", emptyHandler)

req := httptest.NewRequest(http.MethodGet, "/hello/fox", nil)

route, tsr := f.Match(req.Method, req)
fmt.Println(route.Pattern(), tsr) // example.com/hello/{name} false

func (*Router) MustAdd

func (fox *Router) MustAdd(methods []string, pattern string, handler HandlerFunc, opts ...RouteOption) *Route

MustAdd registers a new route for the given methods, pattern and matchers. On success, it returns the newly registered Route. This function is a convenience wrapper for the Router.Add function and panics on error.

func (*Router) Name

func (fox *Router) Name(name string) *Route

Name performs a lookup for a registered route matching the given route name. It returns the Route if a match is found or nil otherwise. This function is safe for concurrent use by multiple goroutines and while mutations on routes are ongoing. See also Router.Route as an alternative.

func (*Router) NewRoute

func (fox *Router) NewRoute(methods []string, pattern string, handler HandlerFunc, opts ...RouteOption) (*Route, error)

NewRoute create a new Route, configured with the provided options. If an error occurs, it returns one of the following:

func (*Router) Route

func (fox *Router) Route(methods []string, pattern string, matchers ...Matcher) *Route

Route performs a lookup for a registered route matching the given methods, pattern and matchers. It returns the Route if a match is found or nil otherwise. This function is safe for concurrent use by multiple goroutine and while mutation on route are ongoing. See also Router.Has or Iter.Routes as an alternative.

func (*Router) ServeHTTP

func (fox *Router) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP is the main entry point to serve a request. It handles all incoming HTTP requests and dispatches them to the appropriate handler function based on the request's method and path.

func (*Router) Txn

func (fox *Router) Txn(write bool) *Txn

Txn create a new read-write or read-only transaction. Each Txn must be finalized with Txn.Commit or Txn.Abort. It's safe to create transaction from multiple goroutine and while the router is serving request. Creating a write transaction blocks while another write transaction is in progress. However, the returned Txn itself is NOT thread-safe. See also Router.Updates and Router.View for managed read-write and read-only transaction.

Example

This example demonstrate how to create an unmanaged read-write transaction.

f, _ := NewRouter()

// Txn create an unmanaged read-write or read-only transaction.
txn := f.Txn(true)
defer txn.Abort()

if _, err := txn.Add([]string{http.MethodGet, http.MethodHead}, "example.com/hello/{name}", func(c *Context) {
	_ = c.String(http.StatusOK, fmt.Sprintf("Hello %s\n", c.Param("name")))
}); err != nil {
	log.Printf("error inserting route: %s", err)
	return
}

// Iter returns a collection of range iterators for traversing registered routes.
it := txn.Iter()
// When Iter() is called on a write transaction, it creates a point-in-time snapshot of the transaction state.
// It means that writing on the current transaction while iterating is allowed, but the mutation will not be
// observed in the result returned by PatternPrefix (or any other iterator).
for route := range it.PatternPrefix("tmp.example.com/") {
	if _, err := txn.Delete(slices.Collect(route.Methods()), route.Pattern()); err != nil {
		log.Printf("error deleting route: %s", err)
		return
	}
}
// Finalize the transaction
txn.Commit()

func (*Router) Update

func (fox *Router) Update(methods []string, pattern string, handler HandlerFunc, opts ...RouteOption) (*Route, error)

Update override an existing route for the given methods, pattern and matchers. On success, it returns the newly registered Route. If an error occurs, it returns one of the following:

Route-specific option and middleware must be reapplied when updating a route. if not, any middleware and option will be removed (or reset to their default value), and the route will fall back to using global configuration (if any). It's safe to update a handler while the router is serving requests. This function is safe for concurrent use by multiple goroutine. To add new handler, use Router.Add method.

func (*Router) UpdateRoute

func (fox *Router) UpdateRoute(route *Route) error

UpdateRoute override an existing Route for the given new Route. If an error occurs, it returns one of the following:

It's safe to update a handler while the router is serving requests. This function is safe for concurrent use by multiple goroutine. To add new route, use Router.AddRoute method.

func (*Router) Updates

func (fox *Router) Updates(fn func(txn *Txn) error) error

Updates executes a function within the context of a read-write managed transaction. If no error is returned from the function then the transaction is committed. If an error is returned then the entire transaction is aborted. Updates returns any error returned by fn. This function is safe for concurrent use by multiple goroutine and while the router is serving request. However Txn itself is NOT thread-safe. See also Router.Txn for unmanaged transaction and Router.View for managed read-only transaction.

Example

This example demonstrate how to create a managed read-write transaction.

f, _ := NewRouter()

// Updates executes a function within the context of a read-write managed transaction. If no error is returned
// from the function then the transaction is committed. If an error is returned then the entire transaction is
// aborted.
if err := f.Updates(func(txn *Txn) error {
	if _, err := txn.Add([]string{http.MethodGet, http.MethodHead}, "example.com/hello/{name}", func(c *Context) {
		_ = c.String(http.StatusOK, fmt.Sprintf("Hello %s\n", c.Param("name")))
	}); err != nil {
		return err
	}

	// Iter returns a collection of range iterators for traversing registered routes.
	it := txn.Iter()
	// When Iter() is called on a write transaction, it creates a point-in-time snapshot of the transaction state.
	// It means that writing on the current transaction while iterating is allowed, but the mutation will not be
	// observed in the result returned by PatternPrefix (or any other iterator).
	for route := range it.PatternPrefix("tmp.example.com/") {
		if _, err := txn.Delete(slices.Collect(route.Methods()), route.Pattern()); err != nil {
			return err
		}
	}
	return nil
}); err != nil {
	log.Printf("transaction aborted: %s", err)
}

func (*Router) View

func (fox *Router) View(fn func(txn *Txn) error) error

View executes a function within the context of a read-only managed transaction. View returns any error returned by fn. This function is safe for concurrent use by multiple goroutine and while mutation on routes are ongoing. However Txn itself is NOT thread-safe. See also Router.Txn for unmanaged transaction and Router.Updates for managed read-write transaction.

Example

This example demonstrate how to create a managed read-only transaction.

f, _ := NewRouter()

// View executes a function within the context of a read-only managed transaction.
_ = f.View(func(txn *Txn) error {
	if txn.Has([]string{http.MethodGet}, "/foo") && txn.Has([]string{http.MethodGet}, "/bar") {
		// Do something
	}
	return nil
})

type RouterInfo

type RouterInfo struct {
	MaxRouteParams        int
	MaxRouteParamKeyBytes int
	MaxRouteMatchers      int
	TrailingSlashOption   TrailingSlashOption
	MergeSlashes          NormalizeOption
	CollapseDotSegments   NormalizeOption
	NoMethod              bool
	AutoOptions           bool
	SystemWideOptions     bool
	ClientIPResolver      bool
	AllowRegexpParam      bool
	StrictPathEncoding    bool
}

RouterInfo holds information on the configured global options.

type SchemeMatcher added in v0.29.0

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

SchemeMatcher matches a request based on the connection scheme ("http" or "https"), derived from the TLS state of the connection. Behind a TLS-terminating reverse proxy, the matcher reflects the proxy-to-server hop, not the original client connection.

func MatchScheme added in v0.29.0

func MatchScheme(scheme string) (SchemeMatcher, error)

MatchScheme returns a SchemeMatcher that matches when the request connection scheme equals the given value. Only "http" and "https" are accepted (case-insensitive); any other value returns an error. The scheme is derived solely from the TLS state of the connection between the client and the server, ignoring r.URL.Scheme to prevent spoofing via HTTP/1.1 absolute-form requests. See WithSchemeMatcher for the option counterpart.

func (SchemeMatcher) Equal added in v0.29.0

func (m SchemeMatcher) Equal(matcher Matcher) bool

Equal reports whether matcher is a SchemeMatcher with the same scheme.

func (SchemeMatcher) Match added in v0.29.0

func (m SchemeMatcher) Match(c RequestContext) bool

Match reports whether the request's connection scheme equals the configured one. The scheme is derived from http.Request.TLS: "https" when set, "http" otherwise.

func (SchemeMatcher) Scheme added in v0.29.0

func (m SchemeMatcher) Scheme() string

Scheme returns the scheme tested by this matcher ("http" or "https").

func (SchemeMatcher) String added in v0.29.0

func (m SchemeMatcher) String() string

String returns a textual representation of the matcher in the form "s:scheme".

type TrailingSlashOption

type TrailingSlashOption uint8
const (
	ExactSlash TrailingSlashOption = iota
	RelaxedSlash
	RedirectSlash
)

type Txn

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

Txn is a read or write transaction on the routing tree.

A Txn must not be used concurrently by multiple goroutines. It is however safe to run multiple transactions from different goroutines, each on its own Txn. Read transactions operate on a point-in-time snapshot and never block, while write transactions are serialized by the router. A write Txn must be finalized with Txn.Commit or Txn.Abort. Routing requests never block, even while a write transaction is in progress.

func (*Txn) Abort

func (txn *Txn) Abort()

Abort cancel the transaction. This is a noop for read transactions, already aborted or committed transactions.

func (*Txn) Add

func (txn *Txn) Add(methods []string, pattern string, handler HandlerFunc, opts ...RouteOption) (*Route, error)

Add registers a new route for the given methods, pattern and matchers. On success, it returns the newly registered Route. If an error occurs, it returns one of the following:

To override an existing handler, use Txn.Update.

func (*Txn) AddRoute

func (txn *Txn) AddRoute(route *Route) error

AddRoute registers a new Route. If an error occurs, it returns one of the following:

To override an existing route, use Txn.UpdateRoute.

func (*Txn) Commit

func (txn *Txn) Commit()

Commit finalize the transaction. This is a noop for read transactions, already aborted or committed transactions.

func (*Txn) Delete

func (txn *Txn) Delete(methods []string, pattern string, opts ...MatcherOption) (*Route, error)

Delete deletes an existing route for the given methods, pattern and matchers. On success, it returns the deleted Route. If an error occurs, it returns one of the following:

func (*Txn) DeleteRoute

func (txn *Txn) DeleteRoute(route *Route) (*Route, error)

DeleteRoute deletes an existing route that match the provided Route pattern and matchers. On success, it returns the deleted Route. If an error occurs, it returns one of the following:

func (*Txn) Has

func (txn *Txn) Has(methods []string, pattern string, matchers ...Matcher) bool

Has allows to check if the given methods, pattern and matchers exactly match a registered route. See also Txn.Route as an alternative.

func (*Txn) Iter

func (txn *Txn) Iter() Iter

Iter returns a collection of range iterators for traversing registered routes. When called on a write transaction, Iter creates a point-in-time snapshot of the transaction state. Therefore, writing on the current transaction while iterating is allowed, but the mutation will not be observed in the result returned by iterators collection.

func (*Txn) Len

func (txn *Txn) Len() int

Len returns the number of registered route.

func (*Txn) Lookup

func (txn *Txn) Lookup(w ResponseWriter, r *http.Request) (route *Route, cc *Context, tsr bool)

Lookup performs a manual route lookup for a given http.Request, returning the matched Route along with a Context, and a boolean indicating if the route was matched by adding or removing a trailing slash (trailing slash action recommended). If there is a direct match or a tsr is possible, Lookup always return a Route and a Context. The Context should always be closed if non-nil. See also Txn.Match as an alternative.

func (*Txn) Match

func (txn *Txn) Match(method string, r *http.Request) (route *Route, tsr bool)

Match performs a route lookup for the given method and http.Request. It returns the matching registered Route (if any) along with a boolean indicating if the route was matched by adding or removing a trailing slash (trailing slash action recommended). See also Txn.Lookup as an alternative.

func (*Txn) Name

func (txn *Txn) Name(name string) *Route

Name performs a lookup for a registered route matching the given route name. It returns the Route if a match is found or nil otherwise. See also Txn.Route as an alternative.

func (*Txn) Route

func (txn *Txn) Route(methods []string, pattern string, matchers ...Matcher) *Route

Route performs a lookup for a registered route matching the given methods, pattern and matchers. It returns the Route if a match is found or nil otherwise. See also Txn.Has or Iter.Routes as an alternative.

func (*Txn) Snapshot

func (txn *Txn) Snapshot() *Txn

Snapshot returns a point in time snapshot of the current state of the transaction. Returns a new read-only transaction or nil if the transaction is already aborted or committed.

func (*Txn) Truncate

func (txn *Txn) Truncate() error

Truncate remove all routes registered in the router. Truncating on a read-only transaction returns ErrReadOnlyTxn.

func (*Txn) Update

func (txn *Txn) Update(methods []string, pattern string, handler HandlerFunc, opts ...RouteOption) (*Route, error)

Update override an existing route for the given methods, pattern and matchers. On success, it returns the newly registered Route. If an error occurs, it returns one of the following:

Route-specific option and middleware must be reapplied when updating a route. if not, any middleware and option will be removed (or reset to their default value), and the route will fall back to using global configuration (if any). To add a new handler, use Txn.Add.

func (*Txn) UpdateRoute

func (txn *Txn) UpdateRoute(route *Route) error

UpdateRoute override an existing Route for the given new Route. If an error occurs, it returns one of the following:

To add a new route, use Txn.AddRoute.

Directories

Path Synopsis
internal
constraints
Package constraints defines a set of useful constraints to be used with type parameters.
Package constraints defines a set of useful constraints to be used with type parameters.

Jump to

Keyboard shortcuts

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