gitmyhub

httprouter

Go ★ 17k updated 1y ago

A high performance HTTP request router that scales well

HttpRouter is a fast, lightweight HTTP router for Go that adds named URL path variables and method-based routing to Go web services, using a memory-efficient radix tree so matching stays fast even with many routes.

Gosetup: easycomplexity 2/5

HttpRouter is a lightweight HTTP request router for Go, a programming language commonly used to build web services. A router, also called a multiplexer or mux for short, is the part of a web server that decides which piece of code should handle each incoming request based on the URL and the request method like GET or POST. HttpRouter exists because Go's built-in standard router does not support variables in URL paths and does not distinguish between request methods, which is awkward when building modern web APIs.

The library is built around a compact tree structure called a radix tree, which groups routes that share a common prefix. This lets the router quickly find a match even when there are many routes or long URL paths, and matching is designed to allocate almost no extra memory. A request can only match exactly one registered route, so there are no ambiguous overlaps. URL paths can contain named placeholders like :name to capture a single segment, or catch-all placeholders like *filepath to capture everything after a point in the path.

Beyond raw matching, the README highlights quality-of-life behavior: it can automatically redirect requests when a trailing slash is wrong, fix common path mistakes like wrong casing or stray slashes, serve static files, and recover from crashes inside a handler through a panic handler. It also has built-in support for OPTIONS requests and 405 Method Not Allowed responses, which makes it a natural fit for hierarchical REST-style APIs.

You would reach for HttpRouter when writing a Go web service or API and want predictable, fast routing without pulling in a full web framework. The full README is longer than what was provided.

Where it fits