from contextlib import asynccontextmanager from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware from app.api.router import api_router from app.core.config import get_settings from app.services.simulator import DataSimulator settings = get_settings() simulator = DataSimulator() @asynccontextmanager async def lifespan(app: FastAPI): await simulator.start() yield await simulator.stop() app = FastAPI( title="天普零碳园区智慧能源管理平台", description="Tianpu Zero-Carbon Park Smart Energy Management System", version="1.0.0", lifespan=lifespan, ) app.add_middleware( CORSMiddleware, allow_origins=["http://localhost:3000", "http://localhost:5173", "http://127.0.0.1:3000", "http://127.0.0.1:5173"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) app.include_router(api_router) @app.get("/health") async def health(): return {"status": "ok", "app": settings.APP_NAME}