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:
Next, lets require Cloudkit and Vegas.
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:
Thats it. Heres the whole app:
#!/usr/bin/env ruby 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: