chore: 初始化项目结构并添加基础配置文件
添加后端API和前端项目的初始目录结构,包括应用层模块、工具模块和模型定义 配置前后端的.dockerignore文件以排除开发环境文件 添加前端样式文件,包括字体、Tailwind CSS和主题配置 初始化Python虚拟环境中的类型标记文件
This commit is contained in:
114
后端-孕期报告解读-api/app/main.py
Normal file
114
后端-孕期报告解读-api/app/main.py
Normal file
@@ -0,0 +1,114 @@
|
||||
import logging
|
||||
import os
|
||||
import time
|
||||
from contextlib import asynccontextmanager
|
||||
|
||||
from fastapi import FastAPI, Request
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
from fastapi.responses import FileResponse, JSONResponse
|
||||
|
||||
from app.config import settings
|
||||
from app.database import init_db
|
||||
from app.routers import (
|
||||
auth,
|
||||
user,
|
||||
report,
|
||||
analysis,
|
||||
chat,
|
||||
health_records,
|
||||
calendar,
|
||||
doctor,
|
||||
share,
|
||||
)
|
||||
|
||||
# 日志配置
|
||||
logging.basicConfig(
|
||||
level=logging.DEBUG if settings.debug else logging.INFO,
|
||||
format="%(asctime)s [%(levelname)s] %(name)s: %(message)s",
|
||||
datefmt="%Y-%m-%d %H:%M:%S",
|
||||
)
|
||||
logger = logging.getLogger("孕育无忧")
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(app: FastAPI):
|
||||
logger.info("服务启动中...")
|
||||
await init_db()
|
||||
os.makedirs(settings.upload_dir, exist_ok=True)
|
||||
logger.info("服务启动完成 http://%s:%s", settings.host, settings.port)
|
||||
yield
|
||||
logger.info("服务关闭")
|
||||
|
||||
|
||||
app = FastAPI(
|
||||
title="孕育无忧 API",
|
||||
description="孕期健康检查报告解读后端服务",
|
||||
version="1.0.0",
|
||||
lifespan=lifespan,
|
||||
)
|
||||
|
||||
# CORS 配置
|
||||
cors_origins = (
|
||||
["*"]
|
||||
if settings.cors_origins == "*"
|
||||
else [o.strip() for o in settings.cors_origins.split(",") if o.strip()]
|
||||
)
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=cors_origins,
|
||||
allow_credentials=True,
|
||||
allow_methods=["*"],
|
||||
allow_headers=["*"],
|
||||
)
|
||||
|
||||
|
||||
# 请求日志中间件
|
||||
@app.middleware("http")
|
||||
async def log_requests(request: Request, call_next):
|
||||
start = time.time()
|
||||
response = await call_next(request)
|
||||
duration = round((time.time() - start) * 1000)
|
||||
logger.info(
|
||||
"%s %s → %s (%dms)",
|
||||
request.method,
|
||||
request.url.path,
|
||||
response.status_code,
|
||||
duration,
|
||||
)
|
||||
return response
|
||||
|
||||
|
||||
# 全局异常处理
|
||||
@app.exception_handler(Exception)
|
||||
async def global_exception_handler(request: Request, exc: Exception):
|
||||
logger.error("未处理异常 %s %s: %s", request.method, request.url.path, exc, exc_info=True)
|
||||
return JSONResponse(
|
||||
status_code=500,
|
||||
content={"detail": "服务器内部错误,请稍后重试"},
|
||||
)
|
||||
|
||||
app.mount("/uploads", StaticFiles(directory=settings.upload_dir), name="uploads")
|
||||
|
||||
app.include_router(auth.router, prefix="/api/auth", tags=["认证"])
|
||||
app.include_router(user.router, prefix="/api/user", tags=["用户"])
|
||||
app.include_router(report.router, prefix="/api/report", tags=["报告"])
|
||||
app.include_router(analysis.router, prefix="/api/analysis", tags=["AI解读"])
|
||||
app.include_router(chat.router, prefix="/api/chat", tags=["AI对话"])
|
||||
app.include_router(health_records.router, prefix="/api/health-records", tags=["健康档案"])
|
||||
app.include_router(calendar.router, prefix="/api/calendar", tags=["产检日历"])
|
||||
app.include_router(doctor.router, prefix="/api/doctor", tags=["专家咨询"])
|
||||
app.include_router(share.router, prefix="/api/share", tags=["分享"])
|
||||
|
||||
|
||||
@app.get("/api/health")
|
||||
async def health_check():
|
||||
return {"status": "ok", "service": "孕育无忧 API"}
|
||||
|
||||
|
||||
@app.get("/")
|
||||
async def web_demo():
|
||||
"""Web 体验页面"""
|
||||
return FileResponse(
|
||||
os.path.join(os.path.dirname(__file__), "static", "index.html")
|
||||
)
|
||||
Reference in New Issue
Block a user