feat: 重构API服务器,添加配置重载功能并移除冗余代码
This commit is contained in:
@@ -1,8 +0,0 @@
|
||||
from fastapi import FastAPI
|
||||
from strawberry.fastapi import GraphQLRouter
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
graphql_router = GraphQLRouter(schema=None, path="/") # Replace `None` with your actual schema
|
||||
|
||||
app.include_router(graphql_router, prefix="/graphql", tags=["GraphQL"])
|
||||
|
||||
28
src/api/main.py
Normal file
28
src/api/main.py
Normal file
@@ -0,0 +1,28 @@
|
||||
from fastapi import APIRouter
|
||||
from strawberry.fastapi import GraphQLRouter
|
||||
# from src.config.config import BotConfig
|
||||
from src.common.logger_manager import get_logger
|
||||
from src.api.reload_config import reload_config as reload_config_func
|
||||
from src.common.server import global_server
|
||||
# import uvicorn
|
||||
# import os
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
logger = get_logger("api")
|
||||
|
||||
# maiapi = FastAPI()
|
||||
logger.info("API server started.")
|
||||
graphql_router = GraphQLRouter(schema=None, path="/") # Replace `None` with your actual schema
|
||||
|
||||
router.include_router(graphql_router, prefix="/graphql", tags=["GraphQL"])
|
||||
|
||||
@router.post("/config/reload")
|
||||
async def reload_config():
|
||||
return await reload_config_func()
|
||||
|
||||
def start_api_server():
|
||||
"""启动API服务器"""
|
||||
global_server.register_router(router, prefix="/api/v1")
|
||||
|
||||
18
src/api/reload_config.py
Normal file
18
src/api/reload_config.py
Normal file
@@ -0,0 +1,18 @@
|
||||
from fastapi import HTTPException
|
||||
from rich.traceback import install
|
||||
from src.config.config import BotConfig
|
||||
import os
|
||||
install(extra_lines=3)
|
||||
|
||||
|
||||
|
||||
async def reload_config():
|
||||
try:
|
||||
from src.config import config as config_module
|
||||
bot_config_path = os.path.join(BotConfig.get_config_dir(), "bot_config.toml")
|
||||
config_module.global_config = BotConfig.load_config(config_path=bot_config_path)
|
||||
return {"status": "reloaded"}
|
||||
except FileNotFoundError as e:
|
||||
raise HTTPException(status_code=404, detail=str(e)) from e
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=500, detail=f"重载配置时发生错误: {str(e)}") from e
|
||||
@@ -1,5 +1,6 @@
|
||||
import asyncio
|
||||
import time
|
||||
import os
|
||||
from .plugins.utils.statistic import LLMStatistics
|
||||
from .plugins.moods.moods import MoodManager
|
||||
from .plugins.schedule.schedule_generator import bot_schedule
|
||||
@@ -18,6 +19,7 @@ from .plugins.remote import heartbeat_thread # noqa: F401
|
||||
from .individuality.individuality import Individuality
|
||||
from .common.server import global_server
|
||||
from rich.traceback import install
|
||||
from .api.main import start_api_server
|
||||
|
||||
install(extra_lines=3)
|
||||
|
||||
@@ -54,6 +56,9 @@ class MainSystem:
|
||||
self.llm_stats.start()
|
||||
logger.success("LLM统计功能启动成功")
|
||||
|
||||
# 启动API服务器
|
||||
start_api_server()
|
||||
logger.success("API服务器启动成功")
|
||||
# 初始化表情管理器
|
||||
emoji_manager.initialize()
|
||||
logger.success("表情包管理器初始化成功")
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
|
||||
@@ -1,19 +0,0 @@
|
||||
from fastapi import APIRouter, HTTPException
|
||||
from rich.traceback import install
|
||||
|
||||
install(extra_lines=3)
|
||||
|
||||
# 创建APIRouter而不是FastAPI实例
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.post("/reload-config")
|
||||
async def reload_config():
|
||||
try: # TODO: 实现配置重载
|
||||
# bot_config_path = os.path.join(BotConfig.get_config_dir(), "bot_config.toml")
|
||||
# BotConfig.reload_config(config_path=bot_config_path)
|
||||
return {"message": "TODO: 实现配置重载", "status": "unimplemented"}
|
||||
except FileNotFoundError as e:
|
||||
raise HTTPException(status_code=404, detail=str(e)) from e
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=500, detail=f"重载配置时发生错误: {str(e)}") from e
|
||||
@@ -1,4 +0,0 @@
|
||||
import requests
|
||||
|
||||
response = requests.post("http://localhost:8080/api/reload-config")
|
||||
print(response.json())
|
||||
Reference in New Issue
Block a user