Files
Grinch-AP/WebHost.py

77 lines
2.4 KiB
Python
Raw Normal View History

2020-06-20 20:03:06 +02:00
import os
import multiprocessing
import logging
import typing
2020-06-20 20:03:06 +02:00
import ModuleUpdate
2022-03-14 21:30:18 +01:00
ModuleUpdate.requirements_files.add(os.path.join("WebHostLib", "requirements.txt"))
ModuleUpdate.update()
# in case app gets imported by something like gunicorn
import Utils
2022-03-14 21:30:18 +01:00
Utils.local_path.cached_path = os.path.dirname(__file__)
from WebHostLib import app as raw_app
2020-06-20 20:03:06 +02:00
from waitress import serve
from WebHostLib.models import db
from WebHostLib.autolauncher import autohost, autogen
from WebHostLib.lttpsprites import update_sprites_lttp
from WebHostLib.options import create as create_options_files
2020-06-20 20:03:06 +02:00
configpath = os.path.abspath("config.yaml")
2022-03-31 05:08:15 +02:00
if not os.path.exists(configpath): # fall back to config.yaml in home
configpath = os.path.abspath(Utils.user_path('config.yaml'))
2020-06-20 20:03:06 +02:00
2020-06-27 18:17:36 +02:00
2020-07-10 17:42:22 +02:00
def get_app():
app = raw_app
2020-06-20 20:03:06 +02:00
if os.path.exists(configpath):
import yaml
app.config.from_file(configpath, yaml.safe_load)
2020-06-20 20:03:06 +02:00
logging.info(f"Updated config from {configpath}")
db.bind(**app.config["PONY"])
db.generate_mapping(create_tables=True)
2020-07-10 17:42:22 +02:00
return app
def create_ordered_tutorials_file() -> typing.List[typing.Dict[str, typing.Any]]:
2022-03-14 21:30:18 +01:00
import json
with open(Utils.local_path("WebHostLib", "static", "assets", "tutorial", "tutorials.json")) as source:
2022-03-14 21:30:18 +01:00
data = json.load(source)
data = sorted(data, key=lambda entry: entry["gameTitle"].lower())
folder = Utils.local_path("WebHostLib", "static", "generated")
os.makedirs(folder, exist_ok=True)
with open(os.path.join(folder, "tutorials.json"), "w") as target:
2022-03-14 21:30:18 +01:00
json.dump(data, target)
return data
2022-03-14 21:30:18 +01:00
2020-07-10 17:42:22 +02:00
if __name__ == "__main__":
multiprocessing.freeze_support()
multiprocessing.set_start_method('spawn')
logging.basicConfig(format='[%(asctime)s] %(message)s', level=logging.INFO)
2021-09-07 00:42:02 +02:00
try:
update_sprites_lttp()
except Exception as e:
logging.exception(e)
logging.warning("Could not update LttP sprites.")
2020-07-10 17:42:22 +02:00
app = get_app()
create_options_files()
2022-03-14 21:30:18 +01:00
create_ordered_tutorials_file()
2020-07-10 17:42:22 +02:00
if app.config["SELFLAUNCH"]:
autohost(app.config)
if app.config["SELFGEN"]:
autogen(app.config)
2020-07-10 17:42:22 +02:00
if app.config["SELFHOST"]: # using WSGI, you just want to run get_app()
if app.config["DEBUG"]:
autohost(app.config)
app.run(debug=True, port=app.config["PORT"])
else:
serve(app, port=app.config["PORT"], threads=app.config["WAITRESS_THREADS"])