Skip to content

Latest commit

 

History

History
205 lines (130 loc) · 5.58 KB

README.md

File metadata and controls

205 lines (130 loc) · 5.58 KB

Your next favourite rack based micro framework that is totally addition free! Have a cup of awesomeness with your performance designed framework!

The idea behind is simple. Keep the dependencies and everything as little as possible, while able to write pure rack apps, that will do nothing more than what you defined.

If you want see fancy magic, you are in a bad place buddy! This includes that it do not have such core extensions like activesupport that monkey patch the whole world.

Routing can handle any amount of endpoints that can fit in the memory, so if you that crazy to use more than 10k endpoint, you still dont have to worry about response speed.

It was inspirited by sinatra, grape, and the pure use form of rack. It's in production, powering Back Ends on Heroku

Installation

Add this line to your application's Gemfile:

gem 'rack-app'

And then execute:

$ bundle

Or install it yourself as:

$ gem install rack-app

Usage

config.ru

basic

require './bootstrap.rb'

class App < Rack::App

  desc 'some hello endpoint'
  get '/hello' do
    'Hello World!'
  end

end

complex

require './bootstrap.rb' 

class App < Rack::App

  mount SomeAppClass

  headers 'Access-Control-Allow-Origin' => '*',
          'Access-Control-Expose-Headers' => 'X-My-Custom-Header, X-Another-Custom-Header'

  serializer do |object|
    object.to_s
  end

  desc 'some hello endpoint'
  get '/hello' do
    return 'Hello World!'
  end

  desc 'some restful endpoint'
  get '/users/:user_id' do
    response.status = 201
    params['user_id'] #=> restful parameter :user_id
    say #=> "hello world!"
  end

  desc 'some endpoint that has error and will be rescued'
  get '/make_error' do
    raise(StandardError,'error block rescued')
  end


  def say
    "hello #{params['user_id']}!"
  end

  error StandardError, NoMethodError do |ex|
    {:error=>ex.message}
  end

  root '/hello'

end

you can access Rack::Request with the request method and Rack::Response as response method.

By default if you dont write anything to the response 'body' the endpoint block logic return will be used

Testing

for testing use rack/test or the bundled testing module for writing unit test for your rack application

require 'spec_helper'
require 'rack/app/test'

describe App do

  include Rack::App::Test

  rack_app described_class

  describe '/hello' do
    # example for params and headers and payload use
    subject{ get(url: '/hello', params: {'dog' => 'meat'}, headers: {'X-Cat' => 'fur'}, payload: 'some string') }

    it { expect(subject.status).to eq 200 }

    it { expect(subject.body.join).to eq "Hello World!" }
  end

  describe '/users/:user_id' do
    # restful endpoint example
    subject{ get(url: '/users/1234') }

    it { expect(subject.body.join).to eq 'hello 1234!'}

    it { expect(subject.status).to eq 201 }

  end

  describe '/make_error' do
    # error handled example
    subject{ get(url: '/make_error') }

    it { expect(subject.body.join).to eq '{:error=>"error block rescued"}' }
  end

end

Example Apps To start with

  • Dump duration with zero business logic or routing: 2.4184169999892074e-06 s

    • no routing
    • return only a static array with static values
  • Rack::App duration with routing lookup: 2.9978291999967683e-05 s

    • with routing
    • with value parsing and reponse object building
  • Grape::API duration with routing lookup: 0.0002996424499999746 s

    • with routing
    • with value parsing and reponse object building
  • Rack::App 9.995314276086763x faster (0.00026966415800000693 sec) that Grape::API

  • returning a simple rack response array without any logic is 12.395832480544698x faster (2.7559874999978477e-05 sec) that Rack::App

  • the same dumb empty proc call is 123.90024135676842x faster than Grape::API (0.0002972240329999854 sec)

This was measured with multiple endpoints like that would be in real life example. I feared do this for Rails that is usually slower than Grape :S To be honest, I measured with grape because that is one of my favorite micro framework

Roadmap

Team Backlog

If you have anything to say, you can leave a comment. :)

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/adamluzsi/rack-app.rb This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.