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__/
*.swp
*.swo
sync.sh

View File

@ -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`

View File

@ -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/<path:path>")
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)

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.start()
def stop_watch(*args, **kwargs):
async def stop_watch(*args, **kwargs):
"""Stops the watch thread."""
global WATCH_HANDLE
WATCH_HANDLE.stop()

View File

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