107 lines
3.4 KiB
Python
Executable File
107 lines
3.4 KiB
Python
Executable File
# /opt/docker/dev/service_finder/backend/app/main.py
|
|
import os
|
|
import logging
|
|
from contextlib import asynccontextmanager
|
|
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.
|
|
Itt töltődnek be a memóriába a globális erőforrások.
|
|
"""
|
|
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:
|
|
logger.error(f"❌ i18n hiba az induláskor: {e}")
|
|
|
|
# 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
|
|
)
|
|
|
|
# --- SESSION MIDDLEWARE (OAuth2 / Google Auth támogatás) ---
|
|
# A secret_key az aláírt sütikhez (cookies) szükséges
|
|
app.add_middleware(
|
|
SessionMiddleware,
|
|
secret_key=settings.SECRET_KEY
|
|
)
|
|
|
|
# --- CORS BEÁLLÍTÁSOK (Hálózati kapu) ---
|
|
# Itt engedélyezzük, hogy a Frontend (React/Mobile) elérje az API-t
|
|
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 ---
|
|
# Képek, PDF-ek és a generált nyelvi JSON-ök kiszolgálása
|
|
app.mount("/static", StaticFiles(directory=settings.STATIC_DIR), name="static")
|
|
|
|
# --- ROUTER BEKÖTÉSE ---
|
|
# Itt csatlakozik az összes API végpont (Auth, Fleet, Billing, stb.)
|
|
app.include_router(api_router, prefix=settings.API_V1_STR)
|
|
|
|
# --- ALAPVETŐ RENDSZER VÉGPONTOK ---
|
|
|
|
@app.get("/", tags=["System"])
|
|
async def root():
|
|
""" Rendszer azonosító végpont. """
|
|
return {
|
|
"status": "online",
|
|
"system": "Service Finder Master",
|
|
"version": "2.0.1",
|
|
"environment": "Production" if not settings.DEBUG_MODE else "Development",
|
|
"features": [
|
|
"Hierarchical i18n Enabled",
|
|
"Asset Vault 2.0",
|
|
"Sentinel Security Audit",
|
|
"Robot Pipeline (0-3)"
|
|
]
|
|
}
|
|
|
|
@app.get("/health", tags=["System"])
|
|
async def health_check():
|
|
"""
|
|
Monitoring végpont.
|
|
Ha ez 'ok'-t ad, a Docker és a Load Balancer tudja, hogy a szerver él.
|
|
"""
|
|
return {
|
|
"status": "ok",
|
|
"timestamp": settings.get_now_utc_iso(),
|
|
"database": "connected" # Itt később lehet valódi ping teszt
|
|
} |