Fastr – A Web Framework for Ruby

Foreword

Every month I go on a binge and learn something new. The most recent binge resulted in a new web framework, called fastr.

I’ve always used Rails when I needed to create a web application in Ruby, but despite how great it is to code in, the performance and concurrency is not up to par.

Don’t get me wrong — Rails can scale, but it is not inherently very good when it comes to an individual instance’s performance. While this is hopefully changing in Rails 3, I wanted to see what kind of concurrency and performance I could get out of a simple EventMachine web application. Thus, fastr was born.

Fastr’s Architecture

Fastr is nothing special. It is an extremely simple MVC style web framework, similar in configuration to Rails.

What makes it different though, is how it handles requests and responses.

The core of fastr runs on an EventMachine reactor, which is basically just a big event loop. EventMachine is very capable of maintaining thousands of connections in one instance. This is great when you want an application that supports Comet and chunked responses. There is very minimal overhead in having thousands of connections that have occasional data flowing through.

EventMachine and Fastr also support the ability to defer a response until a long running task finishes executing. This is nice because it won’t block the rest of the application. The only catch is you are limited to EventMachine’s thread pool size, which I believe defaults to 20. You can always up this number though, if you want.

Routing

The routing is very similar to Rails, an example routes.rb file looks like this:

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

Controllers

Controllers are simple, with just a list of actions that are called based on the route. Actions should return a valid Rack response. There are convenience methods in the controller that help in creating Rack responses.

The nice thing about the controllers in Fastr is that you can defer responses with ease. Here is an example of a deferred response:

  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

The alternative render methods also exist, where you can render a HAML template or just text:

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

Future

For now, fastr is just a toy framework for me, that I hope to share with anyone interested in learning about building web frameworks.

I intend to start writing some tests as well as making fastr more robust. I don’t mean to displace any other Ruby web framework, but as I learn with fastr I hope to contribute what I learn back to projects like Rails and Sinatra.

Also, I’d like to get em-mysql or equivalent integrated so that you can really enjoy a event-driven web framework — event with database access.

Check out the GitHub page for more examples on getting fastr up and running.

GitHub: http://github.com/chrismoos/fastr

2 comments so far, add yours

Unlocking the full potential of the iPhone

Since Apple launched the iPhone in 2007 there has been an unprecedented growth in the mobile phone market. From new advances in hardware, such as touch screens, to operating systems mimicking the full power of a desktop computer. Never has a smartphone been able to penetrate the demographics of 10 year olds, to teenagers, or mom and dad — until the iPhone.

There are now hundreds of thousands of applications built for mobile phones, with every use case you can imagine. But even with the growth and advances of the past few years, the industry is still in its youth.

Apple, with its enormous success, is still constantly on the front page because of its questionable practice — sole control of the iPhone platform. Unlike desktop operating systems, such as Windows or Mac OS X, where the user is in complete control of the operating system, the iPhone is a jail, with Apple deciding what can come in and go out.

The App Store, the only way for a developer to target an iPhone, or a user to install an application on the iPhone, is in constant censorship mode. Certain API’s are not allowed and questionable content is put to rest.

It is time that Apple realizes that it is not only the hardware in the iPhone, or the software that powers it, that makes it a powerful force in the mobile phone market. The power is in the apps, the platform, and in the imaginations of developers.

Even though there are many applications for the iPhone, there could be so much more. The iPhone is a powerful device that can be used to provide a platform for mobile applications in every industry possible. Medical, industrial, retail — the list goes on. But as long as Apple is in control of what happens on the iPhone, developers will never be able to harness the full potential of such a beautiful device.

Apple now has the ability to turn the iPhone, and the new iPad, into a commodity device. The PC of the mobile world. My intuition tells me that Apple won’t let this happen. They prefer to maintain complete control of their platform, and while making it a premium and luxury item, they stop it from attaining the growth that it most definitely can achieve.

The future is in Apple’s hands — and they have the ability to make it an awesome place.

Leave the first comment

Groovy scripts and JVM Security

Groovy is a very cool dynamic language for the JVM. Because it runs on the JVM, it also has the great security features as well.

Let’s see how we can run trusted code and allow a dynamic (possibly user defined) script to execute with limited permissions. We will also see how the script can call functions in our trusted code and how we can elevate privileges to allow the untrusted script to get access to trusted data.

Setting up a security policy

We are going to create a java policy file that gives our script and groovy full access, but no permissions for our untrusted scripts

~/.java.policy

grant codeBase "file:/Users/chris/securegroovy/trusted.groovy" {
  permission java.security.AllPermission;
};
 
grant codeBase "file:${groovy.home}/-" {
    permission java.security.AllPermission;
};
 
grant codeBase "file:/restrictedScript" {
 
};

Continue Reading

Getting started with Scala using SBT

One of my biggest gripes with Java (and all the languages that run on the JVM) is getting my project setup and building it. Maven is not my favorite, and ant..well..I don’t like it either. Fortunately, if you want to start a new project in Scala, there is a great build tool available that takes a lot of the pain out of project management and building – SBT, simple-build-tool.

sbt is a simple build tool for Scala projects that aims to do the basics well. It requires Java 1.5 or later.

Installing SBT

I’m using Mac OS X, but the following instructions should be pretty much the same on any Unix based OS.

You can find the latest version of SBT here.

cd ~
wget http://simple-build-tool.googlecode.com/files/sbt-launcher-0.5.6.jar
sudo mv sbt-launcher-0.5.6.jar /usr/local/bin/sbt-launcher.jar
echo "java -Xmx512M -jar /usr/local/bin/sbt-launcher.jar \"\$@\"" | sudo tee /usr/local/bin/sbt
sudo chmod +x /usr/local/bin/sbt

This will install the SBT jar and create a script called sbt that will allow you to run the sbt jar.

Just type sbt and press enter, and you now have access to sbt.

$ sbt
Project does not exist, create new project? (y/N/s) : n

Continue Reading

AjaxTask – a rails plugin for managing background tasks

SOAP, Background Tasks, and AJAX

Recently in Rails I’ve been interacting with various SOAP services and running them in the background with Workling. I needed to relay the SOAP response to the client’s web browser, so I decided to use AJAX to poll the status of my background tasks.

This is great if you have < 30 second background tasks running, but don't want to block a user (and a request).

The Solution

I created a Rails plugin, called AjaxTask, that has two components:

  • Methods to use in your controller to define a task handler and create tasks
  • Javascript library to manage the AJAX between the browser and the handler.

GitHub Link: http://github.com/chrismoos/ajaxtask

In a nutshell, the client initiates a task, the handler responds with a task ID, and the client polls at a user defined interval until the task has finished, or has an error.

The plugin takes the pain out of implementing the handler, as well as the Javascript. All you have to do is run code for your task, and periodically update the status.

I am using Workling to run my background tasks, as well as maintain the status using Workling’s return store.

Okay, enough with the intro, here is the example.

Continue Reading