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.
This commit is contained in:
Kumi 2024-01-23 10:20:41 +01:00
parent 23071d36f2
commit dbb4aff4e7
Signed by: kumi
GPG key ID: ECBCC9082395383F
4 changed files with 117 additions and 12 deletions

1
.gitignore vendored
View file

@ -4,3 +4,4 @@ __pycache__/
dist/ dist/
*.txt *.txt
venv/ venv/
*.log

View file

@ -1,5 +1,6 @@
import argparse import argparse
import sys import sys
import os
import threading import threading
from . import FicheServer from . import FicheServer
@ -28,6 +29,36 @@ def main():
# Parse the arguments # Parse the arguments
args = parser.parse_args() 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 # Create a Fiche object
fiche = FicheServer.from_args(args) fiche = FicheServer.from_args(args)

View file

@ -1,5 +1,6 @@
import argparse import argparse
import sys import sys
import os
import threading import threading
from . import LinesServer from . import LinesServer
@ -22,6 +23,26 @@ def main():
# Parse the arguments # Parse the arguments
args = parser.parse_args() 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 # Create a Lines object
lines = LinesServer.from_args(args) lines = LinesServer.from_args(args)

View file

@ -1,34 +1,86 @@
import argparse import argparse
import sys import sys
import os
import threading import threading
from . import RecupServer from . import RecupServer
# Define the main function # Define the main function
def main(): def main():
# Create an argument parser # 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 # Add arguments to the parser
parser.add_argument('-p', '--port', type=int, help='Port of Recup server (default: 9998)') parser.add_argument(
parser.add_argument('-L', '--listen_addr', help='Listen Address (default: 0.0.0.0)') "-p", "--port", type=int, help="Port of Recup server (default: 9998)"
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", "--listen_addr", help="Listen Address (default: 0.0.0.0)")
parser.add_argument('-l', '--log_file', help='Log file path (default: None - log to stdout)') parser.add_argument(
parser.add_argument('-b', '--banlist', help='Banlist file path') "-o", "--data_dir", help="Fiche server output directory path (default: data/)"
parser.add_argument('-w', '--allowlist', help='Allowlist file path') )
parser.add_argument('-D', '--debug', action='store_true', help='Debug mode') parser.add_argument(
parser.add_argument('-t', '--timeout', type=int, help='Timeout for incoming connections (in seconds)') "-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 # Parse the arguments
args = parser.parse_args() 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 # Create a Recup object
recup = RecupServer.from_args(args) recup = RecupServer.from_args(args)
# Run the server # Run the server
recup.run() recup.run()
# Check if the script is run directly # Check if the script is run directly
if __name__ == '__main__': if __name__ == "__main__":
main() main()