11import os
2- from collections import deque
2+ import aiofiles
33from pathlib import Path
44
55import yaml
@@ -30,7 +30,7 @@ async def health_check():
3030
3131
3232@system .get ("/config" )
33- def get_config (current_user : User = Depends (get_admin_user )):
33+ async def get_config (current_user : User = Depends (get_admin_user )):
3434 """获取系统配置"""
3535 return config .dump_config ()
3636
@@ -52,15 +52,20 @@ async def update_config_batch(items: dict = Body(...), current_user: User = Depe
5252
5353
5454@system .get ("/logs" )
55- def get_system_logs (current_user : User = Depends (get_admin_user )):
55+ async def get_system_logs (current_user : User = Depends (get_admin_user )):
5656 """获取系统日志"""
5757 try :
5858 from src .utils .logging_config import LOG_FILE
5959
60- with open (LOG_FILE ) as f :
61- last_lines = deque (f , maxlen = 1000 )
60+ async with aiofiles .open (LOG_FILE ) as f :
61+ # 读取最后1000行
62+ lines = []
63+ async for line in f :
64+ lines .append (line )
65+ if len (lines ) > 1000 :
66+ lines .pop (0 )
6267
63- log = "" .join (last_lines )
68+ log = "" .join (lines )
6469 return {"log" : log , "message" : "success" , "log_file" : LOG_FILE }
6570 except Exception as e :
6671 logger .error (f"获取系统日志失败: { e } " )
@@ -72,7 +77,7 @@ def get_system_logs(current_user: User = Depends(get_admin_user)):
7277# =============================================================================
7378
7479
75- def load_info_config ():
80+ async def load_info_config ():
7681 """加载信息配置文件"""
7782 try :
7883 # 配置文件路径
@@ -84,9 +89,10 @@ def load_info_config():
8489 logger .debug (f"The config file { config_path } does not exist, using default config" )
8590 config_path = Path ("src/config/static/info.template.yaml" )
8691
87- # 读取配置文件
88- with open (config_path , encoding = "utf-8" ) as file :
89- config = yaml .safe_load (file )
92+ # 异步读取配置文件
93+ async with aiofiles .open (config_path , encoding = "utf-8" ) as file :
94+ content = await file .read ()
95+ config = yaml .safe_load (content )
9096
9197 return config
9298
@@ -99,7 +105,7 @@ def load_info_config():
99105async def get_info_config ():
100106 """获取系统信息配置(公开接口,无需认证)"""
101107 try :
102- config = load_info_config ()
108+ config = await load_info_config ()
103109 return {"success" : True , "data" : config }
104110 except Exception as e :
105111 logger .error (f"获取信息配置失败: { e } " )
@@ -110,7 +116,7 @@ async def get_info_config():
110116async def reload_info_config (current_user : User = Depends (get_admin_user )):
111117 """重新加载信息配置"""
112118 try :
113- config = load_info_config ()
119+ config = await load_info_config ()
114120 return {"success" : True , "message" : "配置重新加载成功" , "data" : config }
115121 except Exception as e :
116122 logger .error (f"重新加载信息配置失败: { e } " )
0 commit comments