Git Product home page Git Product logo

route.cr's Introduction

Build Status GitHub release


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

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

router.cr also contains

  • Convenient rendering tool
  • Access profiler

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

In the following sample codes, class WebServer ... end will be omitted. To initialize RouteHandler

@route_handler = RouteHandler.new

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. In this example, params is omitted. (The usage of params is later)

@index = API.new do |context|
  context.response.print "Hello router.cr"
  context # returning context
end

Define your routes in a draw block.

draw(@route_handler) do # Draw routes
  get "/", @index
end

To activate the routes

def run
  server = HTTP::Server.new(3000, @route_handler) # Set RouteHandler to your server
  server.listen
end

Finally, run your server.

web_server = WebServer.new
web_server.run

See sample 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
  @route_handler = RouteHandler.new

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

  def initialize
    draw(@route_handler) do
      get "/user/:id", @user
    end
  end
end

params also includes query params such as /user?id=3. Here is an example.

class WebServer
  @route_handler = RouteHandler.new

  @user = API.new do |context, params|
    response_body = "user: "
    # Get a query param like /user?id=3
    response_body += params["id"] if params.has_key?("id")
    context.response.print response_body
    context
  end

  def initialize
    draw(@route_handler) do
      get "/user", @user
    end
  end
end

See sample for details.

Rendering views(.ecr)

router.cr also support simple rendering function to render .ecr file with parameters.

First, define your view like

def_view :view_sample, "sample/public/hello.ecr", name: String, num: Int32

where "sample/public/hello.ecr" is

My name is <%= @name %>. The number is <%= @num %>.

:view_sample is an identifier of the view. "sample/public/hello.ecr" is a relative path to the file. name and num are parameters used in "sample/public/hello.ecr". Note that these parameters have to be class variables in the file. See here. When you render them, just call render_view like

context.response.print render_view(:view_sample, "tbrand", 10)

See sample for details.

Get profiles

router.cr also serves ProfileHandler. By using this, you can easily get the access data like

[ GET /one ]        Access: 10          Total: 704.0µs      Ave: 70.4µs
[ GET /two ]        Access: 9           Total: 309.0µs      Ave: 34.3µs
[ GET /three ]      Access: 9           Total: 262.0µs      Ave: 29.1µs

For this, just set ProfileHandler to your server like

server = HTTP::Server.new(3000, [ProfileHandler.new, @route_handler])

After running your server, try accessing "/profile".

See sample 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

route.cr's People

Contributors

tbrand avatar romanhargrave avatar gmartsenkov avatar luislavena avatar veelenga avatar

Watchers

James Cloos avatar

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.