osu-wayback/main.py

78 lines
2.0 KiB
Python
Raw Normal View History

2019-03-11 07:06:31 +01:00
from os import listdir, path
import sqlite3
2019-03-07 14:04:54 +01:00
2019-03-07 14:46:50 +01:00
import tornado.gen
import tornado.httpserver
import tornado.ioloop
import tornado.web
import tornado.netutil
2019-03-07 14:04:54 +01:00
2019-03-11 13:54:27 +01:00
from helpers import apiDocumentationHelper as doc_api
2019-03-11 07:06:31 +01:00
from objects import glob
2019-03-07 14:46:50 +01:00
def make_app():
2019-03-08 11:12:47 +01:00
"""
Make tornado application instance
2019-03-07 14:04:54 +01:00
2019-03-08 11:12:47 +01:00
:return: Tornado Application instance
"""
2019-03-07 14:46:50 +01:00
def get_files(dir):
2019-03-08 11:12:47 +01:00
"""
Get all file-/directory-names in a directory with a simple blacklist
:param str: directory to read
:return: String[] of file/directory
"""
2019-03-07 14:46:50 +01:00
return [d for d in listdir(dir) if not d.startswith("_")]
2019-03-07 14:04:54 +01:00
2019-03-08 11:12:47 +01:00
def map_routes(dir):
"""
Map out a directory array of modules in a given directory
:param str: directory to map out and import
2019-03-11 13:54:27 +01:00
:return: tuple( endpoint, module.handler )[] routes
2019-03-08 11:12:47 +01:00
"""
2019-03-11 13:54:27 +01:00
routes = [(r"/%s" % dir, doc_api.handler)]
2019-03-07 14:46:50 +01:00
apis = get_files(dir)
for api in apis:
if api.endswith(".py"):
api = api.rstrip(".py")
routes.append(
2019-03-08 11:12:47 +01:00
(
2019-03-11 07:27:54 +01:00
r"/%s/%s" % (dir, api), __import__("%s.%s" % (dir.replace("/", "."), api), fromlist=[""]).handler
2019-03-08 11:12:47 +01:00
))
2019-03-07 14:46:50 +01:00
else:
2019-03-08 11:12:47 +01:00
routes += map_routes("%s/%s" % (dir, api))
return routes
2019-03-07 14:04:54 +01:00
2019-03-11 13:54:27 +01:00
glob.routes = map_routes("api")
return tornado.web.Application(glob.routes)
2019-03-11 07:06:31 +01:00
def build_database():
f = open("wayback.db", "w")
f.close()
2019-03-11 07:51:05 +01:00
glob.sql = glob.new_sql()
2019-03-11 07:06:31 +01:00
cur = glob.sql.cursor()
with open("database_structs.sql", "r") as f:
cur.executescript( f.read() )
cur.close()
print("[!] New sqlite database created")
if __name__ == "__main__":
glob.app = make_app()
if not path.isfile("wayback.db"):
build_database()
if glob.sql == None:
2019-03-11 07:51:05 +01:00
glob.sql = glob.new_sql()
2019-03-11 07:06:31 +01:00
print("Serving at %s:%s" % (glob.config["server"]["host"], glob.config["server"]["port"]))
print("To stop server press CTRL + C")
glob.app.listen(glob.config["server"]["port"], address=glob.config["server"]["host"])
tornado.ioloop.IOLoop.instance().start()