add VOICE_WEBSOCKET_URL to configs

- gateway: remove implicit /ws path, leave it up to configs
 - run: fix start_websocket
This commit is contained in:
Luna 2019-02-28 23:12:19 -03:00
parent f3bc65302d
commit ca5386d3ce
6 changed files with 25 additions and 12 deletions

View File

@ -38,6 +38,7 @@ class Config:
# will hit the websocket. # will hit the websocket.
# e.g 'gateway.example.com' for reverse proxies. # e.g 'gateway.example.com' for reverse proxies.
WEBSOCKET_URL = 'localhost:5001' WEBSOCKET_URL = 'localhost:5001'
VOICE_WEBSOCKET_URL = 'localhost:5002'
# Where to host the websocket? # Where to host the websocket?
# (a local address the server will bind to) # (a local address the server will bind to)

View File

@ -46,6 +46,7 @@ class Config:
# will hit the websocket. # will hit the websocket.
# e.g 'gateway.example.com' for reverse proxies. # e.g 'gateway.example.com' for reverse proxies.
WEBSOCKET_URL = 'localhost:5001' WEBSOCKET_URL = 'localhost:5001'
VOICE_WEBSOCKET_URL = 'localhost:5003'
#: Where to host the websocket? #: Where to host the websocket?
# (a local address the server will bind to) # (a local address the server will bind to)

View File

@ -29,7 +29,7 @@ bp = Blueprint('gateway', __name__)
def get_gw(): def get_gw():
"""Get the gateway's web""" """Get the gateway's web"""
proto = 'wss://' if app.config['IS_SSL'] else 'ws://' 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') @bp.route('/gateway')

View File

@ -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 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 """Main handler to instantiate a VoiceWebsocket
with the given url.""" with the given url."""
args = urllib.parse.parse_qs( args = urllib.parse.parse_qs(

9
run.py
View File

@ -24,7 +24,7 @@ import asyncpg
import logbook import logbook
import logging import logging
import websockets import websockets
from quart import Quart, g, jsonify, request from quart import Quart, jsonify, request
from logbook import StreamHandler, Logger from logbook import StreamHandler, Logger
from logbook.compat import redirect_logging from logbook.compat import redirect_logging
from aiohttp import ClientSession from aiohttp import ClientSession
@ -299,7 +299,6 @@ async def post_app_start(app_):
def start_websocket(host, port, ws_handler) -> asyncio.Future: def start_websocket(host, port, ws_handler) -> asyncio.Future:
"""Start a websocket. Returns the websocket 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}') log.info(f'starting websocket at {host} {port}')
async def _wrapper(ws, url): async def _wrapper(ws, url):
@ -315,12 +314,12 @@ async def app_before_serving():
log.info('opening db') log.info('opening db')
await init_app_db(app) await init_app_db(app)
g.app = app loop = asyncio.get_event_loop()
g.loop = asyncio.get_event_loop()
app.session = ClientSession() app.session = ClientSession()
init_app_managers(app) init_app_managers(app)
await post_app_start(app)
# start gateway websocket and voice websocket # start gateway websocket and voice websocket
ws_fut = start_websocket( ws_fut = start_websocket(
@ -333,8 +332,6 @@ async def app_before_serving():
voice_websocket_handler voice_websocket_handler
) )
await post_app_start(app)
await ws_fut await ws_fut
await vws_fut await vws_fut

View File

@ -21,6 +21,7 @@ import asyncio
import sys import sys
import os import os
import socket
import pytest import pytest
# this is very hacky. # this is very hacky.
@ -28,16 +29,29 @@ sys.path.append(os.getcwd())
from run import app as main_app, set_blueprints 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') @pytest.fixture(name='app')
def _test_app(unused_tcp_port, event_loop): def _test_app(event_loop):
set_blueprints(main_app) set_blueprints(main_app)
main_app.config['_testing'] = True main_app.config['_testing'] = True
# reassign an unused tcp port for websockets # reassign an unused tcp port for websockets
# since the config might give a used one. # since the config might give a used one.
main_app.config['WS_PORT'] = unused_tcp_port ws_port, vws_port = _unused_port(), _unused_port()
main_app.config['WEBSOCKET_URL'] = f'localhost:{unused_tcp_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 # make sure we're calling the before_serving hooks
event_loop.run_until_complete(main_app.startup()) event_loop.run_until_complete(main_app.startup())