An Afternoon with Sinatra and Heroku | Howard Dierking
This is a quick summary of my experience today getting my first “hello world” Web API written in Sinatra and deployed to Heroku. For those of you who are already experts, you’ll probably find lots of things I did wrong or could have done differently – please tell me or point me to resources. I want to get better at this. It was a great experience that left me wanting to do more of it…
Getting setup to develop my first Sinatra application on Heroku was basically a 2 step process. First I needed to get a Ruby environment that I could run on Windows. This was the first point of confusion because it seems like there are quite a few different options. However, it seems like rubyinstaller.org has basically risen to the top (at least in terms of Google results). Ruby installer is a single Windows MSI which installs Ruby, some initial gems, and a bunch of other stuff that I’m not sure about. By install, as best I can tell, it lays files down on disk and adds entries to the path environment variable. After installing, I can test my ruby files by simply running Ruby myfile.rb in mingw32. The second thing that I installed via MSI was the Heroku toolbelt. Again, I’m not sure what all this installation did (I suspect more files and path mods), but afterwards I can run commands such as heroku login.
First, here were the resources I followed to get setup on Heroku:
The quickstart took me through the process of signing up (which I had already done), installing the tools, logging in through the command line which would setup ssh keys if I didn’t already have them (I did, and that was a little problematic), and then it linked off to the platform specific getting started guide.
The getting started with Ruby guide walked me through the process of developing a hello world api in Sinatra, testing it locally and then deploying it to Heroku. The first step was setting up the Heroku tools again, which I think may have installed Git again on my system. Need to check that. Writing my first Sinatra app was crazy easy – in fact, this was all it consisted of:
get ‘/’ do
Testing locally consisted of simply running ‘ruby -rubygems web.rb’. The next part of the setup dealt with creating a gemfile (using Bundler?). This part still seems a little too magical for me, but I went through the tutorial and created a gemfile declaring a single dependency on Sinatra. The sample also had a dependency on a gem called thin, but I tried installing that gem a couple times and it failed because I didn’t have some kind of Ruby developer kit. I’m not sure what that meant, but I can run and deploy without thin, so that’s what I did. On a quick glance, it appears that thin is a Web server?
The next step was to create a procfile which from what I can tell instructs the runtime (Cedar, is the runtime that Heroku is using I believe) how to launch my application in production. The template also mentioned Rack, and I’m not sure how that relates to the other stuff, but it wasn’t a blocker.
Now, I tried installing foreman because the getting started guide made it sound like by doing this, I could get a more realistic test of how my app would run in Heroku. The problem was that I couldn’t get foreman to run on Windows, and a thread on github made it sound like foreman simply doesn’t run on Windows at all. Looks like I may be buying a Mac in the not-too-distant future. At any rate, not having foreman didn’t keep me from being able to deploy.
The final step was super easy once I resolved an SSH issue – though I think that this was because I’m already comfortable with Git. I simply initialized a new Git repo in my working folder and then ran the following command:
heroku create –stack cedar
This caused heroku to do a bunch of provisioning stuff, including create a new remote repository – which it then kindly added to my local Git config. This meant that deploying my app was simply:
git push heroku master
Initially, the remote Git repo hung up on me because I had created a new SSH public key on my laptop and it wasn’t the key that I originally configured with Heroku – a search, however, lead me to the right heroku command to add my SSH key:
After running this, I was able to push to heroku, and saw that heroku spun up a Web server and launched my app. 4 seconds later, I ran ‘heroku ps’ and saw that it had crashed. This was because I initially got something wrong in my procfile – so I fixed it and simply updated/pushed my repo. It then redeployed and worked beautifully (at least as far as cUrl is concerned).
In general, there are still some things I need to know better before I’ll feel good about everything I’m doing – there were a lot of terms thrown out there in my getting started experience, and I’m confident that I barely scratched the surface on all of them – specifically, I’m thinking of:
And of course, I need to continue to get better at Ruby in general. However, this was my first experience – and all in all, it was a good one.