From 4d398654cb206fb1a0c4d592e1cda1c3902248f7 Mon Sep 17 00:00:00 2001 From: agatha Date: Sat, 7 Sep 2024 16:12:35 -0400 Subject: [PATCH] feat: add YAML configuration with defaults --- server/Dockerfile | 1 + server/config/config.yaml | 2 ++ server/requirements.txt | 3 ++- server/src/main.py | 6 +++++- server/src/utils/config.py | 28 ++++++++++++++++++++++++++++ 5 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 server/src/utils/config.py diff --git a/server/Dockerfile b/server/Dockerfile index e3d89df..e76c87f 100644 --- a/server/Dockerfile +++ b/server/Dockerfile @@ -7,5 +7,6 @@ COPY requirements.txt . RUN pip install --no-cache-dir -r requirements.txt COPY src /app +COPY config/config.yaml /app CMD ["python", "main.py"] \ No newline at end of file diff --git a/server/config/config.yaml b/server/config/config.yaml index e69de29..83b66a8 100644 --- a/server/config/config.yaml +++ b/server/config/config.yaml @@ -0,0 +1,2 @@ +host: 0.0.0.0 +port: 9999 \ No newline at end of file diff --git a/server/requirements.txt b/server/requirements.txt index 4e8fd74..065b0d0 100644 --- a/server/requirements.txt +++ b/server/requirements.txt @@ -1,2 +1,3 @@ loguru -SQLAlchemy +PyYAML +SQLAlchemy \ No newline at end of file diff --git a/server/src/main.py b/server/src/main.py index f4dba4a..cf22472 100644 --- a/server/src/main.py +++ b/server/src/main.py @@ -3,6 +3,7 @@ import socket import threading import database from database.models import Agent +from utils.config import load_yaml_config from loguru import logger from datetime import datetime @@ -162,5 +163,8 @@ class Server: if __name__ == '__main__': - server = Server("0.0.0.0", 9999) + # Load config.yaml if it exists + config = load_yaml_config("config.yaml") + + server = Server(config.get("host", "0.0.0.0"), config.get("port", 9999)) server.start() diff --git a/server/src/utils/config.py b/server/src/utils/config.py new file mode 100644 index 0000000..6d961bd --- /dev/null +++ b/server/src/utils/config.py @@ -0,0 +1,28 @@ +import yaml + + +def load_yaml_config(file_path): + """ + Loads a YAML configuration file. + + This function loads a YAML configuration file and returns the + configuration data as a dictionary. If the file does not exist, + or there is an error loading the file, the function will return + a default configuration. + + Args: + file_path (str): The path to the YAML configuration file. + + Returns: + dict: A dictionary containing the configuration data. + """ + try: + with open(file_path, "r") as f: + config = yaml.safe_load(f) + except Exception as e: + config = { + "host": "0.0.0.0", + "port": 9999 + } + + return config