lucky_router v0.6.2
LuckyRouter
A library for routing HTTP request with Crystal
Installation
Add this to your application's shard.yml:
dependencies:
lucky_router:
github: luckyframework/lucky_router
Usage
require "lucky_router"
router = LuckyRouter::Matcher(Symbol).new
router.add("get", "/users", :index)
router.add("delete", "/users/:id", :delete)
router.match("get", "/users").payload # :index
router.match("get", "/users").params # {} of String => String
router.match("delete", "/users/1").payload # :delete
router.match("delete", "/users/1").params # {"id" => "1"}
router.match("get", "/missing_route").payload # nil
Matching without parameters
When only the payload is needed, match_payload skips the parameter hash and capture strings:
router.match_payload("delete", "/users/1") # :delete
router.match_payload("get", "/missing") # nil
The live matcher's match and match! return a separate mutable parameter hash for every successful request. Matching methods are case-sensitive. Registering a GET route also registers the lowercase head alias.
Compiling registered routes
Applications that finish registering routes before serving requests can opt into a compiled snapshot:
compiled = router.compile
match = compiled.match!("delete", "/users/1")
match.params["id"] # "1"
match.params["missing"]? # nil
match.params.to_h # {"id" => "1"}
compiled.match_payload("get", "/users") # :index
Compiled lookups return a CompiledMatch with read-only CompiledParams. Parameters support hash-style lookup, fetch, and iteration. Up to two captures fit in the result itself. Larger sets share the compiled route's name index. Use params.to_h when an API requires a Hash(String, String) or you need a mutable copy.
Snapshots index exact static paths, compress literal prefixes, and exclude dynamic siblings that cannot handle the requested method. They preserve route precedence, capture names, optional arguments, globs, URI decoding, and trailing slash behavior. Compilation takes extra time and memory; the original matcher keeps its live route tree and has no static-index lookup overhead.
A snapshot retains the routes and payload references present when it was built. Call router.compile again after adding routes or mutating the public fragment containers. Updating a payload object's contents is visible through either matcher because the payload object is shared.
Frameworks with their own static lookup can use router.compile(static_index: false) to avoid a duplicate index and its probe. All routes, including static and encoded static paths, remain available through the compact trie. The default keeps the exact static index enabled.
See performance measurements and tradeoffs for the benchmark commands and the alternatives evaluated.
Contributing
- Fork it ( https://github.com/luckyframework/lucky_router/fork )
- Create your feature branch (git checkout -b my-new-feature)
- Make your changes
- Run
./bin/testto run the specs, build shards, and check formatting - Commit your changes (git commit -am 'Add some feature')
- Push to the branch (git push origin my-new-feature)
- Create a new Pull Request
Contributors
- paulcsmith Paul Smith - creator, maintainer
lucky_router
- 25
- 10
- 1
- 5
- 1
- about 2 hours ago
- September 14, 2017
MIT License
Mon, 30 Nov 2026 15:22:35 GMT