julienschmidt /httprouter

A high performance HTTP request router that scales well

★ 17,137⑂ 1,467👁 303⑃ 147● 237▤ 4
GoBSD-3-ClauseCreated 2013-12-05pkg.go.dev/github.com/julienschmidt/httprouter
Download: v1.3v1.3-1.3.0 ↓1.3.02019-09-30
+0
Daily
+2
Weekly
+5
Monthly
17,137
stars
1,467
forks
303
watchers
147
prs
237
issues
4
releases

README

HttpRouter  

HttpRouter is a lightweight high performance HTTP request router (also called multiplexer or just mux for short) for Go.

In contrast to the default mux of Go's net/http package, this router supports variables in the routing pattern and matches against the request method. It also scales better.

The router is optimized for high performance and a small memory footprint. It scales well even with very long paths and a large number of routes. A compressing dynamic trie (radix tree) structure is used for efficient matching.

 Features

Only explicit matches: With other routers, like http.ServeMux, a requested URL path could match multiple patterns. Therefore they have some awkward pattern priority rules, like longest match or first registered, first matched. By design of this router, a request can only match exactly one or no route. As a result, there are also no unintended matches, which makes it great for SEO and improves the user experience.

Stop caring about trailing slashes: Choose the URL style you like, the router automatically redirects the client if a trailing slash is missing or if there is one extra. Of course it only does so, if the new path has a handler. If you don't like it, you can turn off this behavior.

Path auto-correction: Besides detecting the missing or additional trailing slash at no extra cost, the router can also fix wrong cases and remove superfluous path elements (like ../ or //). Is CAPTAIN CAPS LOCK one of your users? HttpRouter can help him by making a case-insensitive look-up and redirecting him to the correct URL.

Parameters in your routing pattern: Stop parsing the requested URL path, just give the path segment a name and the router delivers the dynamic value to you. Because of the design of the router, path parameters are very cheap.

Zero Garbage: The matching and dispatching process generates zero bytes of garbage. The only heap allocations that are made are building the slice of the key-value pairs for path parameters, and building new context and request objects (the latter only in the standard Handler/HandlerFunc API). In the 3-argument API, if the request path contains no parameters not a single heap allocation is necessary.

Best Performance: Benchmarks speak for themselves. See below for technical details of the implementation.

No more server crashes: You can set a Panic handler to deal with panics occurring during handling a HTTP request. The router then recovers and lets the PanicHandler log what happened and deliver a nice error page.

Perfect for APIs: The router design encourages to build sensible, hierarchical RESTful APIs. Moreover it has built-in native support for OPTIONS requests and 405 Method Not Allowed replies.

Of course you can also set custom NotFound and  MethodNotAllowed handlers and serve static files.

 Usage

This is just a quick introduction, view the Docs for details.

    $ go get github.com/julienschmidt/httprouter

and use it, like in this trivial example:

go
packa

Top issues

Latest release

v1.3 v1.3.0 2019-09-30
- Fixed license wording to be properly detected as the BSD 3-Clause License
- Fixed case-insensitive path lookup for multibyte runes (263)
- Fixed a bug in the calculation of the maximum number of parameters (261)
- Added a check preventing adding handlers for an empty path, avoid a out-of-range panic (204)
- Fixed a wildcard conflict check (172)
- The methods in Allowed headers are now sorted (248)
- Added an option to add a global handler for automatic responses to OPTIONS requests (156, 200, 214, 260)
- Minor performance optimizations and documentation fixes

The required minimum G

Snapshot: 2026-11-16T09:23:14Z · Data license Apache-2.0 · JSON