From cd5689186b97667d5894003cc793ee10422e7931 Mon Sep 17 00:00:00 2001 From: iou1name Date: Sun, 1 Sep 2019 14:44:54 -0400 Subject: [PATCH] rewrote web app using aiohttp --- .gitignore | 1 + README.md | 4 ++-- aberrant.py | 41 ++++++++++++++++++++--------------------- config.py | 5 +++++ rtorrent.py | 2 +- templates/index.html | 6 +++--- 6 files changed, 32 insertions(+), 27 deletions(-) create mode 100644 config.py diff --git a/.gitignore b/.gitignore index c56bb68..e1d812b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ __pycache__/ *.swp *.swo +sync.sh diff --git a/README.md b/README.md index dba9dc2..539b75c 100644 --- a/README.md +++ b/README.md @@ -3,11 +3,11 @@ It's a WebGUI for rTorrent. ## Requirements Python 3.7+ -Python packages: `flask gunicorn` +Python packages: `gunicorn aiohttp aiohttp_jinja2` ## Install 1. Get on the floor 2. Walk the dinosaur ## Usage -`gunicorn -b localhost:5250 -e SCRIPT_NAME=/aberrant aberrant:app` +`gunicorn aberrant:app --bind localhost:5250 --worker-class aiohttp.GunicornWebWorker` diff --git a/aberrant.py b/aberrant.py index 5565d45..3b24bcd 100644 --- a/aberrant.py +++ b/aberrant.py @@ -2,36 +2,35 @@ """ The primary module for serving the Aberrant application. """ -import json -import signal - -from flask import Flask, render_template, send_from_directory +import jinja2 +import aiohttp_jinja2 +from aiohttp import web, WSMsgType +from aiohttp_jinja2 import render_template +import config import rtorrent -app = Flask(__name__) -app.jinja_env.trim_blocks = True -app.jinja_env.lstrip_blocks = True -app.jinja_env.undefined = "StrictUndefined" +app = web.Application() +app.on_shutdown.append(rtorrent.stop_watch) +aiohttp_jinja2.setup(app, loader=jinja2.FileSystemLoader('templates')) rtorrent.init() -signal.signal(signal.SIGINT, rtorrent.stop_watch) -@app.route("/") -def index(): +routes = web.RouteTableDef() + +@routes.get(config.prefix + "/", name='index') +def index(request): """The index page.""" torrents = rtorrent.get_active() tracker_stats = rtorrent.get_stats() - return render_template("index.html", **locals()) + return render_template("index.html", request, locals()) -@app.route("/static/") -def send_static(path): - """Sends static files.""" - return send_from_directory("static", path) - -@app.route("/get_active_torrents") -def get_active_torrents(): +@routes.get(config.prefix + "/get_active_torrents", name='active-torrents') +def get_active_torrents(request): """Returns all active torrents formatted as JSON.""" - return json.dumps([vars(t) for t in rtorrent.get_active()]) + data = [vars(t) for t in rtorrent.get_active()] + return web.json_response(data) + +app.router.add_routes(routes) if __name__ == "__main__": - app.run(host='0.0.0.0', port=5250) + aiohttp.web.run_app(app, host='0.0.0.0', port=5250) diff --git a/config.py b/config.py new file mode 100644 index 0000000..ca1e2cb --- /dev/null +++ b/config.py @@ -0,0 +1,5 @@ +#!/usr/bin/env python3 +""" +Configuation settings for Aberrant. +""" +prefix = '/aberrant' diff --git a/rtorrent.py b/rtorrent.py index 52a2f71..ca75640 100644 --- a/rtorrent.py +++ b/rtorrent.py @@ -153,7 +153,7 @@ def init(): WATCH_HANDLE = Watch() WATCH_HANDLE.start() -def stop_watch(*args, **kwargs): +async def stop_watch(*args, **kwargs): """Stops the watch thread.""" global WATCH_HANDLE WATCH_HANDLE.stop() diff --git a/templates/index.html b/templates/index.html index a4fbff2..c30a94c 100644 --- a/templates/index.html +++ b/templates/index.html @@ -2,10 +2,10 @@ Aberrant - - + +