Files
mtusi2026lab2/src/config.py
T

39 lines
1.4 KiB
Python

import os
def setting(name, default=None):
path = os.environ.get(name + '_FILE')
if path:
with open(path, encoding='utf-8') as file:
return file.read().strip()
return os.environ.get(name, default)
def positive_int(name, default):
value = int(os.environ.get(name, default))
if value <= 0:
raise SystemExit('%s must be a positive integer' % name)
return value
class Settings:
def __init__(self):
self.host = os.environ.get('APP_HOST', '0.0.0.0')
self.port = positive_int('APP_PORT', '8080')
self.env = os.environ.get('APP_ENV', 'lab')
self.shutdown_timeout = positive_int('APP_SHUTDOWN_TIMEOUT', '5')
self.database_url = setting('DATABASE_URL')
self.cache_url = setting('CACHE_URL') or None
self.session_ttl = positive_int('SESSION_TTL_SECONDS', '3600')
self.session_cache_ttl = positive_int('SESSION_CACHE_TTL_SECONDS', '60')
self.db_pool_min = positive_int('DB_POOL_MIN', '1')
self.db_pool_max = positive_int('DB_POOL_MAX', '10')
self.db_pool_timeout = float(os.environ.get('DB_POOL_TIMEOUT_SECONDS', '2'))
self.db_statement_timeout_ms = positive_int('DB_STATEMENT_TIMEOUT_MS', '3000')
self.db_request_timeout = float(os.environ.get('DB_REQUEST_TIMEOUT_SECONDS', '4'))
if not self.database_url:
raise SystemExit('DATABASE_URL or DATABASE_URL_FILE is required')
settings = Settings()