feat: 添加全局配置相关API
This commit is contained in:
@@ -23,5 +23,6 @@ __author__ = "DLmaster361 <DLmaster_361@163.com>"
|
|||||||
__license__ = "GPL-3.0 license"
|
__license__ = "GPL-3.0 license"
|
||||||
|
|
||||||
from .scripts import router as scripts_router
|
from .scripts import router as scripts_router
|
||||||
|
from .setting import router as setting_router
|
||||||
|
|
||||||
__all__ = ["scripts_router"]
|
__all__ = ["scripts_router", "setting_router"]
|
||||||
|
|||||||
49
app/api/setting.py
Normal file
49
app/api/setting.py
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
# AUTO_MAA:A MAA Multi Account Management and Automation Tool
|
||||||
|
# Copyright © 2024-2025 DLmaster361
|
||||||
|
|
||||||
|
# This file is part of AUTO_MAA.
|
||||||
|
|
||||||
|
# AUTO_MAA is free software: you can redistribute it and/or modify
|
||||||
|
# it under the terms of the GNU General Public License as published
|
||||||
|
# by the Free Software Foundation, either version 3 of the License,
|
||||||
|
# or (at your option) any later version.
|
||||||
|
|
||||||
|
# AUTO_MAA is distributed in the hope that it will be useful,
|
||||||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty
|
||||||
|
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
|
||||||
|
# the GNU General Public License for more details.
|
||||||
|
|
||||||
|
# You should have received a copy of the GNU General Public License
|
||||||
|
# along with AUTO_MAA. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
# Contact: DLmaster_361@163.com
|
||||||
|
|
||||||
|
|
||||||
|
from fastapi import APIRouter, Body
|
||||||
|
|
||||||
|
from core import Config
|
||||||
|
from models.schema import *
|
||||||
|
|
||||||
|
router = APIRouter(prefix="/api/setting", tags=["全局设置"])
|
||||||
|
|
||||||
|
|
||||||
|
@router.post("/get", summary="查询配置", response_model=SettingGetOut, status_code=200)
|
||||||
|
async def get_scripts() -> SettingGetOut:
|
||||||
|
"""查询配置"""
|
||||||
|
|
||||||
|
try:
|
||||||
|
data = await Config.get_setting()
|
||||||
|
except Exception as e:
|
||||||
|
return SettingGetOut(code=500, status="error", message=str(e), data={})
|
||||||
|
return SettingGetOut(data=data)
|
||||||
|
|
||||||
|
|
||||||
|
@router.post("/update", summary="更新配置", response_model=BaseOut, status_code=200)
|
||||||
|
async def update_script(script: SettingUpdateIn = Body(...)) -> BaseOut:
|
||||||
|
"""更新配置"""
|
||||||
|
|
||||||
|
try:
|
||||||
|
await Config.update_setting(script.data)
|
||||||
|
except Exception as e:
|
||||||
|
return BaseOut(code=500, status="error", message=str(e))
|
||||||
|
return BaseOut()
|
||||||
@@ -561,7 +561,7 @@ class AppConfig(GlobalConfig):
|
|||||||
VERSION = "4.5.0.1"
|
VERSION = "4.5.0.1"
|
||||||
|
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
super().__init__()
|
super().__init__(if_save_multi_config=False)
|
||||||
|
|
||||||
# self.app_path = Path(sys.argv[0]).resolve().parent
|
# self.app_path = Path(sys.argv[0]).resolve().parent
|
||||||
# self.app_path_sys = Path(sys.argv[0]).resolve()
|
# self.app_path_sys = Path(sys.argv[0]).resolve()
|
||||||
@@ -671,6 +671,23 @@ class AppConfig(GlobalConfig):
|
|||||||
|
|
||||||
await self.ScriptConfig.remove(uuid.UUID(script_id))
|
await self.ScriptConfig.remove(uuid.UUID(script_id))
|
||||||
|
|
||||||
|
async def get_setting(self) -> Dict[str, Any]:
|
||||||
|
"""获取全局设置"""
|
||||||
|
|
||||||
|
self.logger.info("获取全局设置")
|
||||||
|
|
||||||
|
return await self.toDict(ignore_multi_config=True)
|
||||||
|
|
||||||
|
async def update_setting(self, data: Dict[str, Dict[str, Any]]) -> None:
|
||||||
|
"""更新全局设置"""
|
||||||
|
|
||||||
|
self.logger.info(f"更新全局设置")
|
||||||
|
|
||||||
|
for group, items in data.items():
|
||||||
|
for name, value in items.items():
|
||||||
|
self.logger.debug(f"更新全局设置 - {group}.{name} = {value}")
|
||||||
|
await self.set(group, name, value)
|
||||||
|
|
||||||
# def check_data(self) -> None:
|
# def check_data(self) -> None:
|
||||||
# """检查用户数据文件并处理数据文件版本更新"""
|
# """检查用户数据文件并处理数据文件版本更新"""
|
||||||
|
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ from fastapi import FastAPI
|
|||||||
from fastapi.middleware.cors import CORSMiddleware
|
from fastapi.middleware.cors import CORSMiddleware
|
||||||
|
|
||||||
|
|
||||||
from api import scripts_router
|
from api import scripts_router, setting_router
|
||||||
from core import Config
|
from core import Config
|
||||||
from utils import get_logger
|
from utils import get_logger
|
||||||
|
|
||||||
@@ -51,6 +51,7 @@ app.add_middleware(
|
|||||||
|
|
||||||
|
|
||||||
app.include_router(scripts_router)
|
app.include_router(scripts_router)
|
||||||
|
app.include_router(setting_router)
|
||||||
|
|
||||||
|
|
||||||
def is_admin() -> bool:
|
def is_admin() -> bool:
|
||||||
|
|||||||
@@ -188,9 +188,10 @@ class ConfigBase:
|
|||||||
子配置项可以是 `MultipleConfig` 的实例。
|
子配置项可以是 `MultipleConfig` 的实例。
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self, if_save_multi_config: bool = True):
|
||||||
|
|
||||||
self.file: None | Path = None
|
self.file: None | Path = None
|
||||||
|
self.if_save_multi_config = if_save_multi_config
|
||||||
|
|
||||||
async def connect(self, path: Path):
|
async def connect(self, path: Path):
|
||||||
"""
|
"""
|
||||||
@@ -253,7 +254,7 @@ class ConfigBase:
|
|||||||
if self.file:
|
if self.file:
|
||||||
await self.save()
|
await self.save()
|
||||||
|
|
||||||
async def toDict(self) -> Dict[str, Any]:
|
async def toDict(self, ignore_multi_config: bool = False) -> Dict[str, Any]:
|
||||||
"""将配置项转换为字典"""
|
"""将配置项转换为字典"""
|
||||||
|
|
||||||
data = {}
|
data = {}
|
||||||
@@ -267,7 +268,7 @@ class ConfigBase:
|
|||||||
if item.name:
|
if item.name:
|
||||||
data[item.group][item.name] = item.value
|
data[item.group][item.name] = item.value
|
||||||
|
|
||||||
elif isinstance(item, MultipleConfig):
|
elif not ignore_multi_config and isinstance(item, MultipleConfig):
|
||||||
|
|
||||||
if not data.get("SubConfigsInfo"):
|
if not data.get("SubConfigsInfo"):
|
||||||
data["SubConfigsInfo"] = {}
|
data["SubConfigsInfo"] = {}
|
||||||
@@ -326,7 +327,12 @@ class ConfigBase:
|
|||||||
|
|
||||||
self.file.parent.mkdir(parents=True, exist_ok=True)
|
self.file.parent.mkdir(parents=True, exist_ok=True)
|
||||||
with self.file.open("w", encoding="utf-8") as f:
|
with self.file.open("w", encoding="utf-8") as f:
|
||||||
json.dump(await self.toDict(), f, ensure_ascii=False, indent=4)
|
json.dump(
|
||||||
|
await self.toDict(not self.if_save_multi_config),
|
||||||
|
f,
|
||||||
|
ensure_ascii=False,
|
||||||
|
indent=4,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class MultipleConfig:
|
class MultipleConfig:
|
||||||
|
|||||||
@@ -54,3 +54,11 @@ class ScriptUpdateIn(BaseModel):
|
|||||||
|
|
||||||
class ScriptDeleteIn(BaseModel):
|
class ScriptDeleteIn(BaseModel):
|
||||||
scriptId: str = Field(..., description="脚本ID")
|
scriptId: str = Field(..., description="脚本ID")
|
||||||
|
|
||||||
|
|
||||||
|
class SettingGetOut(BaseOut):
|
||||||
|
data: Dict[str, Dict[str, Any]] = Field(..., description="全局设置数据")
|
||||||
|
|
||||||
|
|
||||||
|
class SettingUpdateIn(BaseModel):
|
||||||
|
data: Dict[str, Dict[str, Any]] = Field(..., description="全局设置更新数据")
|
||||||
|
|||||||
Reference in New Issue
Block a user