Sunday, May 23, 2010

Easy peasy rails/sinatra development on Windows

All are complaining about how difficult it is to set up ruby development environment on Windows. I felt so lucky that I haven't had to touch it for the last 5 years (my iPod Touch music library never got off though).
However, this mainstream os caught me again few days ago when I needed to test UI of my own project in Internet Explorer (good old days of working in the team when you could just ask your fellow programmer to see how it looks in windowz).
First I tried to follow DevChix turorial referred by Oren from heroku. But it didn't work for me (shotgun in particular). So I fought that I will give a try to cygwin again. I had been using it before I migrated all my PC and laptop to ubuntu and I really enjoyed it. There was bash, vim, wget, even apache (those days 1.3). All the good stuff in the evil empire.
So If you want quick ruby on your windows follow this steps:
* run http://www.cygwin.com/setup.exe
* at the end of the installation wizard you are presented with list of software. Choose whatever you want - you can always run installer again and install more software.
* to install git you just select it. It is as simple as that. No difficult choices as described on DevChix wiki.
* If you like developing with sqlite it cannot be simpler. Guess what? Just select it in the installer and you are ready to 'gem install sqlite3-ruby'. Surprised?

The only flow is ruby --version == 1.8.7, but if you really want 1.9 you can always install rvm I quess.

Friday, January 15, 2010

Rack::DomainRedirect v0.1.1 released

Just a quick announcement about Rack::DomainRedirect v0.1.1 release.

Rack::DomainRedirect is a tiny rack middleware for redirecting to the configurable domain. If user request’s the service from domain other than that configured it redirects to the first domain on the configuration list. If no domain is configured it does noting and passes requests to another application in the chain.

Read more about it on Rack::DomainRedirect project page where you can read more, see examples, download the code and get instructions on how to install it using ruby gems.

Wednesday, January 6, 2010

Redirecting in rack

I had my CV on apache once, with few domains such as misza.co.uk, www.misza.co.uk etc pointing at it which were all redirected by apache's mod_redirect to the cv.misza.co.uk with a few simple lines:


# Rewrite rules
RewriteEngine on
RewriteCond %{HTTP_HOST} !^cv\.misza\.co\.uk [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/(.*) http://cv.misza.co.uk/$1 [L,R=301]


Recently I moved it to heroku.com and I didn't have any control over the web server any more.
Fortunately there is Rack - interface between web server and my small sinatra cv application. It looked like the best place to put redirection logic. Here is a piece of rack middleware doing it.


module Rack
# Redirects to correct domain
class RedirectToDomain

HOST = 'example.com'

def initialize(app)
@app = app
end

def call(env)
req = Rack::Request.new(env)

# localhost for local development
if req.host == HOST or req.host == 'localhost'
@app.call(env)
else
res = Rack::Response.new
res.redirect("http://#{HOST}/")
res.finish
end
end
end
end


To use it with your application require the file above and add


use Rack::RedirectToDomain


in the same way as you would use any other rack middleware. And then the magic happens and you have redirection working without polluting your app with this low level redirection stuff.

You can clone the code on github.