-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
151 lines (124 loc) · 3.72 KB
/
main.py
File metadata and controls
151 lines (124 loc) · 3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/usr/bin/env python3
"""
企业微信AI客服系统 - 主服务入口
统一管理和协调所有微服务,提供统一的API网关。
作者: JLH
邮箱: joerggis1024@gmail.com
创建日期: 2025-09-22
更新日期: 2025-09-22
"""
import asyncio
import logging
from contextlib import asynccontextmanager
from typing import Dict, Any
import uvicorn
from fastapi import FastAPI, Depends, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from src.utils.config import get_config
from src.utils.logger import setup_logging
from src.database.database import get_database, close_database
@asynccontextmanager
async def lifespan(app: FastAPI):
"""应用生命周期管理"""
# 启动时初始化
logging.info("🚀 企业微信AI客服系统启动中...")
# 初始化数据库连接
await get_database()
logging.info("✅ 数据库连接初始化完成")
yield
# 关闭时清理
logging.info("🔄 系统正在关闭...")
await close_database()
logging.info("✅ 数据库连接已关闭")
# 创建FastAPI应用
config = get_config()
app = FastAPI(
title="企业微信AI客服系统",
description="集成企业微信、阿里云AI、JIRA的智能客服系统",
version="1.0.0",
docs_url="/docs",
redoc_url="/redoc",
openapi_url="/openapi.json",
lifespan=lifespan
)
# 配置CORS
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # 生产环境应该限制具体域名
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.get("/", tags=["系统信息"])
async def root():
"""系统根路径"""
return {
"message": "企业微信AI客服系统",
"version": "1.0.0",
"status": "running",
"services": {
"database": "✅ 运行中",
"aliyun_ai": "✅ 运行中",
"jira": "✅ 运行中",
"wechat": "✅ 运行中"
}
}
@app.get("/health", tags=["系统信息"])
async def health_check():
"""健康检查"""
return {
"status": "healthy",
"timestamp": "2025-09-22T10:00:00Z",
"services": {
"database": "ok",
"cache": "ok",
"external_apis": "ok"
}
}
@app.get("/info", tags=["系统信息"])
async def system_info():
"""系统信息"""
return {
"system": "企业微信AI客服系统",
"version": "1.0.0",
"author": "JLH",
"email": "joerggis1024@gmail.com",
"architecture": "微服务架构",
"components": [
"数据库服务 (PostgreSQL + SQLAlchemy)",
"阿里云AI服务 (百炼平台 + OSS)",
"JIRA集成服务 (工单管理 + Webhook)",
"企业微信服务 (消息处理 + 用户管理)"
],
"technologies": [
"FastAPI", "SQLAlchemy 2.0", "Pydantic",
"阿里云百炼", "企业微信API", "JIRA REST API"
]
}
# 注册子服务路由
def register_service_routes():
"""注册各服务的路由"""
try:
# 导入各服务的路由(如果存在)
# 这里可以根据实际的API端点进行导入和注册
pass
except ImportError as e:
logging.warning(f"某些服务路由未找到: {e}")
def main():
"""主函数"""
# 设置日志
setup_logging(config.logging)
# 注册服务路由
register_service_routes()
# 启动服务
uvicorn.run(
"main:app",
host="0.0.0.0",
port=config.main_service_port or 8000,
reload=config.app_debug,
log_level=config.logging.uvicorn_log_level.lower(),
access_log=True
)
if __name__ == "__main__":
main()