feat: Add SaaS robustness middleware - Rate limiting with token bucket and sliding window algorithms - Input validation (file, language, provider) - Security headers middleware (CSP, XSS protection, etc.) - Automatic file cleanup with TTL tracking - Memory and disk monitoring - Enhanced health check and metrics endpoints - Request logging with unique IDs
This commit is contained in:
48
config.py
48
config.py
@@ -1,5 +1,6 @@
|
||||
"""
|
||||
Configuration module for the Document Translation API
|
||||
SaaS-ready with comprehensive settings for production deployment
|
||||
"""
|
||||
import os
|
||||
from pathlib import Path
|
||||
@@ -8,7 +9,7 @@ from dotenv import load_dotenv
|
||||
load_dotenv()
|
||||
|
||||
class Config:
|
||||
# Translation Service
|
||||
# ============== Translation Service ==============
|
||||
TRANSLATION_SERVICE = os.getenv("TRANSLATION_SERVICE", "google")
|
||||
DEEPL_API_KEY = os.getenv("DEEPL_API_KEY", "")
|
||||
|
||||
@@ -17,20 +18,51 @@ class Config:
|
||||
OLLAMA_MODEL = os.getenv("OLLAMA_MODEL", "llama3")
|
||||
OLLAMA_VISION_MODEL = os.getenv("OLLAMA_VISION_MODEL", "llava")
|
||||
|
||||
# File Upload Configuration
|
||||
# ============== File Upload Configuration ==============
|
||||
MAX_FILE_SIZE_MB = int(os.getenv("MAX_FILE_SIZE_MB", "50"))
|
||||
MAX_FILE_SIZE_BYTES = MAX_FILE_SIZE_MB * 1024 * 1024
|
||||
|
||||
# Directories
|
||||
BASE_DIR = Path(__file__).parent.parent
|
||||
BASE_DIR = Path(__file__).parent
|
||||
UPLOAD_DIR = BASE_DIR / "uploads"
|
||||
OUTPUT_DIR = BASE_DIR / "outputs"
|
||||
TEMP_DIR = BASE_DIR / "temp"
|
||||
LOGS_DIR = BASE_DIR / "logs"
|
||||
|
||||
# Supported file types
|
||||
SUPPORTED_EXTENSIONS = {".xlsx", ".docx", ".pptx"}
|
||||
|
||||
# API Configuration
|
||||
# ============== Rate Limiting (SaaS) ==============
|
||||
RATE_LIMIT_ENABLED = os.getenv("RATE_LIMIT_ENABLED", "true").lower() == "true"
|
||||
RATE_LIMIT_PER_MINUTE = int(os.getenv("RATE_LIMIT_PER_MINUTE", "30"))
|
||||
RATE_LIMIT_PER_HOUR = int(os.getenv("RATE_LIMIT_PER_HOUR", "200"))
|
||||
TRANSLATIONS_PER_MINUTE = int(os.getenv("TRANSLATIONS_PER_MINUTE", "10"))
|
||||
TRANSLATIONS_PER_HOUR = int(os.getenv("TRANSLATIONS_PER_HOUR", "50"))
|
||||
MAX_CONCURRENT_TRANSLATIONS = int(os.getenv("MAX_CONCURRENT_TRANSLATIONS", "5"))
|
||||
|
||||
# ============== Cleanup Service ==============
|
||||
CLEANUP_ENABLED = os.getenv("CLEANUP_ENABLED", "true").lower() == "true"
|
||||
CLEANUP_INTERVAL_MINUTES = int(os.getenv("CLEANUP_INTERVAL_MINUTES", "15"))
|
||||
FILE_TTL_MINUTES = int(os.getenv("FILE_TTL_MINUTES", "60"))
|
||||
INPUT_FILE_TTL_MINUTES = int(os.getenv("INPUT_FILE_TTL_MINUTES", "30"))
|
||||
OUTPUT_FILE_TTL_MINUTES = int(os.getenv("OUTPUT_FILE_TTL_MINUTES", "120"))
|
||||
|
||||
# Disk space thresholds
|
||||
DISK_WARNING_THRESHOLD_GB = float(os.getenv("DISK_WARNING_THRESHOLD_GB", "5.0"))
|
||||
DISK_CRITICAL_THRESHOLD_GB = float(os.getenv("DISK_CRITICAL_THRESHOLD_GB", "1.0"))
|
||||
|
||||
# ============== Security ==============
|
||||
ENABLE_HSTS = os.getenv("ENABLE_HSTS", "false").lower() == "true"
|
||||
CORS_ORIGINS = os.getenv("CORS_ORIGINS", "*").split(",")
|
||||
MAX_REQUEST_SIZE_MB = int(os.getenv("MAX_REQUEST_SIZE_MB", "100"))
|
||||
REQUEST_TIMEOUT_SECONDS = int(os.getenv("REQUEST_TIMEOUT_SECONDS", "300"))
|
||||
|
||||
# ============== Monitoring ==============
|
||||
LOG_LEVEL = os.getenv("LOG_LEVEL", "INFO")
|
||||
ENABLE_REQUEST_LOGGING = os.getenv("ENABLE_REQUEST_LOGGING", "true").lower() == "true"
|
||||
MAX_MEMORY_PERCENT = float(os.getenv("MAX_MEMORY_PERCENT", "80"))
|
||||
|
||||
# ============== API Configuration ==============
|
||||
API_TITLE = "Document Translation API"
|
||||
API_VERSION = "1.0.0"
|
||||
API_DESCRIPTION = """
|
||||
@@ -40,6 +72,12 @@ class Config:
|
||||
- Excel (.xlsx) - Preserves cell formatting, formulas, merged cells, images
|
||||
- Word (.docx) - Preserves styles, tables, images, headers/footers
|
||||
- PowerPoint (.pptx) - Preserves layouts, animations, embedded media
|
||||
|
||||
SaaS Features:
|
||||
- Rate limiting per client IP
|
||||
- Automatic file cleanup
|
||||
- Health monitoring
|
||||
- Request logging
|
||||
"""
|
||||
|
||||
@classmethod
|
||||
@@ -48,5 +86,7 @@ class Config:
|
||||
cls.UPLOAD_DIR.mkdir(exist_ok=True, parents=True)
|
||||
cls.OUTPUT_DIR.mkdir(exist_ok=True, parents=True)
|
||||
cls.TEMP_DIR.mkdir(exist_ok=True, parents=True)
|
||||
cls.LOGS_DIR.mkdir(exist_ok=True, parents=True)
|
||||
|
||||
|
||||
config = Config()
|
||||
|
||||
Reference in New Issue
Block a user