This commit is contained in:
Emily 2019-03-07 14:46:50 +01:00
parent e491530689
commit c542c66f7e
2 changed files with 64 additions and 30 deletions

38
flask_main.py Normal file
View File

@ -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/<ver>/<func>")
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"])

56
main.py
View File

@ -1,38 +1,34 @@
import json from os import listdir
#import MySQLdb
#import MySQLdb.cursors
from flask import Flask, make_response, request, render_template, jsonify
from objects import glob
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: def make_app():
config = json.load(f) routes = []
# Setup sql def get_files(dir):
#glob.sql = MySQLdb.connect(**config["sql"], cursorclass = MySQLdb.cursors.DictCursor) return [d for d in listdir(dir) if not d.startswith("_")]
@app.route("/") def map_out(dir):
@app.route("/home") apis = get_files(dir)
def home_index(): for api in apis:
return render_template("index.html") 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") map_out("api")
def download_index(): return routes
return render_template("download.html")
@app.route("/api") r"/api/%s/%s"
def api_index():
return render_template("api.html")
@app.route("/api/<ver>/<func>") return tornado.web.Application(routes)
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"])