100 lines
3.1 KiB
Python
Executable File
100 lines
3.1 KiB
Python
Executable File
from contextlib import asynccontextmanager
|
|
from fastapi import FastAPI
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
from fastapi.openapi.utils import get_openapi
|
|
|
|
# 1. Konfiguráció importálása
|
|
from app.core.config import settings
|
|
|
|
# 2. Adatbázis importok
|
|
from app.db.base import Base
|
|
from app.db.session import engine
|
|
|
|
# 3. Router importálása
|
|
from app.api.v1.router import api_router
|
|
|
|
# --- Lifespan (Életciklus) Kezelő ---
|
|
@asynccontextmanager
|
|
async def lifespan(app: FastAPI):
|
|
# INDÍTÁS (Startup): Táblák létrehozása fejlesztői módban
|
|
async with engine.begin() as conn:
|
|
# Importáljuk a modelleket, hogy a Base tudjon róluk!
|
|
from app.models import social, vehicle, user, logistics, expense
|
|
|
|
print("--- Adatbázis táblák ellenőrzése és létrehozása ---")
|
|
await conn.run_sync(Base.metadata.create_all)
|
|
|
|
yield # Itt fut az alkalmazás...
|
|
|
|
# LEÁLLÁS (Shutdown): Erőforrások elengedése
|
|
print("--- Traffic Ecosystem SuperApp leállítása ---")
|
|
await engine.dispose()
|
|
|
|
# --- Alkalmazás Inicializálása ---
|
|
app = FastAPI(
|
|
title=settings.PROJECT_NAME,
|
|
version=settings.VERSION,
|
|
openapi_url=f"{settings.API_V1_STR}/openapi.json",
|
|
lifespan=lifespan,
|
|
# Ez a beállítás segít, hogy a lakat megmaradjon frissítés után is
|
|
swagger_ui_parameters={"persistAuthorization": True}
|
|
)
|
|
|
|
# --- OpenAPI Biztonsági Séma (Hogy legyen Authorize gomb!) ---
|
|
def custom_openapi():
|
|
if app.openapi_schema:
|
|
return app.openapi_schema
|
|
|
|
openapi_schema = get_openapi(
|
|
title=app.title,
|
|
version=app.version,
|
|
description="API dokumentáció JWT hitelesítéssel",
|
|
routes=app.routes,
|
|
)
|
|
|
|
# Itt definiáljuk a lakatot és a JWT formátumot
|
|
openapi_schema["components"]["securitySchemes"] = {
|
|
"OAuth2PasswordBearer": {
|
|
"type": "oauth2",
|
|
"flows": {
|
|
"password": {
|
|
"tokenUrl": f"{settings.API_V1_STR}/auth/login"
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
# Globálisan alkalmazzuk minden védett végpontra
|
|
for path in openapi_schema["paths"]:
|
|
for method in openapi_schema["paths"][path]:
|
|
# Csak azokra a kérésekre rakunk lakatot, amik nincsenek a kivételek között
|
|
# (Az auth/login és auth/register maradjon nyitva)
|
|
if not any(x in path for x in ["/auth/login", "/auth/register"]):
|
|
openapi_schema["paths"][path][method]["security"] = [{"OAuth2PasswordBearer": []}]
|
|
|
|
app.openapi_schema = openapi_schema
|
|
return app.openapi_schema
|
|
|
|
# Felülírjuk az alapértelmezett OpenAPI generátort a sajátunkkal
|
|
app.openapi = custom_openapi
|
|
|
|
# --- Middleware (CORS) ---
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=["*"],
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
# --- Routerek csatolása ---
|
|
app.include_router(api_router, prefix=settings.API_V1_STR)
|
|
|
|
# --- Gyökér végpont ---
|
|
@app.get("/")
|
|
async def root():
|
|
return {
|
|
"message": "Traffic Ecosystem SuperApp API fut",
|
|
"version": settings.VERSION,
|
|
"docs_url": "/docs"
|
|
} |