-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
180 lines (155 loc) · 5.25 KB
/
main.py
File metadata and controls
180 lines (155 loc) · 5.25 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
"""
MaiMBot API Server 主入口文件
独立后端服务,提供多租户AI聊天机器人配置和管理功能
"""
import time
import os
from dotenv import load_dotenv
load_dotenv()
print(f"DEBUG MAIMCONFIG DB URL: {os.getenv('DATABASE_URL')}", flush=True)
from contextlib import asynccontextmanager
from fastapi import FastAPI, HTTPException, status
from fastapi.middleware.cors import CORSMiddleware
from src.api.routes import (
tenant_router,
agent_router,
api_key_router,
auth_router,
active_state_router,
plugin_router,
plugin_router,
usage_router,
system_router,
)
from src.database.connection import init_database, close_database
from src.database.models import create_tables
from src.common.logger import get_logger
logger = get_logger(__name__)
@asynccontextmanager
async def lifespan(app: FastAPI):
"""
应用生命周期管理
"""
# 启动时执行
logger.info("MaiMBot API Server 正在启动...")
try:
# 初始化数据库连接
init_database()
logger.info("数据库连接初始化完成")
# 创建数据库表
await create_tables()
logger.info("数据库表创建完成")
logger.info("MaiMBot API Server 启动完成")
except Exception as e:
logger.error(f"服务启动失败: {e}")
raise
yield
# 关闭时执行
logger.info("MaiMBot API Server 正在关闭...")
try:
# 关闭数据库连接
close_database()
logger.info("数据库连接已关闭")
except Exception as e:
logger.error(f"关闭数据库连接失败: {e}")
def create_app() -> FastAPI:
"""
创建FastAPI应用实例
"""
app = FastAPI(
title="MaiMBot API",
description="多租户AI聊天机器人配置和管理API服务",
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=["*"],
)
# 注册API路由
app.include_router(tenant_router, prefix="/api/v2", tags=["租户管理"])
app.include_router(agent_router, prefix="/api/v2", tags=["Agent管理"])
app.include_router(api_key_router, prefix="/api/v2", tags=["API密钥管理"])
app.include_router(auth_router, prefix="/api/v2", tags=["API密钥认证"])
app.include_router(active_state_router, prefix="/api/v2", tags=["Agent活跃状态"])
app.include_router(plugin_router, prefix="/api/v1", tags=["插件配置管理"])
app.include_router(usage_router, prefix="/api/v1", tags=["使用日志"])
app.include_router(system_router, prefix="/api/v2", tags=["系统配置"])
# 根路径
@app.get("/")
async def root():
"""API根路径"""
return {
"message": "MaiMBot API Server",
"version": "1.0.0",
"description": "多租户AI聊天机器人配置和管理API服务",
"endpoints": {
"tenants": "/api/v2/tenants",
"agents": "/api/v2/agents",
"api_keys": "/api/v2/api-keys",
"auth": "/api/v2/auth",
"docs": "/docs",
"redoc": "/redoc",
},
"features": [
"多租户隔离",
"Agent配置管理",
"API密钥管理",
"API密钥认证",
"内部服务架构",
],
}
# 健康检查
@app.get("/health")
async def health_check():
"""健康检查接口"""
try:
return {
"status": "healthy",
"timestamp": time.time(),
"version": "1.0.0",
"services": {"database": "healthy", "api": "healthy"},
}
except Exception as e:
logger.error(f"健康检查失败: {e}")
raise HTTPException(
status_code=status.HTTP_503_SERVICE_UNAVAILABLE, detail="服务不健康"
) from None
# API信息
@app.get("/info")
async def api_info():
"""API信息接口"""
return {
"name": "MaiMBot API",
"description": "多租户AI聊天机器人配置和管理API服务",
"version": "1.0.0",
"architecture": {
"isolation_levels": ["tenant", "agent", "platform"],
"auth_mode": "internal_service",
"database": "mysql",
},
"supported_features": [
"租户管理",
"Agent配置管理",
"API密钥管理",
"API密钥认证",
"多租户隔离",
],
}
return app
# 创建应用实例
app = create_app()
if __name__ == "__main__":
import uvicorn
# 从环境变量获取端口,默认8000
port = int(os.getenv("PORT", "8000"))
host = os.getenv("HOST", "0.0.0.0")
# 开发环境配置
uvicorn.run("main:app", host=host, port=port, reload=True, log_level="info", reload_excludes=[".git", ".venv", ".idea", "*.log", "*.log.*", "__pycache__", "*.pyc"])