From 3a4cd4935dafe6d445daac3b7d6ced409f99aa23 Mon Sep 17 00:00:00 2001 From: Jan Tuomi Date: Sun, 3 Sep 2023 00:12:19 +0300 Subject: Add web server --- aggro.py | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) (limited to 'aggro.py') diff --git a/aggro.py b/aggro.py index 30b176e..09fb840 100644 --- a/aggro.py +++ b/aggro.py @@ -1,19 +1,28 @@ import json import os -import threading + +# from multiprocessing import Process +from threading import Thread import time from app.AggroConfig import AggroConfig from app.MemoryState import memory_state from app.PluginManager import PluginManager -from app.database import setup_db +from app.database import database_manager +from app.server import run_web_server -def run_plugin_thread(): +def run_plugin_thread(manager: PluginManager, config: AggroConfig): print("Plugin thread starting...") manager.run() print("Plugin thread exiting...") +def run_server_thread(config: AggroConfig): + print("Server thread starting...") + run_web_server(config.server_host, config.server_port) + print("Server thread exiting...") + + if __name__ == "__main__": print("Starting aggro. Press CTRL-C to exit.") @@ -23,21 +32,27 @@ if __name__ == "__main__": aggrofile_content = json.loads(f.read()) aggro_config = AggroConfig( + server_host=aggrofile_content.get("server_host", "localhost"), + server_port=aggrofile_content.get("server_port", 8080), db_path=aggrofile_content.get("db_path", "db.json"), plugins=aggrofile_content["plugins"], graph=aggrofile_content["graph"], ) - setup_db(aggro_config) + database_manager.setup(aggro_config) manager = PluginManager(aggro_config) manager.build_plugin_instances() memory_state.running = True - plugin_thread = threading.Thread(target=run_plugin_thread) + plugin_thread = Thread(target=run_plugin_thread, args=[manager, aggro_config]) plugin_thread.start() + server_thread = Thread(target=run_server_thread, args=[aggro_config]) + server_thread.daemon = True + server_thread.start() + try: while memory_state.running: time.sleep(0.1) @@ -45,4 +60,5 @@ if __name__ == "__main__": memory_state.running = False print("Waiting for threads to exit...") plugin_thread.join() + print("Server thread exiting...") print("Main thread exiting...") -- cgit v1.3