fix running via pyinstaller bin

This commit is contained in:
Izalia Mae 2024-02-16 21:05:12 -05:00
parent c3b4d9ca98
commit 1eb93ab71a
3 changed files with 49 additions and 4 deletions

View file

@ -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=[],

View file

@ -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')

View file

@ -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()