Archipelago/WebHost.py

55 lines
1.4 KiB
Python
Raw Normal View History

2020-06-20 18:03:06 +00:00
import os
import multiprocessing
import logging
from WebHost import app
from waitress import serve
from WebHost.models import db, Room, db_session, select
2020-06-20 18:03:06 +00:00
DEBUG = False
2020-06-23 03:46:32 +00:00
port = 80
2020-06-20 18:03:06 +00:00
2020-06-27 16:17:36 +00:00
def autohost(config: dict):
return
# not implemented yet. https://github.com/ponyorm/pony/issues/527
2020-06-27 16:17:36 +00:00
import time
from datetime import timedelta, datetime
def keep_running():
# db.bind(**config["PONY"])
# db.generate_mapping(check_tables=False)
2020-06-27 16:17:36 +00:00
while 1:
time.sleep(3)
with db_session:
rooms = select(
room for room in Room if
room.last_activity >= datetime.utcnow() - timedelta(hours=room.timeout))
logging.info(rooms)
2020-06-27 16:17:36 +00:00
import threading
threading.Thread(target=keep_running).start()
2020-06-20 18:03:06 +00:00
if __name__ == "__main__":
multiprocessing.freeze_support()
multiprocessing.set_start_method('spawn')
logging.basicConfig(format='[%(asctime)s] %(message)s', level=logging.INFO)
configpath = "config.yaml"
if os.path.exists(configpath):
import yaml
with open(configpath) as c:
app.config.update(yaml.safe_load(c))
logging.info(f"Updated config from {configpath}")
db.bind(**app.config["PONY"])
db.generate_mapping(create_tables=True)
if DEBUG:
autohost(app.config)
2020-06-23 03:46:32 +00:00
app.run(debug=True, port=port)
2020-06-20 18:03:06 +00:00
else:
serve(app, port=port, threads=app.config["WAITRESS_THREADS"])