feat: production deployment - full update with providers, admin, glossaries, pricing, tests
Major changes across backend, frontend, infrastructure: - Provider system with model selection (Google, DeepL, OpenAI, Ollama, Google Cloud) - Admin panel: user management, pricing, settings - Glossary system with CSV import/export - Subscription and tier quota management - Security hardening (rate limiting, API key auth, path traversal fixes) - Docker compose for dev, prod, and IONOS deployment - Alembic migrations for new tables - Frontend: dashboard, pricing page, landing page, i18n (en/fr) - Test suite and verification scripts Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
1
core/__init__.py
Normal file
1
core/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
# Core shared services (Redis, etc.)
|
||||
107
core/logging.py
Normal file
107
core/logging.py
Normal file
@@ -0,0 +1,107 @@
|
||||
"""
|
||||
Centralised logging configuration using structlog.
|
||||
|
||||
Goals (Story 6.4: Structlog Integration):
|
||||
- JSON logs in production with fields: timestamp, level, event, request_id, user_id
|
||||
- Pretty console logs in development
|
||||
- Single configuration entry-point called from main.py
|
||||
- Context helpers to bind request_id / user_id per request
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import os
|
||||
import sys
|
||||
from typing import Optional
|
||||
|
||||
import structlog
|
||||
from structlog.contextvars import bind_contextvars, clear_contextvars, merge_contextvars
|
||||
|
||||
|
||||
def configure_logging(
|
||||
*,
|
||||
json_logs: bool = True,
|
||||
log_level: str = "INFO",
|
||||
) -> None:
|
||||
"""
|
||||
Configure structlog + stdlib logging.
|
||||
|
||||
- json_logs=True → JSONRenderer (production, Docker-friendly)
|
||||
- json_logs=False → ConsoleRenderer (development)
|
||||
- log_level driven by LOG_LEVEL env (INFO/DEBUG/WARNING/ERROR/CRITICAL)
|
||||
"""
|
||||
level = getattr(logging, log_level.upper(), logging.INFO)
|
||||
|
||||
timestamper = structlog.processors.TimeStamper(fmt="iso")
|
||||
|
||||
# Common processor chain (used both for structlog and stdlib records)
|
||||
pre_chain = [
|
||||
merge_contextvars, # adds request_id / user_id when bound
|
||||
structlog.stdlib.add_log_level,
|
||||
timestamper,
|
||||
structlog.processors.format_exc_info,
|
||||
]
|
||||
|
||||
if json_logs:
|
||||
renderer = structlog.processors.JSONRenderer()
|
||||
else:
|
||||
# Developer-friendly console output
|
||||
renderer = structlog.dev.ConsoleRenderer(colors=True)
|
||||
|
||||
processor_formatter = structlog.stdlib.ProcessorFormatter(
|
||||
processor=renderer,
|
||||
foreign_pre_chain=pre_chain,
|
||||
)
|
||||
|
||||
handler = logging.StreamHandler(sys.stdout)
|
||||
handler.setFormatter(processor_formatter)
|
||||
|
||||
# Reset root logger handlers and attach our handler
|
||||
root_logger = logging.getLogger()
|
||||
root_logger.handlers.clear()
|
||||
root_logger.addHandler(handler)
|
||||
root_logger.setLevel(level)
|
||||
|
||||
structlog.configure(
|
||||
processors=pre_chain + [structlog.stdlib.ProcessorFormatter.wrap_for_formatter],
|
||||
logger_factory=structlog.stdlib.LoggerFactory(),
|
||||
wrapper_class=structlog.stdlib.BoundLogger,
|
||||
cache_logger_on_first_use=True,
|
||||
)
|
||||
|
||||
|
||||
def get_logger(name: Optional[str] = None):
|
||||
"""
|
||||
Return a structlog logger.
|
||||
|
||||
Fallbacks to stdlib logger if structlog misconfigured for any reason.
|
||||
"""
|
||||
try:
|
||||
return structlog.get_logger(name) if name is not None else structlog.get_logger()
|
||||
except Exception: # pragma: no cover - defensive fallback
|
||||
return logging.getLogger(name)
|
||||
|
||||
|
||||
def bind_request_context(
|
||||
*,
|
||||
request_id: Optional[str] = None,
|
||||
user_id: Optional[str] = None,
|
||||
) -> None:
|
||||
"""
|
||||
Bind per-request context so every log line includes these fields.
|
||||
Safe to call multiple times; only provided keys are updated.
|
||||
"""
|
||||
kv = {}
|
||||
if request_id:
|
||||
kv["request_id"] = request_id
|
||||
if user_id:
|
||||
kv["user_id"] = user_id
|
||||
if kv:
|
||||
bind_contextvars(**kv)
|
||||
|
||||
|
||||
def clear_request_context() -> None:
|
||||
"""Clear contextvars after a request has been processed."""
|
||||
clear_contextvars()
|
||||
|
||||
134
core/redis.py
Normal file
134
core/redis.py
Normal file
@@ -0,0 +1,134 @@
|
||||
"""
|
||||
Centralized Redis client for the application.
|
||||
Single shared async and sync clients; REDIS_URL from environment.
|
||||
Used by: rate limiting, tier quota, storage tracker, auth blocklist, admin sessions, health check.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import logging
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
_async_client = None
|
||||
_sync_client = None
|
||||
# Sentinel: False = no REDIS_URL or connection failed; None = not yet tried
|
||||
_async_attempted = None
|
||||
_sync_attempted = None
|
||||
|
||||
|
||||
def get_redis_url() -> str:
|
||||
"""Return REDIS_URL from environment (empty if not set)."""
|
||||
return (os.getenv("REDIS_URL") or "").strip()
|
||||
|
||||
|
||||
def get_async_redis():
|
||||
"""
|
||||
Return shared async Redis client or None.
|
||||
Uses REDIS_URL from env. Lazy init; on failure returns None (graceful degradation).
|
||||
"""
|
||||
global _async_client, _async_attempted
|
||||
if _async_attempted is not None:
|
||||
return _async_client if _async_client is not None else None
|
||||
url = get_redis_url()
|
||||
if not url:
|
||||
_async_attempted = True
|
||||
return None
|
||||
try:
|
||||
import redis.asyncio as redis
|
||||
|
||||
_async_client = redis.Redis.from_url(url, decode_responses=True)
|
||||
logger.info("Redis async client connected (shared)")
|
||||
_async_attempted = True
|
||||
return _async_client
|
||||
except Exception as e:
|
||||
logger.warning("Redis async connection failed: %s", e)
|
||||
_async_client = None
|
||||
_async_attempted = True
|
||||
return None
|
||||
|
||||
|
||||
def get_sync_redis():
|
||||
"""
|
||||
Return shared sync Redis client or None.
|
||||
Uses REDIS_URL from env. Lazy init; on failure returns None.
|
||||
Used by health check, admin sessions, auth blocklist.
|
||||
"""
|
||||
global _sync_client, _sync_attempted
|
||||
if _sync_attempted is not None:
|
||||
return _sync_client if _sync_client is not None else None
|
||||
url = get_redis_url()
|
||||
if not url:
|
||||
_sync_attempted = True
|
||||
return None
|
||||
try:
|
||||
import redis
|
||||
|
||||
_sync_client = redis.Redis.from_url(
|
||||
url, decode_responses=True, socket_connect_timeout=5
|
||||
)
|
||||
_sync_client.ping()
|
||||
logger.info("Redis sync client connected (shared)")
|
||||
_sync_attempted = True
|
||||
return _sync_client
|
||||
except Exception as e:
|
||||
logger.warning("Redis sync connection failed: %s", e)
|
||||
_sync_client = None
|
||||
_sync_attempted = True
|
||||
return None
|
||||
|
||||
|
||||
def ping_sync() -> tuple[bool, str]:
|
||||
"""
|
||||
Ping Redis (sync). Returns (success, error_message).
|
||||
Use for health/readiness checks.
|
||||
"""
|
||||
client = get_sync_redis()
|
||||
if not client:
|
||||
return False, "not_configured"
|
||||
try:
|
||||
client.ping()
|
||||
return True, ""
|
||||
except Exception as e:
|
||||
return False, str(e)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Translation job status cache (for GET /api/v1/translations/{id})
|
||||
# Key: translation:job:{job_id}, value: JSON, TTL e.g. 2h
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
JOB_STATUS_KEY_PREFIX = "translation:job"
|
||||
JOB_STATUS_TTL = 2 * 3600 # 2 hours
|
||||
|
||||
|
||||
async def set_job_status_async(job_id: str, data: dict, ttl: int = JOB_STATUS_TTL) -> bool:
|
||||
"""Store job status in Redis for multi-instance progress polling. Returns True if stored."""
|
||||
client = get_async_redis()
|
||||
if not client:
|
||||
return False
|
||||
try:
|
||||
import json
|
||||
|
||||
key = f"{JOB_STATUS_KEY_PREFIX}:{job_id}"
|
||||
await client.set(key, json.dumps(data), ex=ttl)
|
||||
return True
|
||||
except Exception as e:
|
||||
logger.warning("Redis set_job_status_async failed for %s: %s", job_id, e)
|
||||
return False
|
||||
|
||||
|
||||
async def get_job_status_async(job_id: str) -> dict | None:
|
||||
"""Retrieve job status from Redis. Returns None if not found or Redis unavailable."""
|
||||
client = get_async_redis()
|
||||
if not client:
|
||||
return None
|
||||
try:
|
||||
import json
|
||||
|
||||
key = f"{JOB_STATUS_KEY_PREFIX}:{job_id}"
|
||||
data = await client.get(key)
|
||||
return json.loads(data) if data else None
|
||||
except Exception as e:
|
||||
logger.warning("Redis get_job_status_async failed for %s: %s", job_id, e)
|
||||
return None
|
||||
Reference in New Issue
Block a user