diff --git a/config.ci.py b/config.ci.py index 24e9224..04f1fdd 100644 --- a/config.ci.py +++ b/config.ci.py @@ -38,6 +38,7 @@ class Config: # will hit the websocket. # e.g 'gateway.example.com' for reverse proxies. WEBSOCKET_URL = 'localhost:5001' + VOICE_WEBSOCKET_URL = 'localhost:5002' # Where to host the websocket? # (a local address the server will bind to) diff --git a/config.example.py b/config.example.py index 532ae7b..ad6de74 100644 --- a/config.example.py +++ b/config.example.py @@ -46,6 +46,7 @@ class Config: # will hit the websocket. # e.g 'gateway.example.com' for reverse proxies. WEBSOCKET_URL = 'localhost:5001' + VOICE_WEBSOCKET_URL = 'localhost:5003' #: Where to host the websocket? # (a local address the server will bind to) diff --git a/litecord/blueprints/gateway.py b/litecord/blueprints/gateway.py index 562040b..05303f0 100644 --- a/litecord/blueprints/gateway.py +++ b/litecord/blueprints/gateway.py @@ -29,7 +29,7 @@ bp = Blueprint('gateway', __name__) def get_gw(): """Get the gateway's web""" proto = 'wss://' if app.config['IS_SSL'] else 'ws://' - return f'{proto}{app.config["WEBSOCKET_URL"]}/ws' + return f'{proto}{app.config["WEBSOCKET_URL"]}' @bp.route('/gateway') diff --git a/litecord/voice/websocket_starter.py b/litecord/voice/websocket_starter.py index 936c143..002835c 100644 --- a/litecord/voice/websocket_starter.py +++ b/litecord/voice/websocket_starter.py @@ -17,10 +17,10 @@ along with this program. If not, see . """ -import urllib.urlparse +import urllib.parse from litecord.voice.websocket import VoiceWebsocket -async def voice_websocket_handle(app, ws, url): +async def voice_websocket_handler(app, ws, url): """Main handler to instantiate a VoiceWebsocket with the given url.""" args = urllib.parse.parse_qs( diff --git a/run.py b/run.py index edc67fb..f034d67 100644 --- a/run.py +++ b/run.py @@ -24,7 +24,7 @@ import asyncpg import logbook import logging import websockets -from quart import Quart, g, jsonify, request +from quart import Quart, jsonify, request from logbook import StreamHandler, Logger from logbook.compat import redirect_logging from aiohttp import ClientSession @@ -299,7 +299,6 @@ async def post_app_start(app_): def start_websocket(host, port, ws_handler) -> asyncio.Future: """Start a websocket. Returns the websocket future""" - host, port = app.config['WS_HOST'], app.config['WS_PORT'] log.info(f'starting websocket at {host} {port}') async def _wrapper(ws, url): @@ -315,12 +314,12 @@ async def app_before_serving(): log.info('opening db') await init_app_db(app) - g.app = app - g.loop = asyncio.get_event_loop() + loop = asyncio.get_event_loop() app.session = ClientSession() init_app_managers(app) + await post_app_start(app) # start gateway websocket and voice websocket ws_fut = start_websocket( @@ -333,8 +332,6 @@ async def app_before_serving(): voice_websocket_handler ) - - await post_app_start(app) await ws_fut await vws_fut diff --git a/tests/conftest.py b/tests/conftest.py index d77c012..0750091 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -21,6 +21,7 @@ import asyncio import sys import os +import socket import pytest # this is very hacky. @@ -28,16 +29,29 @@ sys.path.append(os.getcwd()) from run import app as main_app, set_blueprints +# pytest-sanic's unused_tcp_port can't be called twice since +# pytest fixtures etc etc. +def _unused_port(): + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock: + sock.bind(('127.0.0.1', 0)) + return sock.getsockname()[1] + @pytest.fixture(name='app') -def _test_app(unused_tcp_port, event_loop): +def _test_app(event_loop): set_blueprints(main_app) main_app.config['_testing'] = True # reassign an unused tcp port for websockets # since the config might give a used one. - main_app.config['WS_PORT'] = unused_tcp_port - main_app.config['WEBSOCKET_URL'] = f'localhost:{unused_tcp_port}' + ws_port, vws_port = _unused_port(), _unused_port() + print(ws_port, vws_port) + + main_app.config['WS_PORT'] = ws_port + main_app.config['WEBSOCKET_URL'] = f'localhost:{ws_port}' + + main_app.config['VWS_PORT'] = vws_port + main_app.config['VOICE_WEBSOCKET_URL'] = f'localhost:{vws_port}' # make sure we're calling the before_serving hooks event_loop.run_until_complete(main_app.startup())