diff --git a/grzegorz/__init__.py b/grzegorz/__init__.py index 3d52899..31478fd 100644 --- a/grzegorz/__init__.py +++ b/grzegorz/__init__.py @@ -1,5 +1,7 @@ -import asyncio +import asyncio, uvloop from sanic import Sanic +from sanic_openapi import swagger_blueprint, openapi_blueprint +from signal import signal, SIGINT from . import mpv from . import nyasync @@ -8,35 +10,38 @@ from . import api #global variable: mpv_control = None#mpv.MPVControl() -async def test(): - resp = await mpv_control.loadfile('grzegorz/grzegorz/res/logo.jpg') - #print(resp) +def make_sanic_app(host="0.0.0.0", port=8080): + global mpv_control -def main(host="0.0.0.0", port=8080, tasks:list = None): app = Sanic(__name__) app.blueprint(api.bp, url_prefix="/api") + app.blueprint(openapi_blueprint) + app.blueprint(swagger_blueprint) - #used to ensure sanic/uvloop creates its asyncio loop before MPVControl tries to use one itself + asyncio.set_event_loop(uvloop.new_event_loop()) + server_coro = app.create_server(host=host, port=port) + loop = asyncio.get_event_loop() + server_task = asyncio.ensure_future(server_coro) + signal(SIGINT, lambda s, f: loop.stop()) + + mpv_control = mpv.MPVControl() + app.config["mpv_control"] = mpv_control async def runMPVControl(): - global mpv_control - - mpv_control = mpv.MPVControl() - app.config["mpv_control"] = mpv_control try: await mpv_control.run() except Exception as e: print(e) - print("mpv is no longer running. Stopping Sanic...") app.stop() + asyncio.ensure_future(runMPVControl()) + + return loop, app - if not tasks: tasks = [] - tasks.insert(0, runMPVControl()) - +def main(tasks:list = []): + loop, app = make_sanic_app() for task in tasks: - app.add_task(task)#instead of ensure_future - - app.run(host=host, port=port) + asyncio.ensure_future(task) + loop.run_forever() if __name__ == '__main__': - main(tasks=[test()]) + main() diff --git a/grzegorz/api.py b/grzegorz/api.py index 22f3ded..fd45abe 100644 --- a/grzegorz/api.py +++ b/grzegorz/api.py @@ -1,5 +1,6 @@ -from sanic import Blueprint, response import asyncio +from sanic import Blueprint, response +from functools import wraps from . import mpv bp = Blueprint("grzegorz-api") @@ -8,6 +9,7 @@ bp = Blueprint("grzegorz-api") #route decorators: def response_json(func): + @wraps(func) async def newfunc(*args, **kwargs): try: mpv_control = args[0].app.config["mpv_control"] @@ -26,6 +28,7 @@ def response_json(func): }) return newfunc def response_text(func): + @wraps(func) async def newfunc(*args, **kwargs): body = await func(*args, **kwargs) return response.text(body) diff --git a/main.py b/main.py index 59cca91..33007a8 100755 --- a/main.py +++ b/main.py @@ -1,3 +1,11 @@ #!/usr/bin/env python3 -from grzegorz import main, test -main(tasks=[test()]) +import asyncio +import grzegorz + +async def grzegorz_splash(): + resp = await grzegorz.mpv_control.loadfile('grzegorz/grzegorz/res/logo.jpg') + #print(resp) + +loop, app = grzegorz.make_sanic_app() +asyncio.ensure_future(grzegorz_splash()) +loop.run_forever() diff --git a/requirements.txt b/requirements.txt index 5c1e7eb..9bd3d94 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ mpv==0.1 youtube-dl sanic==0.7.0 +sanic-openapi==0.4.0 diff --git a/grzegorz/res/logo.jpg b/res/logo.jpg similarity index 100% rename from grzegorz/res/logo.jpg rename to res/logo.jpg diff --git a/grzegorz/res/pvv logo.svg b/res/pvv logo.svg similarity index 100% rename from grzegorz/res/pvv logo.svg rename to res/pvv logo.svg