Hey, PAC man. Sup, subdomains !

By taylor luk on August 12, 2009 @ 11:56 PM

Updated: link to rake task app:sync

Today, i would like to discuss the idea of subdomain in web apps, then share some tips and code to allow you to development and enable subdomain in your rails app, easily.

Enabling subdomain for your user account can be a great feature for your web application, there has been lot of debate about whether this is a good idea.

The bad

I still remember reading this article "Subdomains + Development = Sucks" from couple years ago, it basically highlights a lot of legitimate downside when you want to use subdomain to differentiate user accounts for your web application.

  • SSL certificate for a Wildcard subdomain is way more expansive than a standard SSL cert for a domain.
  • As for development, it's painful to setup a local development environment (try to get local wildcard subdomain), difficulties with local host names
  • definitely more complex to develop and test.

Enough said, Subdomain is pain in the ass.

The good

Despite all that I love subdomains since users get greater isolation and DNS CNAME is one of the best thing happen to internet architecture.

  • technically they are actually separate website if you ask the trust worthy Firefox or even Google the search engine.
  • customer can point one of their domain name to a separate host, external system and services (ie: Amazon s3). Yet preserves their brand identity.
  • Browser allows cookies to be shared between root domain and a subdomain.

And My recipe

Today, I'd like to share couple goodies that i have discovered and some code i have developed to help you enable subdomains in your Rails application.

1. Convention

Use .local on your local environment and .tld for production, Main thing is stick to a pattern that you are comfortable with.

username.myawesomeapp.local
username.myawesomeapp.com

2. subdomain-fu

a rails plugin to help your application understand subdomains, includes nice addition to routing and helpers.

3. Introducing Rack::CustomDomain

It's a rack middleware that i have developed (get it here) to switch on custom domains for your application, So your customer can enter a CNAME entry to support their own domains

username.com  =>  username.myawesomeapp.com

Rack is a beautiful thing and it provides Layered Abstraction and transparent among each rack component, using this ruby class all the subdomain-fu features will still work as usually.

For example: taylorluk.siterest.com, www.taylorluk.com are effectively the same account

Setup it's easy, drop it to app/middlewares/custom_domain.rb and add following to your environment.rb or a environment specific config file.

config.load_paths += %W( #{RAILS_ROOT}/app/middlewares )
config.middleware.use "CustomDomain", ".myawesomeapp.com" 

4. "Proxy PAC"

Pacmen

editing /etc/hosts is waste of my time

This is the magic sauce if you ask me, I used to use a ruby gem "ghost" to add subdomains to local. It's easy to use but the problem is it's too much work. Why there isn't a easy solution to bring wildcard subdomain to your local workstation without installing a fr**king DNS server and luckily i find the solution in this article.

Proxy PAC is a javascript proxy configuration file that you can install to your browser

Save it /Users/USERNAME/.proxy.pac

function FindProxyForURL(url, host) {
  if (shExpMatch(host, "*.myawesomeapp.local")) {
    return "PROXY myawesomeapp.local";
  }
  return "DIRECT";
}
  • Firefox - Preferences/Advanced/Network/Connection Settings and enter the file name file:///Users/USERNAME/.proxy.pac
  • Safari - Preferences/Advanced/Proxy settings select Use a PAC file and enter file://localhost/Users/USERNAME/.proxy.pac

Congrats

Now you have got wildcard subdomain right on your local environment. All of these have helped me when I am building my side project "SiteRest". The development is a lot easier, now i use a rack task to synchronize data between local and production environment and bang accountname.myawesomeapp.local, I can diagnose problem with a specific account easily.

Enjoy !


Bookmark and Share

Please leave a comment, so we can kick start a conversion...

blog comments powered by Disqus