97 lines
3.0 KiB
Python
Executable File
97 lines
3.0 KiB
Python
Executable File
# /opt/docker/dev/service_finder/backend/app/main.py
|
|
import os
|
|
import logging
|
|
from contextlib import asynccontextmanager
|
|
from datetime import datetime, timezone # Szükséges a health check-hez
|
|
from fastapi import FastAPI
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
from fastapi.staticfiles import StaticFiles
|
|
from starlette.middleware.sessions import SessionMiddleware
|
|
|
|
from app.api.v1.api import api_router
|
|
from app.core.config import settings
|
|
from app.database import AsyncSessionLocal
|
|
from app.services.translation_service import translation_service
|
|
|
|
# --- LOGGING KONFIGURÁCIÓ ---
|
|
logging.basicConfig(level=logging.INFO)
|
|
logger = logging.getLogger("Sentinel-Main")
|
|
|
|
# --- LIFESPAN (Startup/Shutdown események) ---
|
|
@asynccontextmanager
|
|
async def lifespan(app: FastAPI):
|
|
"""
|
|
A rendszer 'ébredési' folyamata.
|
|
Hiba esetén ENG alapértelmezésre vált a rendszer.
|
|
"""
|
|
logger.info("🛰️ Sentinel Master System ébredése...")
|
|
|
|
# 1. Nyelvi Cache betöltése az adatbázisból
|
|
async with AsyncSessionLocal() as db:
|
|
try:
|
|
await translation_service.load_cache(db)
|
|
logger.info("🌍 i18n fordítási kulcsok aktiválva.")
|
|
except Exception as e:
|
|
# Itt a kért ERROR log: jelezzük a hibát, de a rendszer ENG fallback-el megy tovább
|
|
logger.error(f"❌ i18n hiba az induláskor: {e}. Rendszer alapértelmezett (ENG) módra vált.")
|
|
|
|
# Statikus könyvtárak ellenőrzése
|
|
os.makedirs(settings.STATIC_DIR, exist_ok=True)
|
|
os.makedirs(os.path.join(settings.STATIC_DIR, "previews"), exist_ok=True)
|
|
|
|
yield
|
|
|
|
logger.info("💤 Sentinel Master System leállítása...")
|
|
|
|
# --- APP INICIALIZÁLÁS ---
|
|
app = FastAPI(
|
|
title="Service Finder Master API",
|
|
description="Sentinel Traffic Ecosystem, Asset Vault & AI Evidence Processing",
|
|
version="2.0.1",
|
|
openapi_url=f"{settings.API_V1_STR}/openapi.json",
|
|
docs_url="/docs",
|
|
lifespan=lifespan
|
|
)
|
|
|
|
# --- MIDDLEWARES ---
|
|
app.add_middleware(
|
|
SessionMiddleware,
|
|
secret_key=settings.SECRET_KEY
|
|
)
|
|
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=[str(origin) for origin in settings.BACKEND_CORS_ORIGINS],
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
# --- STATIKUS FÁJLOK ---
|
|
app.mount("/static", StaticFiles(directory=settings.STATIC_DIR), name="static")
|
|
|
|
# --- ROUTER BEKÖTÉSE ---
|
|
app.include_router(api_router, prefix=settings.API_V1_STR)
|
|
|
|
# --- ALAPVETŐ RENDSZER VÉGPONTOK ---
|
|
|
|
@app.get("/", tags=["System"])
|
|
async def root():
|
|
return {
|
|
"status": "online",
|
|
"system": "Service Finder Master",
|
|
"version": "2.0.1",
|
|
"environment": "Production" if not settings.DEBUG_MODE else "Development"
|
|
}
|
|
|
|
@app.get("/health", tags=["System"])
|
|
async def health_check():
|
|
"""
|
|
Monitoring végpont.
|
|
JAVÍTVA: A settings.get_now_utc_iso() hiba kiiktatva, standard datetime-ra cserélve.
|
|
"""
|
|
return {
|
|
"status": "ok",
|
|
"timestamp": datetime.now(timezone.utc).isoformat(),
|
|
"database": "connected"
|
|
} |