evizitei / fastr

Micro web framework for Ruby and EventMachine.

Home Page:http://chrismoos.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

fastr

Micro web framework for Ruby. Should be used with an EventMachine rack server.

Getting Started

$ sudo gem install fastr
$ fastr init helloworld
$ cd helloworld
$ thin -p 5000 start

Directory Structure

The directory structure is similar to rails:

  • app/(config/controller/views/models)

  • lib

  • test

Routes

The routes are configured in app/config/routes.rb

router.draw do |route|
	route.for '/:controller/:action'
	#route.for '/home/:action', :action => '[A-Za-z]+'
	#route.for '/test', :to => 'home#index'
end

Settings

Various settings can be configured in app/config/settings.rb

config.log_level = Logger::DEBUG

Controller

class HomeController < Fastr::Controller
  def index
    render(:text, "Hello, world!")
  end
end

Return a view in a controller

The return for a controller is just a rack response, i.e [200, {“Content-Type” => “text/plain”}, “Hello, World!”]

You can also use the following render methods:

render(:text, "My text")

With HAML, the template is rendered and any instance variables in your controller are available in the template.

render(:haml, :template => "index") # this searches for index.haml in your app/views/ folder

Deferred Responses

fastr also lets you return a deferred response. This is useful if you want to chunk the response back to the client, or have a long running operation that you want to perform without blocking EventMachine.

The following is an example of a deferred response. It executes a sleep which normally would block EventMachine, but by using response.task, we tell EventMachine to run this code in its internal thread pool and when finished the callback is executed.

The following is an example of a controller action.

def long_running_task
  defer_response(200, {"Content-Type" => "text/plain"}) do |response|
    puts "in our deferred response...now we can do cool stuff!"
    response.send_data("hey\n")

    long_task = proc {
      log.debug "Sleeping for 5 seconds...but this won't block other requests"
      sleep(5)
      log.debug "Finished sleeping, returning response to client."
      return "finished"
    }

    callback = proc { |result|
      log.debug "Callback result: #{result}"
      response.send_data("#{result}\n")
      response.succeed
    }

    response.task(long_task, callback)
  end
end

Current Status

Right now just the base is done. The controller supports a very basic render method.

Note on Patches/Pull Requests

  • Fork the project.

  • Make your feature addition or bug fix.

  • Add tests for it. This is important so I don’t break it in a future version unintentionally.

  • Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)

  • Send me a pull request. Bonus points for topic branches.

Copyright © 2010 Chris Moos. See LICENSE for details.

About

Micro web framework for Ruby and EventMachine.

http://chrismoos.com

License:MIT License


Languages

Language:Ruby 100.0%