Commit ca5386d3 authored by Luna's avatar Luna 😻

add VOICE_WEBSOCKET_URL to configs

 - gateway: remove implicit /ws path, leave it up to configs
 - run: fix start_websocket
parent f3bc6530
......@@ -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)
......
......@@ -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)
......
......@@ -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')
......
......@@ -17,10 +17,10 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
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(
......
......@@ -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
......
......@@ -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())
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment