Giter Club home page Giter Club logo

router.cr's Introduction

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

router.cr's People

Contributors

luislavena avatar mamantoha avatar romanhargrave avatar s0kil avatar stakach avatar tbrand avatar veelenga avatar vladfaust avatar waghanza avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

router.cr's Issues

Version badge

Hi @tbrand,

I can see on README that version is pin to 0.2.2

However, the last version is 0.2.4

Regards,

Parsing POST/GET params?

Is there an example for how to retrieve POST params from an HTML form? router.cr seems to ignore GET and POST params, only providing the params available in the URL (e.g., '/posts/:post_id').

Am I missing something? Or is that a limitation of this router framework?

Is the project maintained?

Hello,

I've checked your project and it either:

  • Has specs failed with the latest Crystal (0.26.1)
  • Has no any specs to run so it cannot be validated to be up to date
  • Has the latest commit dated more than a year ago

Your project is in Awesome Crystal list, so I'd like to ask if the project maintained?

I'll initiate a PR to remove your project from the list if you don't respond positively within 7 days.

I'm sorry if I'm wrong, because I've been checking many shards with automated script.

Thanks,
Vlad.

Incorrect release tag

This makes shards think it has to fetch the repo on every build. To fix this the git tag should match the version in shards.yml

Add constraint to route

Hi @tbrand,

It could be useful to add some path constraint to routes, I mean adding a regex (or else to allow / or not some pathes).

For example, adding /user/:id(\\d+), create a route for /user/0, /user/1... but not/user/a`

WDYT ?

PS : This example is taken from kitura

Question: Separating Route Class

What is best way to separate route in different class. I'm thinking to create modular app and each has its own route declaration.

Commit messages

I don't want to step on your toes here but how about writing some more descriptive commit messages in the future? :) "Fix" doesn't tell me much. :P

Rename API to Action

When a server is accessed with a path, it idiomatically triggers something a.k.a. an action happens. My proposal is to replace API (alias API = Proc(HTTP::Server::Context, Hash(String, String)) with Action.

Feature Request: Add method or class handler

As addition to use block handler, it would be nice to add method handler or class handler to make the router code cleaner and follow separation of concern.

# block handler
get "/" do |context, params|
 context.response.print "Hello router.cr!"
 context
end

# method handler
get "/" home
post "/user" add_user

def home(context, params) : HTTP::Server::Context
  context.response.print "Hello router.cr!"
  context
end

# class handler
article = Article.new
get "/article/:id" article.get 
put "/article/:id" article.update 

class Article
  def get(context, params)
    context.response.print "Hello router.cr!"
    context
  end
end

"routers" don't cooperate when combined

You should be able to combine multiple handlers that can handle different routes (e.g. a handler for /favicon.ico or a monitoring API, etc...)

Route::RouteHandler#call(context) raises an exception when it can't route rather than "passing the torch" to the next handler. This makes combining routers impossible.

Performance optimization with slice

Is it possible to improve performance if the router could convert string to bytes using to_slice?

From the benchmark, to_slice would cost <4 nanoseconds and efficient to compare using ==

Range  17.51M ( 57.11ns) (ยฑ10.77%)  32 B/op  14.44ร— slower
Slice     252.8M (  3.96ns) (ยฑ 5.81%)   0 B/op        fastest

Range:
key[0..-2]

Slice:
(key.to_slice)[0,key.size-2]

Add tests for TechEmpower Framework Benchmarks

Hi @tbrand, I like router.cr as middleware.

Would be possible to add router.cr to https://github.com/TechEmpower/FrameworkBenchmarks ?

TFB have three categories:

  1. Middlewares, like puma, nginx
  2. Micro frameworks like Kemal, Sinatra, Flask
  3. Full frameworks like Phoenix, Django, Amber.

I think router.cr can compete in the first category, What do you think?

Tests for crystal-raw: https://github.com/TechEmpower/FrameworkBenchmarks/tree/master/frameworks/Crystal/crystal

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.