GithubHelp home page GithubHelp logo

tempbottle / router.cr Goto Github PK

View Code? Open in Web Editor NEW

This project forked from tbrand/router.cr

0.0 1.0 0.0 56 KB

Minimum High Performance Middleware for Crystal Web Server.

License: MIT License

Crystal 100.00%

router.cr's Introduction

Build Status GitHub release


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.๐Ÿš€

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(3000, route_handler)
    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 romanhargrave avatar tbrand avatar veelenga avatar vladfaust avatar

Watchers

 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.