2012-10-10 00:16:25 +02:00
|
|
|
#!/usr/bin/env python
|
2012-11-08 23:23:25 +01:00
|
|
|
import optparse
|
2012-10-10 00:16:25 +02:00
|
|
|
import subprocess
|
|
|
|
import os
|
|
|
|
from os import path
|
|
|
|
|
|
|
|
from twisted.internet import reactor
|
|
|
|
from twisted.web import proxy, server, resource
|
|
|
|
|
2012-11-08 23:23:25 +01:00
|
|
|
parser = optparse.OptionParser(r"""
|
|
|
|
|
2012-10-10 00:16:25 +02:00
|
|
|
Starts the app listening on localhost, for local development.
|
|
|
|
|
|
|
|
This script launches the Django and Tornado servers, then runs a reverse proxy
|
|
|
|
which serves to both of them. After it's all up and running, browse to
|
|
|
|
|
|
|
|
http://localhost:9991/
|
|
|
|
|
|
|
|
Note that, while runserver and runtornado have the usual auto-restarting
|
|
|
|
behavior, the reverse proxy itself does *not* automatically restart on changes
|
|
|
|
to this file.
|
2012-11-08 23:23:25 +01:00
|
|
|
""")
|
|
|
|
|
|
|
|
parser.add_option('--test',
|
|
|
|
action='store_true', dest='test',
|
|
|
|
help='Use the testing database and ports')
|
|
|
|
|
|
|
|
(options, args) = parser.parse_args()
|
|
|
|
|
|
|
|
base_port = 9991
|
|
|
|
manage_args = ''
|
|
|
|
if options.test:
|
|
|
|
base_port = 9981
|
|
|
|
manage_args = '--settings=humbug.test_settings'
|
2012-10-10 00:16:25 +02:00
|
|
|
|
2012-11-08 23:23:25 +01:00
|
|
|
proxy_port = base_port
|
|
|
|
django_port = base_port+1
|
|
|
|
tornado_port = base_port+2
|
2012-10-10 00:16:25 +02:00
|
|
|
proxy_host = 'localhost:%d' % (proxy_port,)
|
|
|
|
|
|
|
|
os.chdir(path.join(path.dirname(__file__), '..'))
|
|
|
|
|
|
|
|
procs = []
|
2012-11-08 23:23:25 +01:00
|
|
|
for cmd in ['python manage.py runserver %s localhost:%d' % (manage_args, django_port),
|
|
|
|
'python manage.py runtornado %s localhost:%d' % (manage_args, tornado_port)]:
|
2012-10-10 00:16:25 +02:00
|
|
|
procs.append(subprocess.Popen(cmd, shell=True))
|
|
|
|
|
|
|
|
class Resource(resource.Resource):
|
|
|
|
def getChild(self, name, request):
|
|
|
|
request.requestHeaders.setRawHeaders('X-Forwarded-Host', [proxy_host])
|
|
|
|
|
2012-10-17 00:54:08 +02:00
|
|
|
if request.uri in ['/json/get_updates', '/api/v1/get_messages']:
|
2012-11-08 23:23:25 +01:00
|
|
|
return proxy.ReverseProxyResource('localhost', tornado_port, '/'+name)
|
2012-10-10 00:16:25 +02:00
|
|
|
|
2012-11-08 23:23:25 +01:00
|
|
|
return proxy.ReverseProxyResource('localhost', django_port, '/'+name)
|
2012-10-10 00:16:25 +02:00
|
|
|
|
|
|
|
reactor.listenTCP(proxy_port, server.Site(Resource()), interface='127.0.0.1')
|
|
|
|
|
|
|
|
try:
|
|
|
|
reactor.run()
|
|
|
|
finally:
|
|
|
|
for proc in procs:
|
|
|
|
proc.terminate()
|