feat: Phase 1+2 — project setup and core infrastructure
- requirements.txt, config.yaml, .env, Dockerfile, docker-compose.yml - app/core: config (YAML+env override), logging (JSON structured), exceptions (typed hierarchy), json_utils (Markdown fence stripping) - app/clients: LLMClient ABC + ZhipuAIClient (run_in_executor), StorageClient ABC + RustFSClient (boto3 head_object for size check) - app/main.py: FastAPI app with health endpoint and router registration - app/core/dependencies.py: lru_cache singleton factories - tests/conftest.py: mock_llm, mock_storage, test_app, client fixtures - pytest.ini: asyncio_mode=auto - 11 unit tests passing
This commit is contained in:
46
app/main.py
Normal file
46
app/main.py
Normal file
@@ -0,0 +1,46 @@
|
||||
from contextlib import asynccontextmanager
|
||||
|
||||
from fastapi import FastAPI
|
||||
|
||||
from app.core.exceptions import (
|
||||
AIServiceError,
|
||||
ai_service_exception_handler,
|
||||
unhandled_exception_handler,
|
||||
)
|
||||
from app.core.logging import RequestLoggingMiddleware, get_logger
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(app: FastAPI):
|
||||
logger.info("startup", extra={"message": "AI service starting"})
|
||||
yield
|
||||
logger.info("shutdown", extra={"message": "AI service stopping"})
|
||||
|
||||
|
||||
app = FastAPI(
|
||||
title="Label AI Service",
|
||||
description="知识图谱标注平台 AI 计算服务",
|
||||
version="1.0.0",
|
||||
lifespan=lifespan,
|
||||
)
|
||||
|
||||
app.add_middleware(RequestLoggingMiddleware)
|
||||
app.add_exception_handler(AIServiceError, ai_service_exception_handler)
|
||||
app.add_exception_handler(Exception, unhandled_exception_handler)
|
||||
|
||||
|
||||
@app.get("/health", tags=["Health"])
|
||||
async def health():
|
||||
return {"status": "ok"}
|
||||
|
||||
|
||||
# Routers registered after implementation (imported lazily to avoid circular deps)
|
||||
from app.routers import text, image, video, qa, finetune # noqa: E402
|
||||
|
||||
app.include_router(text.router, prefix="/api/v1")
|
||||
app.include_router(image.router, prefix="/api/v1")
|
||||
app.include_router(video.router, prefix="/api/v1")
|
||||
app.include_router(qa.router, prefix="/api/v1")
|
||||
app.include_router(finetune.router, prefix="/api/v1")
|
||||
Reference in New Issue
Block a user