• Stars
    star
    55
  • Rank 519,684 (Top 11 %)
  • Language
    Crystal
  • License
    MIT License
  • Created over 8 years ago
  • Updated over 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

a standalone router for crystal

crouter Build Status

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. 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:

dependencies:
  crouter:
    github: jreinert/crouter

Usage

require "crouter"

class MyController
  private getter context, params
  def initialize(@context, @params)
  end

  def my_action
    # do something
    context.response << "hi there"
  end
end

class MyRouter < Crouter::Router
  get "/" do
    context.response << "hello world"
  end

  post "/path/with/:param" do
    context.response << "you passed #{params["param"]}"
  end

  get "/path/with(/optional(/:parts))" do
    context.repsonse << "you passed #{params["parts"]? || "nothing"}"
  end

  put "/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
end

class 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
end

puts "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 Joakim Reinert - creator, maintainer

More Repositories

1

autopass

a rofi frontend for pass
Ruby
51
star
2

ncurses-crystal

ncurses bindings for crystal
Crystal
24
star
3

open_api.cr

Type safe OpenAPI spec library
Crystal
14
star
4

yanc-font

Yet another neat condensed font. Based on gohufont.
Shell
14
star
5

passed

A simple script to modify password store entries with sed commands
Ruby
12
star
6

crystal-jsonapi

JSON API serializing for crystal
Crystal
12
star
7

fuzzr

A fast fuzzy search library for the browser, written in rust, compiled to webasm
Rust
7
star
8

gpg.cr

GnuPG bindings for crystal
Crystal
6
star
9

rofi-curses

a rofi clone for ncurses
Crystal
5
star
10

cresult

Rust-like error handling for crystal
Crystal
5
star
11

nats.cr

Native NATS client library in crystal language
Crystal
5
star
12

env_config.cr

Type safe ENV-Based application configs
Crystal
3
star
13

dynmenu

A dmenu wrapper written in ruby with support for subtle wm functions
Ruby
3
star
14

buzz.cr

An mqtt library for crystal using libmosquitto
Crystal
3
star
15

xxhash-crystal

Crystal bindings for xxHash - an extremely fast non-cryptographic hashing algorithm
Crystal
3
star
16

archup

(Very) quickly install a bootable arch linux system
Shell
3
star
17

ketchup

a highly customizable pomodoro timer written in crystal
Crystal
3
star
18

xapian-crystal

xapian bindings for crystal
Crystal
3
star
19

paramnoia

Params for the paranoid
Crystal
3
star
20

hless

less with syntax highlighting
Crystal
2
star
21

scalr

Fast, caching image scaling service
Crystal
2
star
22

docker.cr

Easy workaround for quirks you will encounter when running crystal in Docker.
Crystal
2
star
23

cgit-auth

Crystal
2
star
24

wee-slack

A WeeChat plugin for Slack.com. Synchronizes read markers, provides typing notification, search, etc..
Python
1
star
25

socky

Because TCP sockets sock!
1
star
26

esp_utils

Commonly used utility functions for my esp projects
C++
1
star
27

weather_logger

Simple esp based weather logger using the BME280 sensor
Arduino
1
star
28

rrepo

Tiny library implementing repository pattern in ruby
Ruby
1
star
29

scrobbly.cr

Music scrobbler with multiple front and backends
Crystal
1
star
30

conquer

one bar to rule them all
Ruby
1
star
31

paladin

Shotgun for crystal apps
Crystal
1
star
32

ghiblibot

A mastodon bot that posts random images from Studio Ghibli movies
Crystal
1
star
33

audition

Simple now-playing server
Crystal
1
star
34

pam.d

pam files
Shell
1
star
35

slide_server

Simple server for sharing markdown written slides from a base directory
JavaScript
1
star