51 lines
1.3 KiB
Python
51 lines
1.3 KiB
Python
|
|
import asyncio
|
||
|
|
from contextlib import asynccontextmanager
|
||
|
|
|
||
|
|
from fastapi import FastAPI
|
||
|
|
from fastapi.middleware.cors import CORSMiddleware
|
||
|
|
|
||
|
|
from app.routers import vehicles, positions, events
|
||
|
|
from app.websocket import router as ws_router
|
||
|
|
from app.services.connection_checker import connection_checker_loop
|
||
|
|
|
||
|
|
|
||
|
|
@asynccontextmanager
|
||
|
|
async def lifespan(app: FastAPI):
|
||
|
|
# Startup: запуск фоновых задач
|
||
|
|
task = asyncio.create_task(connection_checker_loop())
|
||
|
|
yield
|
||
|
|
# Shutdown: остановка фоновых задач
|
||
|
|
task.cancel()
|
||
|
|
try:
|
||
|
|
await task
|
||
|
|
except asyncio.CancelledError:
|
||
|
|
pass
|
||
|
|
|
||
|
|
|
||
|
|
app = FastAPI(
|
||
|
|
title="Transport Monitoring API",
|
||
|
|
description="API для системы мониторинга транспорта",
|
||
|
|
version="1.0.0",
|
||
|
|
lifespan=lifespan
|
||
|
|
)
|
||
|
|
|
||
|
|
# CORS middleware
|
||
|
|
app.add_middleware(
|
||
|
|
CORSMiddleware,
|
||
|
|
allow_origins=["*"],
|
||
|
|
allow_credentials=True,
|
||
|
|
allow_methods=["*"],
|
||
|
|
allow_headers=["*"],
|
||
|
|
)
|
||
|
|
|
||
|
|
# Include routers
|
||
|
|
app.include_router(vehicles.router, prefix="/vehicles", tags=["vehicles"])
|
||
|
|
app.include_router(positions.router, tags=["positions"])
|
||
|
|
app.include_router(events.router, prefix="/events", tags=["events"])
|
||
|
|
app.include_router(ws_router)
|
||
|
|
|
||
|
|
|
||
|
|
@app.get("/health")
|
||
|
|
async def health_check():
|
||
|
|
return {"status": "ok"}
|