Skip to content

Commit d61d3ab

Browse files
author
Marco Paolini
committed
Make connections configurable
1 parent 31edae5 commit d61d3ab

File tree

4 files changed

+77
-5
lines changed

4 files changed

+77
-5
lines changed

pushpull/amqp/gateway.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ class Exchanger:
1212
ROLE_WS = 1
1313
ROLE_APP = 2
1414

15-
def __init__(self, name, role, client_id=0, **connection_params):
16-
self._conn_params = connection_params
15+
def __init__(self, name, role, client_id=0):
1716
if role not in [self.ROLE_WS, self.ROLE_APP]:
1817
raise ValueError('bad role {}'.format(role))
1918
self.role = role
@@ -22,7 +21,9 @@ def __init__(self, name, role, client_id=0, **connection_params):
2221

2322
async def __aenter__(self):
2423
logger.debug('connecting with role {}'.format(self.role))
25-
self._conn = await asynqp.connect(**self._conn_params)
24+
from .. import config
25+
params = config.get_amqp_conn_params()
26+
self._conn = await asynqp.connect(**params)
2627
self._chan = await self._conn.open_channel()
2728
app_routing_key = '{}.app'.format(self.name)
2829
app_exchange = await self._chan.declare_exchange(app_routing_key, 'fanout')

pushpull/cli/server.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66

77
@click.command()
88
def serve():
9+
from .. import config
910
logging.basicConfig(level=logging.DEBUG)
10-
aiohttp.web.main(['-H', 'localhost', '-P', '8080', 'pushpull.websocket.server:serve'])
11+
host, port = config.get_host_port()
12+
aiohttp.web.main(['-H', host, '-P', port, 'pushpull.websocket.server:serve'])
1113

1214

1315
if __name__ == '__main__':

pushpull/config.py

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import urllib.parse
2+
import os
3+
4+
try:
5+
from django.conf import settings
6+
except ImportError:
7+
WEBSOCKET_URL = os.environ.get('PUSHPULL_WEBSOCKET_URL', '')
8+
BROKER_URL = os.environ.get('PUSHPULL_BROKER_URL', '')
9+
else:
10+
WEBSOCKET_URL = settings.PUSHPULL_WEBSOCKET_URL
11+
BROKER_URL = settings.PUSHPULL_BROKER_URL
12+
13+
14+
def get_host_port():
15+
url = WEBSOCKET_URL or 'http://localhost:8080'
16+
url_chunks = urllib.parse.urlparse(url)
17+
host_port = url_chunks.netloc
18+
if ':' in host_port:
19+
host, port = host_port.split(':')
20+
else:
21+
host = host_port
22+
port = '8080'
23+
return host, port
24+
25+
26+
def get_url_path():
27+
path = '/pushpull'
28+
if WEBSOCKET_URL:
29+
url_chunks = urllib.parse.urlparse(WEBSOCKET_URL)
30+
path = url_chunks.path
31+
return path
32+
33+
34+
def get_amqp_conn_params():
35+
url = BROKER_URL or 'amqp://guest:guest@localhost:5672/'
36+
url_chunks = urllib.parse.urlparse(url)
37+
if '@' not in url_chunks.netloc:
38+
host_port = url_chunks.netloc
39+
username, password = None, None
40+
else:
41+
auth, host_port = url_chunks.netloc.split('@')
42+
if ':' in auth:
43+
username, password = auth.split(':')
44+
else:
45+
username, password = None, None
46+
if not username:
47+
username = 'guest'
48+
if not password:
49+
password = 'guest'
50+
if ':' in host_port:
51+
host, port = host_port.split(':')
52+
else:
53+
host = host_port
54+
port = None
55+
if not port:
56+
port = 5672
57+
else:
58+
port = int(port)
59+
path = url_chunks.path
60+
if not path:
61+
path = '/'
62+
return {
63+
'host': host,
64+
'port': port,
65+
'username': username,
66+
'password': password,
67+
'virtual_host': path,
68+
}

pushpull/websocket/server.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import aiohttp.web
22

33
from . import gateway
4+
from .. import config
45

56

67
def serve(argv):
78
app = aiohttp.web.Application()
8-
app.router.add_route('*', '/sock', gateway.websocket_rabbitmq_gateway)
9+
app.router.add_route('GET', config.get_url_path(), gateway.websocket_rabbitmq_gateway)
910
return app

0 commit comments

Comments
 (0)