rewrote web app using aiohttp

This commit is contained in:
iou1name 2019-09-01 14:44:54 -04:00
parent d570a0c1ae
commit cd5689186b
6 changed files with 32 additions and 27 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
__pycache__/ __pycache__/
*.swp *.swp
*.swo *.swo
sync.sh

View File

@ -3,11 +3,11 @@ It's a WebGUI for rTorrent.
## Requirements ## Requirements
Python 3.7+ Python 3.7+
Python packages: `flask gunicorn` Python packages: `gunicorn aiohttp aiohttp_jinja2`
## Install ## Install
1. Get on the floor 1. Get on the floor
2. Walk the dinosaur 2. Walk the dinosaur
## Usage ## Usage
`gunicorn -b localhost:5250 -e SCRIPT_NAME=/aberrant aberrant:app` `gunicorn aberrant:app --bind localhost:5250 --worker-class aiohttp.GunicornWebWorker`

View File

@ -2,36 +2,35 @@
""" """
The primary module for serving the Aberrant application. The primary module for serving the Aberrant application.
""" """
import json import jinja2
import signal import aiohttp_jinja2
from aiohttp import web, WSMsgType
from flask import Flask, render_template, send_from_directory from aiohttp_jinja2 import render_template
import config
import rtorrent import rtorrent
app = Flask(__name__) app = web.Application()
app.jinja_env.trim_blocks = True app.on_shutdown.append(rtorrent.stop_watch)
app.jinja_env.lstrip_blocks = True aiohttp_jinja2.setup(app, loader=jinja2.FileSystemLoader('templates'))
app.jinja_env.undefined = "StrictUndefined"
rtorrent.init() rtorrent.init()
signal.signal(signal.SIGINT, rtorrent.stop_watch)
@app.route("/") routes = web.RouteTableDef()
def index():
@routes.get(config.prefix + "/", name='index')
def index(request):
"""The index page.""" """The index page."""
torrents = rtorrent.get_active() torrents = rtorrent.get_active()
tracker_stats = rtorrent.get_stats() tracker_stats = rtorrent.get_stats()
return render_template("index.html", **locals()) return render_template("index.html", request, locals())
@app.route("/static/<path:path>") @routes.get(config.prefix + "/get_active_torrents", name='active-torrents')
def send_static(path): def get_active_torrents(request):
"""Sends static files."""
return send_from_directory("static", path)
@app.route("/get_active_torrents")
def get_active_torrents():
"""Returns all active torrents formatted as JSON.""" """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__": if __name__ == "__main__":
app.run(host='0.0.0.0', port=5250) aiohttp.web.run_app(app, host='0.0.0.0', port=5250)

5
config.py Normal file
View File

@ -0,0 +1,5 @@
#!/usr/bin/env python3
"""
Configuation settings for Aberrant.
"""
prefix = '/aberrant'

View File

@ -153,7 +153,7 @@ def init():
WATCH_HANDLE = Watch() WATCH_HANDLE = Watch()
WATCH_HANDLE.start() WATCH_HANDLE.start()
def stop_watch(*args, **kwargs): async def stop_watch(*args, **kwargs):
"""Stops the watch thread.""" """Stops the watch thread."""
global WATCH_HANDLE global WATCH_HANDLE
WATCH_HANDLE.stop() WATCH_HANDLE.stop()

View File

@ -2,10 +2,10 @@
<html> <html>
<head> <head>
<title>Aberrant</title> <title>Aberrant</title>
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='aberrant.css') }}"> <link rel="stylesheet" type="text/css" href="/static/aberrant.css">
<script type="text/javascript" src="{{ url_for('static', filename='aberrant.js') }}"></script> <script type="text/javascript" src="/static/aberrant.js"></script>
<script> <script>
const get_torrents_uri = "{{ url_for('get_active_torrents') }}"; const get_torrents_uri = "{{ request.app.router['active-torrents'].url_for() }}";
</script> </script>
<script>window.onload = load;</script> <script>window.onload = load;</script>
</head> </head>