Vegas: Rack it up

Even though I’ve done some presentations on it already, for some reason, Vegas, the simple sinatra starter, hasn’t gotten much press here. Maybe it was because I was a little embarrassed about its lack of test coverage, or just how simple it was. No longer, as of today, not only does it have some test coverage, but its actually awesome enough to talk about.

Vegas::Runner is dirt simple. Basically, take your existing (or brand new) Sinatra, create a bin/ file and pass your app class to Vegas::Runner. Now when you run the bin, Vegas takes care of PID’s, options, finding a suitable Rack::Handler, etc. (More information here). Gembox is the canonical example, but there are others out there.

Before today, even though I claimed otherwise, Vegas was pretty dependent on Sinatra. I was O.K. with that. Sinatra had some niceties for starting up that I found it easier to rely on. Then I realized the potential of mounting/launching other, non-Sinatra Rack applications.

For example, Cloudkit is pretty cool. With the newest version of Vegas and Cloudkit, I can create a single file, command-line launch-able data based web service.

First, I create a bin. Lets call it ‘cloudcity’. The first thing I need to do is tell the bin that we’re going to execute this with ruby:

#!/usr/bin/env ruby

Next, lets require Cloudkit and Vegas.

#!/usr/bin/env ruby

require 'rubygems'
require 'vegas'
require 'cloudkit'

OK, now that our env, is setup, we just have to build a little JSON schemaless data storage. Rack::Builder lets us just group all of the Rack middlewares and extensions we want into an ‘app’:

app = Rack::Builder.new do
  expose :people, :places
end

Next we just need to pass this app to Vegas::Runner and give it a name:

Vegas::Runner.new(app, 'cloudcity')

Thats it. Heres the whole app:

#!/usr/bin/env ruby

require 'rubygems'
require 'vegas'
require 'cloudkit'

app = Rack::Builder.new do
  expose :people, :places
end

Vegas::Runner.new(app, 'cloudcity')

Now if we run: cloudcity in the terminal, Vegas will find an open port and launch us directly into the app. For now, we get this informative but semi-useless message:

Buuuuut, if we hit ’/people’ we get a friendly JSON delivery:

{"total":0,"offset":0,"uris":[]}

So that was a pretty trivial example. Vegas didnt add that much to Cloudkit’s existing awesomeness. However, take this as a prod to think about the possibilites. You could build a javascript app, that uses cloudkit as storage, package it as a gem, with a Vegas powered bin, and have an easily distributable browser-based application with JSON storage. The beauty of Cloudkit, is that besides Rack and Ruby, there arent really any other dependencies to worry about. I haven’t tried it yet, but potentially, you could use Vegas to distribute a modern (Rack-based) Rails app (the database part might be a little tough, though).

Comments are closed.

About

QuirkeyBlog is Aaron Quint's perspective on the ongoing adventure of Code, Life, Work and the Web.

twitter/@aq.

instagram/@quirkey.

github/quirkey.

QuirkeyBlog is proudly powered by WordPress

Categories