Code Monkey home page Code Monkey logo

crouter's Introduction

crouter Build Status

A standalone router for crystal

Features

  • route params (also optional and nested optional)
  • grouping under a prefix
  • handle response with either block or seperate controller
  • support for query params (also mixed with route params)
  • most errors reveal themselves already at compile time

Benchmark results

Benchmarked with non-trivial route patterns. See src/benchmark.cr. Due to performance optimizations compile-time increases with the amount of routes.

requests per second
without router (raw server throughput) 345.02k (± 3.91%)       fastest
         through router with 32 routes 248.48k (± 3.56%)  1.39× slower
         through router with 64 routes 251.07k (± 3.57%)  1.37× slower
        through router with 128 routes 126.84k (± 2.49%)  2.72× slower
        through router with 256 routes 167.36k (± 3.69%)  2.06× slower

Installation

Add this to your application's shard.yml:

dependencies:
  crouter:
    github: jreinert/crouter

Usage

require "crouter"

class MyController
  private getter context, params
  def initialize(@context, @params)
  end

  def my_action
    # do something
    context.response << "hi there"
  end
end

class MyRouter < Crouter::Router
  get "/" do
    context.response << "hello world"
  end

  post "/path/with/:param" do
    context.response << "you passed #{params["param"]}"
  end

  get "/path/with(/optional(/:parts))" do
    context.repsonse << "you passed #{params["parts"]? || "nothing"}"
  end

  put "/handle/with/controller", "MyController#my_action"

  group "/group" do
    put "/routes", "MyGroupController#my_action"
    group "/or/even/:nest" do
      post "/them" do
        context.response << "with params! #{params["nest"]}"
      end
    end
  end
end

class MyRestAPI < Crouter::Router
  group "/posts" do
    get    "/",         "PostsController#index"
    get    "/:id",      "PostsController#show"
    get    "/:id/edit", "PostsController#edit"
    post   "/",         "PostsController#create"
    put    "/:id",      "PostsController#update"
    delete "/:id",      "PostsController#delete"
  end
end

puts "Listening on http://localhost:8989"
HTTP::Server.new(8989, [HTTP::LogHandler.new, MyRestAPI.new("/api"), MyRouter.new])

Contributing

  1. Fork it ( https://github.com/jreinert/crouter/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

  • jreinert Joakim Reinert - creator, maintainer

crouter's People

Contributors

rx14 avatar

Watchers

James Cloos avatar  avatar  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.