

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware

import logging
from logging.handlers import RotatingFileHandler


# 日志记录
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
# 文件处理器
rotating_handler = RotatingFileHandler(filename="logs/app.log", maxBytes=10240000, backupCount=5, encoding="utf-8")
rotating_handler.setLevel(logging.INFO)
logger.addHandler(rotating_handler)

# 创建一个日志消息的格式
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
rotating_handler.setFormatter(formatter)


# 初始化FastAPI应用
app = FastAPI(
    title="流水分析智能体服务",
    description="流水分析",
    version="1.0"
)

# 添加CORS中间件解决预检请求问题
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],  # 允许所有来源，生产环境应指定具体域名
    allow_credentials=True,
    allow_methods=["*"],  # 允许所有HTTP方法
    allow_headers=["*"],  # 允许所有头部
)

@app.get("/")
def root():
    return {"message": "Hello, Agents for you!"}


if __name__ == "__main__":
    import uvicorn
    uvicorn.run("main:app", host="0.0.0.0", port=3699, workers=1, reload=True)
    logger.info("agent app is started at 3699 port")