feat: add YAML configuration with defaults

This commit is contained in:
agatha 2024-09-07 16:12:35 -04:00
parent 829e842d09
commit 4d398654cb
5 changed files with 38 additions and 2 deletions

View File

@ -7,5 +7,6 @@ COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt RUN pip install --no-cache-dir -r requirements.txt
COPY src /app COPY src /app
COPY config/config.yaml /app
CMD ["python", "main.py"] CMD ["python", "main.py"]

View File

@ -0,0 +1,2 @@
host: 0.0.0.0
port: 9999

View File

@ -1,2 +1,3 @@
loguru loguru
PyYAML
SQLAlchemy SQLAlchemy

View File

@ -3,6 +3,7 @@ import socket
import threading import threading
import database import database
from database.models import Agent from database.models import Agent
from utils.config import load_yaml_config
from loguru import logger from loguru import logger
from datetime import datetime from datetime import datetime
@ -162,5 +163,8 @@ class Server:
if __name__ == '__main__': 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() server.start()

View File

@ -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