dynamic app routing for Bottle.py based on hostname / domain
Posted | archive
Problem:
I want ONE bottle project to display different pages for:
- http://foo.est.im
- http://bar.est.im
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']
@foo.route('/')
def hello():
return 'hello!'
bar = Bottle()
bar.hostnames = ['bar.est.im']
@bar.route('/')
def hi():
return 'hi~'
@route('/')
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='0.0.0.0', port=8002, debug=True)
autoreload.main(dev_server)
Comments