From 94567275b85214ad3875c81acb44c3a8205633cc Mon Sep 17 00:00:00 2001 From: huangzhuang_3rd Date: Wed, 12 Aug 2026 13:30:42 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20AI=20Worker=20=E5=B9=B3=E5=8F=B0=20MVP?= =?UTF-8?q?=20v1.0.0=20-=20=E5=A4=9A=E7=A7=9F=E6=88=B7/=E9=A1=B9=E7=9B=AE?= =?UTF-8?q?=E7=AE=A1=E7=90=86/AI=20Worker/=E4=BB=BB=E5=8A=A1=E7=BC=96?= =?UTF-8?q?=E6=8E=92/HITL=E5=AE=A1=E6=A0=B8/=E6=88=90=E6=9C=AC=E6=B2=BB?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/main.py | 67 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 backend/app/main.py diff --git a/backend/app/main.py b/backend/app/main.py new file mode 100644 index 0000000..5c15d69 --- /dev/null +++ b/backend/app/main.py @@ -0,0 +1,67 @@ +"""AI Worker Platform - FastAPI Application Entry Point.""" +import os +from contextlib import asynccontextmanager +from fastapi import FastAPI, Request +from fastapi.middleware.cors import CORSMiddleware +from fastapi.responses import JSONResponse +from app.config import settings +from app.database import init_db +from app.api.v1 import api_router +from app.core.exceptions import AppException + + +@asynccontextmanager +async def lifespan(app: FastAPI): + """Initialize database on startup.""" + init_db() + # Seed default tenant if not exists + from app.database import SessionLocal + from app.models.tenant import Tenant + db = SessionLocal() + try: + existing = db.query(Tenant).filter(Tenant.slug == "default").first() + if not existing: + tenant = Tenant(name="Default Tenant", slug="default") + db.add(tenant) + db.commit() + print("[Startup] Created default tenant") + finally: + db.close() + print(f"[Startup] {settings.APP_NAME} v{settings.APP_VERSION} ready") + yield + + +app = FastAPI( + title=settings.APP_NAME, + version=settings.APP_VERSION, + description="AI Worker Project Management Platform - MVP", + lifespan=lifespan, +) + +# CORS +app.add_middleware( + CORSMiddleware, + allow_origins=settings.cors_origins_list, + allow_credentials=True, + allow_methods=["*"], + allow_headers=["*"], +) + + +# Exception handler +@app.exception_handler(AppException) +async def app_exception_handler(request: Request, exc: AppException): + return JSONResponse( + status_code=exc.status_code, + content={"detail": exc.detail}, + ) + + +# Health check +@app.get("/health") +def health(): + return {"status": "ok", "version": settings.APP_VERSION} + + +# API routes +app.include_router(api_router)