Preamble
Sometime last year I had several opportunities to build completely custom applications which would have to run on Linux. Coming from a 90% Win background that sounded less than appealing. However, I was looking for a new development experience and this article is a result of that search.
One of the opportunities fell through because the client demanded that I use PHP with MySql. That’s all well and good and it’s great there are a lot of people who like PHP but I am not one of them. I never connected to the PHP environment like I did with .NET or Javascript – PHP always felt like an afterthought to me.
So, for the remaining projects I decided to do some research and find out two things:
- What platform would enable me to concentrate on solving the client request without becoming mired in the technology
- What platform would allow me to have fun and be productive as well.
I know that having fun seems counter-intuitive at work but that is the main reason I have been programming for 24+ years: I am having fun doing it!
Platforms/Stacks I looked at
Rails
Consistent development cycle, Ruby just works and data support is great but, but…developing just felt like I was jumping through about 5 hoops everytime I had to change the database or change the code or the view, etc. Rails also is heavier than I wanted to go in terms of a framework and learning curve.
Node.js
Seems like a fast and clean platform and Javascript is one of the main languages I code. I tried working with it on a few test projects but deadlines were looming and it just did not click with me at the time. I will go back and get firmer grounding in this for a more private project but not for a client.
.NET
My bread and butter for many years. I know Asp.Net and variants (mvc) but all of my client run a Linux platform and I did not want to jump on Mono. Poor excuse maybe but it was just not there for me.
Sinatra
I have written a few backend web api’s with Sinatra and really liked it and I really enjoyed the fast code / test cycle. Ruby is great and Sinatra works great with mysql.
In addition, working with the Slim templating language initially, was a lot like coding in Python with the strict indentation rules but once I got used it fast and extremely productive especially after finding the Html2Slim web app – what a lifesaver!
So, enough lame explanation why I chose Sinatra, let’s get to my local machine setup (links at the end):
Local Machine Setup and Components
Platform
- Macbook Pro
- 16 GB Ram
- OSX 10.9.5
- Ruby 2.x (RVM)
- MySql 5.x
Project Components
This is a list of the core Gemfiles I use:
- gem ‘sinatra’
- gem ‘sinatra-contrib’
- gem ‘slim’
- gem ‘thin’
- gem ‘mysql2’
- gem ‘sequel’
You can manage Ruby Gems manually or simply update your Gemfile and run ‘Bundle Install’ and/or ‘Bundle Update’ from your Gemfile directory.
Bundle is great and installs/updates everything right out of the box. I am running all of the latest gemfile versions and update at least weekly using the ‘bundle update’ command.
Updating all the time works in my favor for setting up new servers since I just run ‘bundle install’ and done with no worries about version issues. So far, I have not had many issues with different versions of gems not working. There were a few in the beginning but I am updated past those issues. I think I would rather work through issues and maybe help fix a bug or two instead of keep using old versions.
Some notes about my choices in terms of gem components (just the main ones listed above):
slim
I tried using erb as it was built in but I quickly got tired of typing all the html not to mention the clumsy tag structures for data. Tried haml and it was a little better and then I tried slim and initially I hated it. The indent rules and some of the quirks where plain text are concerned drove me crazy until I got used to it but once I did it was fast coding and from my perspective it is a great performer. (actively maintained)
thin
This great little app server came along for the ride since it was recommended in the Sinatra docs I just went with it. I have to say I have never been disappointed, so far.. I know about Unicorn, Phusion Passenger and Cougar but my time important (unless someone can tell me how to get it back once used?) and Thin fit the bill in terms of ease of setup and once it runs it just keeps running (either standalone or on reverse proxy with Apache).
mysql2
Just an adaptor for mysql. This was recommended for use with the ORM I am currently using. Install and forget.
sequel
Originally, I started with DataMapper and I had so many issues with it in terms of getting things to just work the way I wanted them to I almost dumped Sinatra altogether. Then I found Sequel and wow! You can go two ways with Sequel:
- Manage your own db structure and simply have Sequel map that structure to code for you – it’s seamless.
- Define your db structure with Sequel in your code and let it manage your database for you in terms of generating your database and maintaing the structure.
Option #2 sounds cool and everything but I manage a dev/test environment and a separate prod environment so I wanted to be in control of what happens to the database and when so I went with Option #1.
Here is what I do: I have a master sql file for the db structure which I update and run against the database. Sequel, how it is setup, reads that structure, builds the objects on the fly and I am good to go – what could be easier? BTW: This is not a strictly technical explanation but I think repeating docs in an article is a waste of time.
So, that’s it for my main components. I use a bunch of others but they are for specific requirements which have nothing to do with actually getting up and running with Sinatra and MySql.
Rack
Just a quick word about Rack. Sinatra is based on Rack and it is well worth you while to get to know Rack at least a little bit. Interesting bit of Ruby Webserver Interface code which seems to run many Ruby based frameworks.
My Development Environment
A couple of years ago I used Jetbrain’s RubyMine for a project and really liked it. However, I have been using SublimeText editor for quite a while and even though I am not some SublimeText guru I get around so I tried it as my main dev environment. Sublime runs Ruby very well and was fairly easy to setup but after a while I wanted a more Ruby/Rack focused dev tool so I went back to RubyMine and have never looked back.
I know there are some out there who just sneered at me for jumping on to RubyMine instead of being a loyalist Sublimer or whatever.. oh well.
RubyMine has a great Ruby editor with some fantastic code completion which was far better and faster than Sublime’s (perhaps it was me but Sublime always seemed to offer everything instead of contextual suggestions). RubyMine also has great Javascript and Slim support. There are other things that make it worthwhile such as filewatcher’s for .css/.js compression as well as a great set of runtime configurations that allow me to run several different runtime configurations at will.
Sure all of this is available with Sublime or some other editor/ide but this is what I run so there you have it.
If you want to check out one of the fun projects I wrote for a friend of mine head over here: http://12PACksports.com. Yeah, he’s a huge PAC 12 fan and wanted to get some of his thoughts down out there so I built him a bloglike app. You may be thinking: “Uh, are you dense? What about Blogger, Movable Type, WordPress, etc?” and yep, you would be right: They are out there. He wanted to design his own app and have some custom backend features that you can’t see and besides we managed to get 90% of what he wanted for 100% of his $ budget so I am happy with that! He’s just starting this endeavor but it has RSS if you want to keep up (RSS: another cool 30 minute exercise generating RSS using the built in Ruby lib and Sequel – take that .NET!).
Questions and/or comments are welcome and if I can help I will give it a shot. I would like to expand on this article but that depends on what comes up comment-wise.
Links
Sinatra Framework – Web Development DSL
Sinatra Contrib – Some Common Sinatra Extensions
Padrino – Web Framework based on Sinatra
Rack – Ruby Web Interface
Html2Slim – Html to Slim converter
Bundler – Manage your Gems
Slim – Template Language
Thin – Rack Application Server
Sequel – Ruby Database Toolkit
Ruby Toolbox – My goto site for all things Ruby
Sublime Text Editor – The only text editor I use now
JetBrains RubyMine – Excellent Ruby, Javascript, CSS IDE