subdomain dispatcher
This commit is contained in:
parent
2906b79dfc
commit
1a08aade00
8 changed files with 157 additions and 129 deletions
30
app.py
30
app.py
|
@ -1,35 +1,9 @@
|
|||
from gevent import monkey, pywsgi
|
||||
monkey.patch_all()
|
||||
from realms import create_app, config
|
||||
from threading import Lock
|
||||
from realms import config, init_db, make_app, SubdomainDispatcher
|
||||
|
||||
class SubdomainDispatcher(object):
|
||||
|
||||
def __init__(self, domain, create_app):
|
||||
self.domain = domain
|
||||
self.create_app = create_app
|
||||
self.lock = Lock()
|
||||
self.instances = {}
|
||||
|
||||
def get_application(self, host):
|
||||
host = host.split(':')[0]
|
||||
assert host.endswith(self.domain), 'Configuration error'
|
||||
subdomain = host[:-len(self.domain)].rstrip('.')
|
||||
with self.lock:
|
||||
app = self.instances.get(subdomain)
|
||||
if app is None:
|
||||
app = self.create_app(subdomain)
|
||||
self.instances[subdomain] = app
|
||||
return app
|
||||
|
||||
def __call__(self, environ, start_response):
|
||||
app = self.get_application(environ['HTTP_HOST'])
|
||||
return app(environ, start_response)
|
||||
|
||||
|
||||
def make_app(subdomain):
|
||||
return create_app(subdomain)
|
||||
|
||||
if __name__ == '__main__':
|
||||
init_db(config.db['dbname'])
|
||||
app = SubdomainDispatcher(config.domain, make_app)
|
||||
pywsgi.WSGIServer(('', config.port), app).serve_forever()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue