• Stars
    star
    258
  • Rank 152,701 (Top 4 %)
  • Language
    Crystal
  • License
    MIT License
  • Created about 7 years ago
  • Updated almost 3 years ago

Reviews

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

Repository Details

Minimum High Performance Middleware for Crystal Web Server.

Build Status GitHub tag


The default web server of the Crystal is quite good πŸ˜„ but it weak at routing 😒.
Kemal or other web frameworks written in Crystal are awesome πŸ˜„, but it's too fat for some purpose 😒.

router.cr is a minimum but High Performance middleware for Crystal web server.
See the amazing performance of router.cr here.:rocket:

Installation

Add this to your application's shard.yml:

dependencies:
  router:
    github: tbrand/router.cr

Usage

Basic usage

require "router"

Include Router to utilize router.cr.

class WebServer
  include Router
end

Define a method to draw all routes for your web server.

class WebServer
  include Router
  
  def draw_routes
    # Drawing routes HERE!
  end
end

In that method, call HTTP method name (downcase) like get or post with PATH and BLOCK where

  • PATH : String
  • BLOCK : block of HTTP::Server::Context, Hash(String, String) -> HTTP::Server::Context
class WebServer
  include Router

  def draw_routes
    get "/" do |context, params|
      context.response.print "Hello router.cr!"
      context
    end
  end
end

Here we've defined a GET route at root path (/) that just print out "Hello router.cr" when we get access. To activate (run) the route, just define run methods for your server with route_handler

class WebServer
  include Router
  
  def draw_routes
    get "/" do |context, params|
      context.response.print "Hello router.cr!"
      context
    end
  end
  
  def run
    server = HTTP::Server.new(route_handler)
    server.bind_tcp 8080
    server.listen
  end
end

Here route_handler is getter defined in Router. So you can call route_handler at anywhere in WebServer instance.

Finally, run your server.

web_server = WebServer.new
web_server.draw_routes
web_server.run

See sample and tips for details.

Path parameters

params is a Hash(String, String) that is used when you define a path parameters such as /user/:id (:id is a parameters). Here is an example.

class WebServer
  include Router

  def draw_routes
    get "/user/:id" do |context, params|
      context.response.print params["id"] # get :id in url from params
      context
    end
  end
end

See sample and tips for details.

Contributing

  1. Fork it ( https://github.com/tbrand/router.cr/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

  • tbrand Taichiro Suzuki - creator, maintainer

More Repositories

1

neph

A modern command line job processor, similar with make command
Crystal
202
star
2

dystopia

Anonymity on the Internet by Transparent way.
Rust
93
star
3

mssh

Simple and powerful job executor on remote nodes. πŸš€
Crystal
40
star
4

cargo-tomlfmt

Formatting Cargo.toml
Rust
25
star
5

zir

Realizes to write macros in any scripts into any languages.
Crystal
22
star
6

iemon

Crystal
19
star
7

OneItem

Simple implementation for one item selected RecyclerView. This makes it easier to use arbitrary VideoView (such as MediaPlayer) in RecyclerView. This library includes RecyclerView.LayoutManager and Recyclerview.OnScrollListener.
Java
17
star
8

fastlane-plugin-bluepill

Fastlane plugin to use Bluepill powered by LinkedIn
Ruby
15
star
9

awesome-logger

πŸ‘‘ Awesome Logger is SIMPLE and FLEXIBLE logger for Crystal applications. πŸ‘‘
Crystal
14
star
10

byzan

Distributed blockchain KVS with high availability written in Rust
Rust
10
star
11

tokoroten

Using multiple processes in Crystal
Crystal
8
star
12

OutOfMemory

The developers community of Q and A.
Rust
8
star
13

pixvas

Create pixel image in your terminal
Crystal
7
star
14

roulette

Simulate, Analyze and Play Roulette with Algorithms
Crystal
6
star
15

cdk-lambda-log

"tail" the latest Lambda function's log by one line
TypeScript
4
star
16

famo

Command line tool for caching builds on S3
Rust
2
star
17

gaga

Rust
1
star
18

slack-logger

Crystal
1
star
19

ncpu.cr

Get number of cpus in Crystal
Crystal
1
star
20

rp

Experimental implementation aims to be substitute for `cp` command.
Rust
1
star
21

MobileCUDA

C
1
star
22

helnet

Practice about network stuff
Rust
1
star