Init server

This commit is contained in:
Jake Howard 2018-12-12 20:03:45 +00:00
parent 96c52a0328
commit 81789ec29e
Signed by: jake
GPG key ID: 57AFB45680EDD477
4 changed files with 39 additions and 15 deletions

View file

@ -19,8 +19,12 @@ def cli():
def start(ctx, no_fork): def start(ctx, no_fork):
if worker.is_running(): if worker.is_running():
ctx.fail("Worker already running") ctx.fail("Worker already running")
if no_fork:
worker.run()
else:
daemon = daemonize.Daemonize( daemon = daemonize.Daemonize(
"catfish", worker.PID_FILE, worker.main, foreground=no_fork "catfish", worker.PID_FILE, worker.run, foreground=no_fork
) )
try: try:
# HACK: Temporary hack until https://github.com/thesharp/daemonize/pull/70 is solved # HACK: Temporary hack until https://github.com/thesharp/daemonize/pull/70 is solved

View file

@ -3,6 +3,9 @@ import os
import tempfile import tempfile
import psutil import psutil
from catfish.utils.processes import terminate_processes 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") PID_FILE = os.path.join(tempfile.gettempdir(), "catfish.pid")
@ -33,6 +36,9 @@ def stop_worker():
terminate_processes([get_running_process()]) terminate_processes([get_running_process()])
def main(): async def run_worker():
while True: loop = asyncio.get_running_loop()
time.sleep(1) await asyncio.gather(run_server(loop))
run = partial(asyncio.run, run_worker())

14
catfish/worker/router.py Normal file
View file

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

View file

@ -11,7 +11,7 @@ setup(
include_package_data=True, include_package_data=True,
zip_safe=False, zip_safe=False,
pathon_requires=">=3.6", pathon_requires=">=3.6",
install_requires=["click", "daemonize", "psutil"], install_requires=["click", "daemonize", "psutil", "aiohttp"],
entry_points=""" entry_points="""
[console_scripts] [console_scripts]
ctf=catfish.__main__:cli ctf=catfish.__main__:cli