From dbb4aff4e7a7f6138dcbf09bca92bc34525570d1 Mon Sep 17 00:00:00 2001 From: Kumi Date: Tue, 23 Jan 2024 10:20:41 +0100 Subject: [PATCH] Configure server args from env vars and update .gitignore Server initialization in `fiche_server.py`, `lines_server.py`, and `recup_server.py` has been modified to configure runtime arguments using environment variables, providing flexibility for different deployment environments. This enhances the portability of the code by allowing server configuration without code changes. Additionally, the `.gitignore` file has been updated to exclude `*.log` files, ensuring that log files do not get committed to version control, thus keeping the repository clean. --- .gitignore | 3 +- src/pyfiche/fiche_server.py | 31 ++++++++++++++++ src/pyfiche/lines_server.py | 21 +++++++++++ src/pyfiche/recup_server.py | 74 +++++++++++++++++++++++++++++++------ 4 files changed, 117 insertions(+), 12 deletions(-) diff --git a/.gitignore b/.gitignore index 19be985..e262c02 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ data/ __pycache__/ dist/ *.txt -venv/ \ No newline at end of file +venv/ +*.log diff --git a/src/pyfiche/fiche_server.py b/src/pyfiche/fiche_server.py index 5eebf07..4fed5a5 100644 --- a/src/pyfiche/fiche_server.py +++ b/src/pyfiche/fiche_server.py @@ -1,5 +1,6 @@ import argparse import sys +import os import threading from . import FicheServer @@ -28,6 +29,36 @@ def main(): # Parse the arguments args = parser.parse_args() + # Get environment variables + domain = os.environ.get('PYFICHE_DOMAIN', 'localhost') + port = os.environ.get('PYFICHE_PORT', 9999) + listen_addr = os.environ.get('PYFICHE_LISTEN_ADDR', '0.0.0.0') + slug_size = os.environ.get('PYFICHE_SLUG_SIZE', 8) + https = os.environ.get('PYFICHE_HTTPS', False) + output_dir = os.environ.get('PYFICHE_OUTPUT_DIR', 'data/') + buffer_size = os.environ.get('PYFICHE_BUFFER_SIZE', 4096) + max_size = os.environ.get('PYFICHE_MAX_SIZE', 5242880) + log_file = os.environ.get('PYFICHE_LOG_FILE', None) + banlist = os.environ.get('PYFICHE_BANLIST', None) + allowlist = os.environ.get('PYFICHE_ALLOWLIST', None) + debug = os.environ.get('PYFICHE_DEBUG', False) + timeout = os.environ.get('PYFICHE_TIMEOUT', None) + + # Set the arguments + args.domain = args.domain or domain + args.port = args.port or int(port) + args.listen_addr = args.listen_addr or listen_addr + args.slug_size = args.slug_size or int(slug_size) + args.https = args.https or bool(https) + args.output_dir = args.output_dir or output_dir + args.buffer_size = args.buffer_size or int(buffer_size) + args.max_size = args.max_size or int(max_size) + args.log_file = args.log_file or log_file + args.banlist = args.banlist or banlist + args.allowlist = args.allowlist or allowlist + args.debug = args.debug or bool(debug) + args.timeout = args.timeout or timeout + # Create a Fiche object fiche = FicheServer.from_args(args) diff --git a/src/pyfiche/lines_server.py b/src/pyfiche/lines_server.py index 669927a..8fffc6d 100644 --- a/src/pyfiche/lines_server.py +++ b/src/pyfiche/lines_server.py @@ -1,5 +1,6 @@ import argparse import sys +import os import threading from . import LinesServer @@ -22,6 +23,26 @@ def main(): # Parse the arguments args = parser.parse_args() + # Get environment variables + port = os.environ.get('PYFICHE_LINES_PORT', 9997) + listen_addr = os.environ.get('PYFICHE_LINES_LISTEN_ADDR', os.environ.get('PYFICHE_LISTEN_ADDR', '0.0.0.0')) + data_dir = os.environ.get('PYFICHE_LINES_DATA_DIR', os.environ.get('PYFICHE_DATA_DIR', 'data/')) + log_file = os.environ.get('PYFICHE_LINES_LOG_FILE', os.environ.get('PYFICHE_LOG_FILE', None)) + banlist = os.environ.get('PYFICHE_LINES_BANLIST', os.environ.get('PYFICHE_BANLIST', None)) + allowlist = os.environ.get('PYFICHE_LINES_ALLOWLIST', os.environ.get('PYFICHE_ALLOWLIST', None)) + max_size = os.environ.get('PYFICHE_LINES_MAX_SIZE', os.environ.get('PYFICHE_MAX_SIZE', 5242880)) + debug = os.environ.get('PYFICHE_LINES_DEBUG', os.environ.get('PYFICHE_DEBUG', False)) + + # Set the arguments + args.port = args.port or int(port) + args.listen_addr = args.listen_addr or listen_addr + args.data_dir = args.data_dir or data_dir + args.log_file = args.log_file or log_file + args.banlist = args.banlist or banlist + args.allowlist = args.allowlist or allowlist + args.max_size = args.max_size or max_size + args.debug = args.debug or bool(debug) + # Create a Lines object lines = LinesServer.from_args(args) diff --git a/src/pyfiche/recup_server.py b/src/pyfiche/recup_server.py index 2a7cd35..3ce7b9a 100644 --- a/src/pyfiche/recup_server.py +++ b/src/pyfiche/recup_server.py @@ -1,34 +1,86 @@ import argparse import sys +import os import threading from . import RecupServer + # Define the main function def main(): # Create an argument parser - parser = argparse.ArgumentParser(description='PyRecup Server - returns files uploaded through PyFiche') + parser = argparse.ArgumentParser( + description="PyRecup Server - returns files uploaded through PyFiche" + ) # Add arguments to the parser - parser.add_argument('-p', '--port', type=int, help='Port of Recup server (default: 9998)') - parser.add_argument('-L', '--listen_addr', help='Listen Address (default: 0.0.0.0)') - parser.add_argument('-o', '--data_dir', help='Fiche server output directory path (default: data/)') - parser.add_argument('-B', '--buffer_size', type=int, help='Buffer size (default: 16)') # TODO: Do we *really* need this? - parser.add_argument('-l', '--log_file', help='Log file path (default: None - log to stdout)') - parser.add_argument('-b', '--banlist', help='Banlist file path') - parser.add_argument('-w', '--allowlist', help='Allowlist file path') - parser.add_argument('-D', '--debug', action='store_true', help='Debug mode') - parser.add_argument('-t', '--timeout', type=int, help='Timeout for incoming connections (in seconds)') + parser.add_argument( + "-p", "--port", type=int, help="Port of Recup server (default: 9998)" + ) + parser.add_argument("-L", "--listen_addr", help="Listen Address (default: 0.0.0.0)") + parser.add_argument( + "-o", "--data_dir", help="Fiche server output directory path (default: data/)" + ) + parser.add_argument( + "-B", "--buffer_size", type=int, help="Buffer size (default: 16)" + ) # TODO: Do we *really* need this? + parser.add_argument( + "-l", "--log_file", help="Log file path (default: None - log to stdout)" + ) + parser.add_argument("-b", "--banlist", help="Banlist file path") + parser.add_argument("-w", "--allowlist", help="Allowlist file path") + parser.add_argument("-D", "--debug", action="store_true", help="Debug mode") + parser.add_argument( + "-t", + "--timeout", + type=int, + help="Timeout for incoming connections (in seconds)", + ) # Parse the arguments args = parser.parse_args() + # Get environment variables + port = os.environ.get("PYFICHE_RECUP_PORT", 9998) + listen_addr = os.environ.get( + "PYFICHE_RECUP_LISTEN_ADDR", os.environ.get("PYFICHE_LISTEN_ADDR", "0.0.0.0") + ) + data_dir = os.environ.get( + "PYFICHE_RECUP_DATA_DIR", os.environ.get("PYFICHE_DATA_DIR", "data/") + ) + buffer_size = os.environ.get("PYFICHE_RECUP_BUFFER_SIZE", 16) + log_file = os.environ.get( + "PYFICHE_RECUP_LOG_FILE", os.environ.get("PYFICHE_LOG_FILE", None) + ) + banlist = os.environ.get( + "PYFICHE_RECUP_BANLIST", os.environ.get("PYFICHE_BANLIST", None) + ) + allowlist = os.environ.get( + "PYFICHE_RECUP_ALLOWLIST", os.environ.get("PYFICHE_ALLOWLIST", None) + ) + debug = os.environ.get( + "PYFICHE_RECUP_DEBUG", os.environ.get("PYFICHE_DEBUG", False) + ) + timeout = os.environ.get("PYFICHE_RECUP_TIMEOUT", None) + + # Set the arguments + args.port = args.port or int(port) + args.listen_addr = args.listen_addr or listen_addr + args.data_dir = args.data_dir or data_dir + args.buffer_size = args.buffer_size or buffer_size + args.log_file = args.log_file or log_file + args.banlist = args.banlist or banlist + args.allowlist = args.allowlist or allowlist + args.debug = args.debug or bool(debug) + args.timeout = args.timeout or timeout + # Create a Recup object recup = RecupServer.from_args(args) # Run the server recup.run() + # Check if the script is run directly -if __name__ == '__main__': +if __name__ == "__main__": main()