From e165f7908015b8bb85fc754f1c310d80fbe1a37b Mon Sep 17 00:00:00 2001 From: NGnius Date: Wed, 11 Sep 2019 09:43:32 -0400 Subject: [PATCH] Implement structure and release request --- .gitignore | 3 ++ requirements.txt | 1 + rxsmserver/__init__.py | 65 +++++++++++++++++++++++++++++++++ scripts/install-requirements.sh | 3 ++ scripts/run.sh | 3 ++ 5 files changed, 75 insertions(+) create mode 100644 requirements.txt create mode 100644 rxsmserver/__init__.py create mode 100755 scripts/install-requirements.sh create mode 100755 scripts/run.sh diff --git a/.gitignore b/.gitignore index e61bca2..b14bccb 100644 --- a/.gitignore +++ b/.gitignore @@ -114,3 +114,6 @@ dmypy.json # Pyre type checker .pyre/ +# runtime +.token +**.db diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..7e10602 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +flask diff --git a/rxsmserver/__init__.py b/rxsmserver/__init__.py new file mode 100644 index 0000000..e91ec64 --- /dev/null +++ b/rxsmserver/__init__.py @@ -0,0 +1,65 @@ +# Created 2019-09-10 by NGnius +from flask import Flask, Blueprint, jsonify, request +import sqlite3 as dblib +from hashlib import sha512 +from os import getcwd +from threading import get_ident +from time import time + +database_path = "rxsm-update.db" + +database_connections = dict() +app = Flask("rxsm-update-server") + +def get_or_create_connection(): + global database_connections, database_path + thread_id = get_ident() + if thread_id not in database_connections: + database_connections[thread_id] = dblib.connect(database_path) + return database_connections[thread_id] + +@app.route('/release', methods=['POST']) +def create_or_modify_release(): + try: + req_json = request.get_json(force=True) + token = req_json['token'] + version = req_json['version'] + url = req_json['url'] + except: + return jsonify({'status':400, 'reason':'Invalid request; missing parameter or invalid JSON'}), 400 + + with open(".token", "r") as f: + master_token = f.read() + if sha512(token.encode()).hexdigest() != master_token: + return jsonify({'status':403, 'reason':'Permission denied; invalid token'}), 403 + + db_connection = get_or_create_connection() + cursor = db_connection.cursor() + cursor.execute("SELECT version FROM releases WHERE version=?", (version,)) + result = cursor.fetchone() + operation = "" + if result is None: + cursor.execute("INSERT INTO releases (version, url, created_date) VALUES (?,?,?)", (version, url, int(time()))) + operation = "create" + else: + cursor.execute("UPDATE releases SET url=?, created_date=? WHERE version=?", (url, int(time()), version)) + operation = "update" + db_connection.commit() + + return jsonify({'status':200, 'reason':'Version %s %sd successfully' % (version, operation)}), 200 + +# always try to create db table(s) +conn = get_or_create_connection() +cursor = conn.cursor() +cursor.execute(\ + """CREATE TABLE IF NOT EXISTS releases ( + id INTEGER PRIMARY KEY, + version TEXT UNIQUE NOT NULL, + url TEXT NOT NULL, + created_date INTEGER NOT NULL + )""" ) +conn.commit() + +if __name__ == '__main__': + print("Working directory: " + getcwd()) + app.run(host='0.0.0.0', port=9080, threaded=True) diff --git a/scripts/install-requirements.sh b/scripts/install-requirements.sh new file mode 100755 index 0000000..8625ba6 --- /dev/null +++ b/scripts/install-requirements.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +sudo python3 -m install -r requirements.txt diff --git a/scripts/run.sh b/scripts/run.sh new file mode 100755 index 0000000..7009b3e --- /dev/null +++ b/scripts/run.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +python3 rxsmserver/__init__.py