development full of
merriment and sense

Deploying Sinatra Apps on Dreamhost

Geoffrey on October 9, 2008 at 11:22 pm

So I LOVE creating small apps as a way of trying out new things. The problem is that I rarely deploy them anywhere. Many end up sitting in my /workspace directory until I decide I need to reclaim the space and probably won’t work on it ever again.

Recently, the Dallas Relevance folks have been meeting at Panera while we wait for our office space to materialize. Most of the time everything works out nicely: free wifi, decent coffee, plenty of room to spread out. But one thing that doesn’t work correctly while at Panera is tinyurl.com. For some reason, tinyurl is blocked by Panera filters. This would be fine for most, but since my twitter friends insist on using tinyurl to post links in their tweets, it is annoying not being able to see what is going on.

Microapp to the Rescue

So I figured this would be an opportunity for simple little that let me enter the tinyurl, figure out where it was going to redirect to, and go ahead and redirect me there. This way I am never accessing the evil tinyurl.com directly from the Panera network, but instead letting my little app do that for me.

The app was pretty easy to write. I used Sinatra and created one “controller” and one “view”. Within a few minutes, Sinatra had “taken the stage on port 4567″ and my app was working. Locally.

Give the app a home

The next challenge came when I tried to deploy it to my trusty Dreamhost account. I love Dreamhost for playing around with small apps. You get unlimited domains, they have a pretty cool admin control panel, and they support deploying Rails applications with Passenger Phusion. And since Passenger Phusion 2.0 supports Rack enabled Ruby apps, I knew I should be able to deploy this new app to my Dreamhost account. A quick search turned up a useful post with information on deploying to Sinatra apps on Dreamhost. Unfortunately, the first attempt didn’t work.

Always check the logs

So I went to the logs to see what went wrong… wait there weren’t any logs! Fortunately I found this post for logging with Sinatra apps. Unfortunately, the logs didn’t help. Apparently, my problem ran deeper. So like all good debuggers, I started commenting out code and printing out where I was in the app. The first thing I commented out was the called to render the view using ERB. Turns out you can configure where the root of the app is located. Apparently the root path for a Sinatra app running on Dreamhost is not exactly the path where you deployed it. Sinatra::Application.default_options[:root] looked like this:

/home/.machinename/username/app.domain.com/Rack: /home/.machinename/username

Additional configuration needed

Looking through the Sinatra source turned up the needed configuration changes need:

path = "/path/to/app"

Sinatra::Application.default_options.merge!(
  :root => path,
  :views => path + '/views',
  :public => path + '/public',
  :run => false,
  :env => :production
)

A quick deploy later and the application was up and running. Tomorrow we are meeting at Panera and I will get to see what everyone is tweeting about.

Addendum: Deployment too?

Since Sinatra apps are so small, you could just copy everything up to the server manually. But I like have a little Rake task to do that for me. It just touches the tmp/restart.txt that Passenger uses to know when to restart and then uses rsync to copy the files up to the server.

desc 'Deploy to the server using rsync'
task :restart do
  sh "touch tmp/restart.txt"
end

desc 'Deploy to the server using rsync'
task :deploy => :restart do
  cmd = "rsync -ruv * #{USERNAME}@#{DOMAIN}:#{DEPLOY_PATH}"
  sh cmd
end

Take a look at the code

I have posted the code on Github for everyone to take a look at. Enjoy.

Filed under: Dallas, Dreamhost, Sinatra, Web Applications, microapps

Using Google Charts with Rails

Geoffrey on August 22, 2008 at 12:10 pm

With one of my recent microapps, UnscientificPolls.com, I wanted to show the polling data in more interesting ways than just the vote counts. Charting was the logical conclusion, but how do it was a more difficult decision.

Some of the criteria I had for choosing the charting solution included: ease of use (it is microapp, of course), compatible with shared hosting environment, fast, easy to customize.

Some of the libraries I looked at included: flot with jquery, gruff, scruffy, sparklines, and googlecharts. I settled on the googlecharts library because I didn’t need the interactive features of flot and I didn’t want to worry about RMagick needed for gruff, scruffy, or sparklines.

Google Charts API

The Google Charts API is an interesting tool that lets you dynamically generate charts using a “simple” URL scheme. The usage policy is very generous too: “There’s no limit to the number of calls per day you can make to the Google Chart API.”

This would allow me to offload the image generation to Google (who supposedly has quite a bit of computing power) and let my application, in a shared hosting environment, focus on collecting votes.

Enter googlecharts

The challenge with the Google Charts API “simple” url scheme is that it would very tedious to have generate it by concatenating the strings together. Fortunately, Matt Aimonetti built the googlecharts gem for Ruby. You can get it from Rubyforge (gem install googlecharts) or Github (gem install mattetti-googlecharts).

Installing googlecharts in my Rails App

With googlecharts installed on my machine I could start using it, by adding it to my config/environment.rb file.

Rails::Initializer.run do |config|
  config.gem "googlecharts", :lib => "gchart"
end

Since the file we need to include is named “gchart”, not “googlecharts”, we have to specify the :lib => "gchart" option.

I also didn’t want to worry about installing in on the deployment machine, so I unpacked it to the vendor/gems folder using rake gems:unpack.

Now to the Charts

Once all that was in place the challenge was getting the data into a format that would be easy to pass to the library. It turns out, that wasn’t too challenging either.

The Helper

In my view helper module I created a method that would collect the data needed for the chart.

  def pie_chart poll
    @pie_chart ||= {
      :data => poll.choices.collect(&:votes_count),
      :colors => poll.choices.collect {|c| c.winner? ? "264409" : "8A1F11" }
    }
  end

This just loops over the choices and collects the needed data and puts it in an easy to use Hash.

The View

    <%= Gchart.pie :size => '240x160',
                   :title => 'Vote split',
                   :data => pie_chart(@poll)[:data],
                   :bar_colors => pie_chart(@poll)[:colors],
                   :format => 'image_tag' %>

Using googlecharts Gchart made it easy to build the “simple” url needed for a pie chart using the Google Charts API (also supports line, scatter, venn, sparklines, and meter charts) I didn’t even have to add the tag because I could pass the :format => 'image_tag' and one was generated for me.

Conclusion

I was extremely happy with how quick and easy it was to get some simple charts into my application (check them out at UnscientificPolls.com). The response time from Google seems to be as fast as if the images were stored locally. It also saved me the headache of installing with RMagick. This is definitely a good fit for simple graphs and charts in a Rails application.

Filed under: Dreamhost, GitHub, JQuery, Projects, Rails, Ruby, Web Applications, googlecharts, microapps

CSS Mockups for Ads

Geoffrey on August 15, 2008 at 3:43 pm

Occasionally I need to mockup where the ads are going to go in an application (it has to pay for itself somehow, right?). Rather than putting the ads into the application while I am still doing development, I use some simple CSS to put a placeholder where the ads will go. In Rails, it looks like this:

<div class="ads vertical_tower">
  <% if RAILS_ENV == 'production' -%>
    <script>... Live Ad Code Goes Here </script>
  <% else -%>
    Ads Go Here
  <% end -%>
</div>

Then I can use my simple ad template CSS to make it standout. Check out the css source on Github

Filed under: CSS, Projects, Rails, Web Applications, Web Development, microapps, wireframing

Microapps Encourage Hacking

Geoffrey on June 3, 2008 at 8:54 am

Small Train
photo by Jeff Belmonte

I am back from RailsConf 2008 and two of my favorite talks were “Microapps for Fun and Profit” by Erik Kastner and “23 Hacks” by Nathaniel Talbott. I have recently been toying around with creating small little apps where I can try out new ideas and sharpen my skills.

One of those apps is the Template Generator Pro. It was a really simple little app the generates funny cover letters, two week notices, job postings, and more. Not a lot to it. What did I learn? I deployed it to SliceHost (my previous apps have been deployed to DreamHost) and starting learning more about hosting and system administration. I also had a chance to port the Nonsense Perl script to a Ruby version. That was fun!

Tools of the Trade

What am I using for my microapps? The first ones (CathechizeMe and TemplateGeneratorPro) were small Rails applications. But that is alot of overhead and not a lot of “micro” in that. So for new things I am looking at Sinatra for a framework and Stone or ActiveRecord with SQLite for persistance. I like JQuery for the Javascript and BluePrint CSS helps me make it look pretty fairly easily. Open Source Web Design and Open Web Design help to stimulate the creative aspects of the designs.

Check it Out

You can see some my little hacks being stored on my GitHub account: http://www.github.com/gdagley. I also have some projects from work at http://www.github.com/relevance.

Filed under: CSS, GitHub, JQuery, JavaScript, RailsConf, SQLite, Sinatra, SliceHost, Web Applications, microapps, mongrel, nginx

  • gdagley on Twitter

  • gdagley on del.icio.us

Powered by WordPress