Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/repomaa/crouter
a standalone router for crystal
https://github.com/repomaa/crouter
Last synced: about 1 month ago
JSON representation
a standalone router for crystal
- Host: GitHub
- URL: https://github.com/repomaa/crouter
- Owner: repomaa
- License: mit
- Created: 2015-11-09T00:52:15.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2019-01-01T05:20:15.000Z (almost 6 years ago)
- Last Synced: 2024-10-27T13:28:24.624Z (about 2 months ago)
- Language: Crystal
- Size: 462 KB
- Stars: 55
- Watchers: 2
- Forks: 4
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-crystal - crouter - A standalone router (Routing)
README
# crouter [![Build Status](https://travis-ci.org/jreinert/crouter.svg?branch=master)](https://travis-ci.org/jreinert/crouter)
A standalone router for crystal
## Features
- route params (also optional and nested optional)
- grouping under a prefix
- handle response with either block or seperate controller
- support for query params (also mixed with route params)
- most errors reveal themselves already at compile time## Benchmark results
Benchmarked with non-trivial route patterns. See
[src/benchmark.cr](src/benchmark.cr). Due to performance optimizations
compile-time increases with the amount of routes.```
requests per second
without router (raw server throughput) 345.02k (± 3.91%) fastest
through router with 32 routes 248.48k (± 3.56%) 1.39× slower
through router with 64 routes 251.07k (± 3.57%) 1.37× slower
through router with 128 routes 126.84k (± 2.49%) 2.72× slower
through router with 256 routes 167.36k (± 3.69%) 2.06× slower
```## Installation
Add this to your application's `shard.yml`:
```yaml
dependencies:
crouter:
github: jreinert/crouter
```## Usage
```crystal
require "crouter"class MyController
private getter context, params
def initialize(@context, @params)
enddef my_action
# do something
context.response << "hi there"
end
endclass MyRouter < Crouter::Router
get "/" do
context.response << "hello world"
endpost "/path/with/:param" do
context.response << "you passed #{params["param"]}"
endget "/path/with(/optional(/:parts))" do
context.repsonse << "you passed #{params["parts"]? || "nothing"}"
endput "/handle/with/controller", "MyController#my_action"
group "/group" do
put "/routes", "MyGroupController#my_action"
group "/or/even/:nest" do
post "/them" do
context.response << "with params! #{params["nest"]}"
end
end
end
endclass MyRestAPI < Crouter::Router
group "/posts" do
get "/", "PostsController#index"
get "/:id", "PostsController#show"
get "/:id/edit", "PostsController#edit"
post "/", "PostsController#create"
put "/:id", "PostsController#update"
delete "/:id", "PostsController#delete"
end
endputs "Listening on http://localhost:8989"
HTTP::Server.new(8989, [HTTP::LogHandler.new, MyRestAPI.new("/api"), MyRouter.new])
```## Contributing
1. Fork it ( https://github.com/jreinert/crouter/fork )
2. Create your feature branch (git checkout -b my-new-feature)
3. Commit your changes (git commit -am 'Add some feature')
4. Push to the branch (git push origin my-new-feature)
5. Create a new Pull Request## Contributors
- [jreinert](https://github.com/jreinert) Joakim Reinert - creator, maintainer