aidress/app/main.py
2025-03-21 20:26:47 +08:00

73 lines
2.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
import os
import logging
from dotenv import load_dotenv
from app.routers import dashscope_router, qcloud_router, dress_router, tryon_router
from app.utils.config import get_settings
from app.database import Base, engine
# 创建数据库表
Base.metadata.create_all(bind=engine)
# 加载环境变量
load_dotenv(dotenv_path=".env")
# 配置日志
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
logger = logging.getLogger(__name__)
app = FastAPI(
title="AI-Dressing API",
description="基于 DashScope 的 AI 服务 API",
version="0.1.0",
)
# 添加 CORS 中间件
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # 在生产环境中,应该指定确切的域名
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# 注册路由
app.include_router(dashscope_router.router, prefix="/api/dashscope", tags=["DashScope"])
app.include_router(qcloud_router.router, prefix="/api/qcloud", tags=["腾讯云"])
app.include_router(dress_router.router, prefix="/api/dresses", tags=["服装"])
app.include_router(tryon_router.router, prefix="/api/tryons", tags=["试穿"])
@app.get("/", tags=["健康检查"])
async def root():
"""API 根端点"""
return {"status": "正常", "message": "服务运行中"}
@app.get("/health", tags=["健康检查"])
async def health_check():
"""健康检查端点用于Docker容器健康监控"""
return {"status": "healthy", "message": "服务运行正常"}
@app.get("/info", tags=["服务信息"])
async def get_info():
"""获取服务基本信息"""
settings = get_settings()
return {
"app_name": "AI-Dressing API",
"version": "0.1.0",
"debug_mode": settings.debug,
}
if __name__ == "__main__":
import uvicorn
settings = get_settings()
uvicorn.run(
"app.main:app",
host=settings.host,
port=settings.port,
reload=settings.debug,
)