forked from mtusi2026/mtusi2026lab5
lab4 minimum moved to lab5
This commit is contained in:
+113
@@ -0,0 +1,113 @@
|
||||
import os
|
||||
import socket
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
from urllib.parse import quote
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class ObjectSettings:
|
||||
endpoint: str
|
||||
region: str
|
||||
bucket: str
|
||||
access_key: str
|
||||
secret_key: str
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class Settings:
|
||||
host: str
|
||||
port: int
|
||||
env: str
|
||||
log_level: str
|
||||
shutdown_timeout: int
|
||||
drain_seconds: float
|
||||
keepalive_timeout: int
|
||||
session_ttl_seconds: int
|
||||
idempotency_ttl_seconds: int
|
||||
database_url: str
|
||||
instance_id: str
|
||||
app_version: str
|
||||
|
||||
|
||||
def _read_secret_file(file_var: str, file_name: str) -> str:
|
||||
try:
|
||||
value = Path(file_name).read_text(encoding='utf-8').strip()
|
||||
except OSError:
|
||||
raise RuntimeError('%s could not be read' % file_var) from None
|
||||
if not value:
|
||||
raise RuntimeError('%s is empty' % file_var)
|
||||
return value
|
||||
|
||||
|
||||
def _database_url() -> str:
|
||||
"""DATABASE_URL_FILE, then DATABASE_URL, then DB_* parts with the password from DB_PASSWORD_FILE.
|
||||
|
||||
Secret values are never logged.
|
||||
"""
|
||||
file_name = os.getenv('DATABASE_URL_FILE', '').strip()
|
||||
if file_name:
|
||||
return _read_secret_file('DATABASE_URL_FILE', file_name)
|
||||
value = os.getenv('DATABASE_URL', '').strip()
|
||||
if value:
|
||||
return value
|
||||
password_file = os.getenv('DB_PASSWORD_FILE', '').strip()
|
||||
if not password_file:
|
||||
raise RuntimeError('DATABASE_URL, DATABASE_URL_FILE or DB_PASSWORD_FILE is required')
|
||||
password = _read_secret_file('DB_PASSWORD_FILE', password_file)
|
||||
parts = {name: os.getenv(name, '').strip() for name in ('DB_HOST', 'DB_USER', 'DB_NAME')}
|
||||
missing = [name for name, part in parts.items() if not part]
|
||||
if missing:
|
||||
raise RuntimeError('%s required with DB_PASSWORD_FILE' % ', '.join(missing))
|
||||
return 'postgresql://%s:%s@%s:%s/%s' % (
|
||||
quote(parts['DB_USER'], safe=''),
|
||||
quote(password, safe=''),
|
||||
parts['DB_HOST'],
|
||||
int(os.getenv('DB_PORT', '5432')),
|
||||
quote(parts['DB_NAME'], safe=''),
|
||||
)
|
||||
|
||||
|
||||
def load_object_settings() -> ObjectSettings:
|
||||
"""S3 credentials come from files. The values are not logged."""
|
||||
endpoint = os.getenv('S3_ENDPOINT', '').strip()
|
||||
region = os.getenv('S3_REGION', 'us-east-1').strip() or 'us-east-1'
|
||||
bucket = os.getenv('S3_BUCKET', '').strip()
|
||||
key_file = os.getenv('S3_ACCESS_KEY_ID_FILE', '').strip()
|
||||
secret_file = os.getenv('S3_SECRET_ACCESS_KEY_FILE', '').strip()
|
||||
missing = [
|
||||
name for name, value in (
|
||||
('S3_ENDPOINT', endpoint),
|
||||
('S3_BUCKET', bucket),
|
||||
('S3_ACCESS_KEY_ID_FILE', key_file),
|
||||
('S3_SECRET_ACCESS_KEY_FILE', secret_file),
|
||||
) if not value
|
||||
]
|
||||
if missing:
|
||||
raise RuntimeError('%s is required' % ', '.join(missing))
|
||||
return ObjectSettings(
|
||||
endpoint,
|
||||
region,
|
||||
bucket,
|
||||
_read_secret_file('S3_ACCESS_KEY_ID_FILE', key_file),
|
||||
_read_secret_file('S3_SECRET_ACCESS_KEY_FILE', secret_file),
|
||||
)
|
||||
|
||||
|
||||
def load_settings() -> Settings:
|
||||
return Settings(
|
||||
host=os.getenv('APP_HOST', '0.0.0.0'),
|
||||
port=int(os.getenv('APP_PORT', '8080')),
|
||||
env=os.getenv('APP_ENV', 'lab'),
|
||||
log_level=os.getenv('LOG_LEVEL', 'info').lower(),
|
||||
shutdown_timeout=int(os.getenv('SHUTDOWN_TIMEOUT_SECONDS', '3')),
|
||||
drain_seconds=float(os.getenv('DRAIN_SECONDS', '4')),
|
||||
# Longer than HAProxy's idle-connection purge (pool-purge-delay 5 s), so the LB usually closes them first.
|
||||
keepalive_timeout=int(os.getenv('KEEPALIVE_TIMEOUT_SECONDS', '10')),
|
||||
session_ttl_seconds=int(os.getenv('SESSION_TTL_SECONDS', '3600')),
|
||||
idempotency_ttl_seconds=int(os.getenv('IDEMPOTENCY_TTL_SECONDS', '86400')),
|
||||
database_url=_database_url(),
|
||||
# Docker sets the hostname to the short container ID, which stop-one.sh maps back to a container.
|
||||
instance_id=os.getenv('INSTANCE_ID', '').strip() or socket.gethostname(),
|
||||
app_version=os.getenv('APP_VERSION', 'dev').strip() or 'dev',
|
||||
)
|
||||
Reference in New Issue
Block a user