Project

hostrich

0.0
No commit activity in last 3 years
No release in over 3 years
Hostrich is a Rack middleware that eases multi-domain web app development.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.3
>= 0
>= 0

Runtime

>= 0
 Project Readme

Hostrich

Hostrich is a Rack middleware that eases multi-domain web app development.

Usage

Add the middleware at the top of your development stack, passing it the domain(s) your app is using:

# Gemfile
gem 'hostrich', group: :development
# config.ru

# if using Rack::Deflater, put it here
if ENV['RACK_ENV'] == 'development'
  use Hostrich, ['somedomain.com', 'otherdoma.in']
end
# ... any other middleware ...

run Your::RackApp

Database-dynamic hosts

If your app serves pages on hosts that depend on models, say a Website model with a custom_domain column, you can append hosts to the Hostrich.hosts array after your app intialization:

# config/environments/development.rb

config.after_initialize do
  Hostrich.hosts += Website.pluck(:custom_domain).compact
end

Rationale

Hostrich tricks your development environment into thinking it is serving your application from your production host (example.com) instead of your usual development host (example.dev).

Thus, your application doesn’t have to know about any dev-prod hosts mapping. This makes your code simpler and less prone to errors.

To make this possible, you must access your local app at http://example.com.dev or the like, where a suffix is added to the full production domain. This way Hostrich can extract .dev from the host and append it everywhere example.com is output in your response bodies and headers.

xip.io is Hostrich’s best friend. For complex multi-domain web apps like ours at Medalist, it’s a must. Our app responds to medali.st, manage.medali.st, *.mli.st and even anydomain.com because some of our users have custom domains. xip.io allows us to access our local app at:

  • medali.st.127.0.0.1.xip.io
  • manage.medali.st.127.0.0.1.xip.io
  • mikaelkingsbury.mli.st.127.0.0.1.xip.io
  • mikaelkingsbury.ca.127.0.0.1.xip.io

And our application code only knows about production hosts:

# config/routes.rb

Medalist::Application.routes.draw do
  # http://medali.st/
  namespace :public, host: 'medali.st' do
    # ...
  end

  # http://manage.medali.st/
  namespace :manage, host: 'manage.medali.st' do
    # ...
  end

  # http://usersite.mli.st/
  # http://usersite.com/
  namespace :usersite do
    # ...
  end
end

TODO

  • This README isn’t that great.

© 2014 Rafaël Blais Masson. Hostrich is released under the MIT license.