From 1eb93ab71a3922ab5ec18ffdb5f62d5d5c08aa99 Mon Sep 17 00:00:00 2001 From: Izalia Mae Date: Fri, 16 Feb 2024 21:05:12 -0500 Subject: [PATCH] fix running via pyinstaller bin --- relay.spec | 11 +++++++++-- relay/manage.py | 40 ++++++++++++++++++++++++++++++++++++++++ relay/views/api.py | 2 -- 3 files changed, 49 insertions(+), 4 deletions(-) diff --git a/relay.spec b/relay.spec index 8510cd8..8f84331 100644 --- a/relay.spec +++ b/relay.spec @@ -1,7 +1,10 @@ # -*- mode: python ; coding: utf-8 -*- +import importlib +from pathlib import Path block_cipher = None +aiohttp_swagger_path = Path(importlib.import_module('aiohttp_swagger').__file__).parent a = Analysis( @@ -9,9 +12,13 @@ a = Analysis( pathex=[], binaries=[], datas=[ - ('relay/data', 'relay/data') + ('relay/data', 'relay/data'), + (aiohttp_swagger_path, 'aiohttp_swagger') + ], + hiddenimports=[ + 'gunicorn', + 'gunicorn.glogging' ], - hiddenimports=[], hookspath=[], hooksconfig={}, runtime_hooks=[], diff --git a/relay/manage.py b/relay/manage.py index e8aab1b..6609b7c 100644 --- a/relay/manage.py +++ b/relay/manage.py @@ -4,9 +4,12 @@ import Crypto import asyncio import click import platform +import subprocess +import sys import typing from aputils.signer import Signer +from gunicorn.app.wsgiapp import WSGIApplication from pathlib import Path from shutil import copyfile from urllib.parse import urlparse @@ -234,9 +237,21 @@ def cli_run(ctx: click.Context, dev: bool = False) -> None: click.echo(pip_command) return + if getattr(sys, 'frozen', False): + subprocess.run([sys.executable, 'run-gunicorn'], check = False) + return + ctx.obj.run(dev) +@cli.command('run-gunicorn') +@click.pass_context +def cli_run_gunicorn(ctx: click.Context) -> None: + runner = GunicornRunner(ctx.obj) + runner.run() + + + @cli.command('convert') @click.option('--old-config', '-o', help = 'Path to the config file to convert from') @click.pass_context @@ -903,6 +918,31 @@ def cli_whitelist_import(ctx: click.Context) -> None: click.echo('Imported whitelist from inboxes') +class GunicornRunner(WSGIApplication): + def __init__(self, app: Application): + self.app = app + self.app_uri = 'relay.application:main_gunicorn' + self.options = { + 'bind': f'{app.config.listen}:{app.config.port}', + 'worker_class': 'aiohttp.GunicornWebWorker', + 'workers': app.config.workers, + 'raw_env': f'CONFIG_FILE={app.config.path}' + } + + WSGIApplication.__init__(self) + + + def load_config(self): + for key, value in self.options.items(): + self.cfg.set(key, value) + + + def run(self): + logging.info('Starting webserver for %s', self.app.config.domain) + WSGIApplication.run(self) + + + def main() -> None: # pylint: disable=no-value-for-parameter cli(prog_name='relay') diff --git a/relay/views/api.py b/relay/views/api.py index 7a49abc..84774c7 100644 --- a/relay/views/api.py +++ b/relay/views/api.py @@ -42,8 +42,6 @@ def check_api_path(method: str, path: str) -> bool: @web.middleware async def handle_api_path(request: web.Request, handler: Coroutine) -> web.Response: - print("Authorization:", request.headers.get('Authorization')) - try: request['token'] = request.headers['Authorization'].replace('Bearer', '').strip()