From 81789ec29e660f444f768c5cec00faed67636d5a Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Wed, 12 Dec 2018 20:03:45 +0000 Subject: [PATCH] Init server --- catfish/__main__.py | 26 +++++++++++++++----------- catfish/worker/__init__.py | 12 +++++++++--- catfish/worker/router.py | 14 ++++++++++++++ setup.py | 2 +- 4 files changed, 39 insertions(+), 15 deletions(-) create mode 100644 catfish/worker/router.py diff --git a/catfish/__main__.py b/catfish/__main__.py index cd6725e..0af9aad 100644 --- a/catfish/__main__.py +++ b/catfish/__main__.py @@ -19,17 +19,21 @@ def cli(): def start(ctx, no_fork): if worker.is_running(): ctx.fail("Worker already running") - daemon = daemonize.Daemonize( - "catfish", worker.PID_FILE, worker.main, foreground=no_fork - ) - try: - # HACK: Temporary hack until https://github.com/thesharp/daemonize/pull/70 is solved - os._exit = sys.exit - daemon.start() - except SystemExit: - worker.wait_for_worker() - proc = worker.get_running_process() - click.echo("Worker started with pid {}".format(proc.pid)) + + if no_fork: + worker.run() + else: + daemon = daemonize.Daemonize( + "catfish", worker.PID_FILE, worker.run, foreground=no_fork + ) + try: + # HACK: Temporary hack until https://github.com/thesharp/daemonize/pull/70 is solved + os._exit = sys.exit + daemon.start() + except SystemExit: + worker.wait_for_worker() + proc = worker.get_running_process() + click.echo("Worker started with pid {}".format(proc.pid)) @cli.command() diff --git a/catfish/worker/__init__.py b/catfish/worker/__init__.py index 11eaa1b..01f2bcf 100644 --- a/catfish/worker/__init__.py +++ b/catfish/worker/__init__.py @@ -3,6 +3,9 @@ import os import tempfile import psutil from catfish.utils.processes import terminate_processes +import asyncio +from .router import run_server +from functools import partial PID_FILE = os.path.join(tempfile.gettempdir(), "catfish.pid") @@ -33,6 +36,9 @@ def stop_worker(): terminate_processes([get_running_process()]) -def main(): - while True: - time.sleep(1) +async def run_worker(): + loop = asyncio.get_running_loop() + await asyncio.gather(run_server(loop)) + + +run = partial(asyncio.run, run_worker()) diff --git a/catfish/worker/router.py b/catfish/worker/router.py new file mode 100644 index 0000000..d82162c --- /dev/null +++ b/catfish/worker/router.py @@ -0,0 +1,14 @@ +import click +from aiohttp import web + + +async def handle_request(request): + return web.json_response({}) + + +async def run_server(loop, port=8080): + click.echo("Starting server...") + aiohttp_server = web.Server(handle_request) + aio_server = await loop.create_server(aiohttp_server, "0.0.0.0", port) + click.echo("Server listening on port {}".format(port)) + await aio_server.serve_forever() diff --git a/setup.py b/setup.py index fdc08ef..2101e6f 100644 --- a/setup.py +++ b/setup.py @@ -11,7 +11,7 @@ setup( include_package_data=True, zip_safe=False, pathon_requires=">=3.6", - install_requires=["click", "daemonize", "psutil"], + install_requires=["click", "daemonize", "psutil", "aiohttp"], entry_points=""" [console_scripts] ctf=catfish.__main__:cli