dynamic app routing for Bottle.py based on hostname / domain


I want ONE bottle project to display different pages for:

These should be separate Bottle apps.

There's an ugly hack from the official maillist, I think I can make it better.

The secret is to create a customized WSGI app:

from fnmatch import fnmatch
from bottle import Bottle, default_app, run

foo = Bottle()
foo.hostnames = ['foo.est.im']
def hello():
    return 'hello!'

bar = Bottle()
bar.hostnames = ['bar.est.im']
def hi():
    return 'hi~'

def index():
    print 'hello world'

def application(environ, start_response):
    for app in [foo, bar]:
        hostnames = getattr(app, 'hostnames', [])
        if filter(lambda x:fnmatch(hostname, x), hostnames):
            return app(environ, start_response)
    return default_app()(environ, start_response)

Then run this motherfucker:

from django.utils import autoreload
def dev_server():
    run(application, host='', port=8002, debug=True)