lucky_router

The router for the Lucky framework, can be used outside Lucky
0.1.4 Yanked release released
luckyframework/router
22 8 3
Lucky

route.cr

Build Status Dependency Status devDependency Status

The default web server of the Crystal is quite good but it weak at routing. Kemal is an awesome defacto standard web framework for Crystal, but it's too fat for some purpose.

route.cr is a minimum but powerful path router for Crystal web server. See the amazing performance of route.cr here

Installation

Add this to your application's shard.yml:

dependencies:
  route:
    github: tbrand/route.cr

Usage

require "route"

Include Route to utilize this.

class WebServer
  include Route
end

To define API, call API.new with context and params(optional) where context is HTTP::Server::Context and params is Hash(String, String). All APIs have to return the context.

@index = API.new do |context|
  context.response.print "Hello route.cr"
  context
end

Define your route at somewhere. In this example, define it in a contructor.

class WebServer
  def initialize
    get "/", @index
  end
end

To activate the routes, use routeHandler.

class WebServer
  def run
    server = HTTP::Server.new(3000, routeHandler)
    server.listen
  end
end

Finally, run your server.

web_server = WebServer.new
web_server.run

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

class WebServer

  @user = API.new do |context, params|
    context.response.print params["id"] # get :id in url from params
    context
  end

  def initialize
    get "/user/:id", @user
  end
end

See sample for details.

Contributing

  1. Fork it ( https://github.com/tbrand/route.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
lucky_router:
  github: luckyframework/router
  version: ~> 0.1.4
License MIT
Crystal 0.21.1

Authors

Dependencies 1

  • radix
    {'github' => 'luislavena/radix'}

Development Dependencies 0

Dependents 2

Last synced .
search fire star recently