From c542c66f7e76801d2c4d996a54c4155974158e9c Mon Sep 17 00:00:00 2001 From: Sunpy Date: Thu, 7 Mar 2019 14:46:50 +0100 Subject: [PATCH] Redo? --- flask_main.py | 38 ++++++++++++++++++++++++++++++++++ main.py | 56 ++++++++++++++++++++++++--------------------------- 2 files changed, 64 insertions(+), 30 deletions(-) create mode 100644 flask_main.py diff --git a/flask_main.py b/flask_main.py new file mode 100644 index 0000000..445082f --- /dev/null +++ b/flask_main.py @@ -0,0 +1,38 @@ +import json +#import MySQLdb +#import MySQLdb.cursors +from flask import Flask, make_response, request, render_template, jsonify +from objects import glob + +import api + +app = Flask(__name__) + +with open("config.json", "r") as f: + config = json.load(f) + +# Setup sql +#glob.sql = MySQLdb.connect(**config["sql"], cursorclass = MySQLdb.cursors.DictCursor) + +@app.route("/") +@app.route("/home") +def home_index(): + return render_template("index.html") + +@app.route("/download") +def download_index(): + return render_template("download.html") + +@app.route("/api") +def api_index(): + return render_template("api.html") + +@app.route("/api//") +def api_call(ver, func): + if ver not in api.versions or func not in api.versions[ver].handlers: + return jsonify(api.missing.handle(request)) + data = api.versions[ver].handlers[func].handle(request) + return jsonify(data) + +if __name__ == "__main__": + app.run(**config["web"]) \ No newline at end of file diff --git a/main.py b/main.py index 445082f..93ab342 100644 --- a/main.py +++ b/main.py @@ -1,38 +1,34 @@ -import json -#import MySQLdb -#import MySQLdb.cursors -from flask import Flask, make_response, request, render_template, jsonify -from objects import glob +from os import listdir -import api +import tornado.gen +import tornado.httpserver +import tornado.ioloop +import tornado.web +import tornado.netutil -app = Flask(__name__) -with open("config.json", "r") as f: - config = json.load(f) +def make_app(): + routes = [] -# Setup sql -#glob.sql = MySQLdb.connect(**config["sql"], cursorclass = MySQLdb.cursors.DictCursor) + def get_files(dir): + return [d for d in listdir(dir) if not d.startswith("_")] -@app.route("/") -@app.route("/home") -def home_index(): - return render_template("index.html") + def map_out(dir): + apis = get_files(dir) + for api in apis: + endpoint = "" + if api.endswith(".py"): + api = api.rstrip(".py") + routes.append( + { + "/%s/%s" % (dir, api): __import__("%s.%s" % (dir.replace("/", "."), api), fromlist=[""]) + }) + else: + map_out("%s/%s" % (dir, api)) -@app.route("/download") -def download_index(): - return render_template("download.html") + map_out("api") + return routes -@app.route("/api") -def api_index(): - return render_template("api.html") + r"/api/%s/%s" -@app.route("/api//") -def api_call(ver, func): - if ver not in api.versions or func not in api.versions[ver].handlers: - return jsonify(api.missing.handle(request)) - data = api.versions[ver].handlers[func].handle(request) - return jsonify(data) - -if __name__ == "__main__": - app.run(**config["web"]) \ No newline at end of file + return tornado.web.Application(routes)