You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
131 lines
4.0 KiB
131 lines
4.0 KiB
|
2 months ago
|
"""
|
||
|
|
AmazingData 数据服务平台 - 系统配置 API
|
||
|
|
"""
|
||
|
|
|
||
|
|
from fastapi import APIRouter, Depends, HTTPException
|
||
|
|
from sqlalchemy.orm import Session
|
||
|
|
from typing import Optional, Dict, Any
|
||
|
|
from backend.models.database import get_db
|
||
|
|
from backend.models.schemas import BaseResponse, ConfigItem, ConfigUpdateRequest, TestConnectionResponse
|
||
|
|
from backend.models.tables import SystemConfig, User
|
||
|
|
from backend.auth.dependencies import get_current_user, require_admin
|
||
|
|
from backend.services.config_service import config_service
|
||
|
|
from backend.services.data_service import data_service
|
||
|
|
|
||
|
|
router = APIRouter()
|
||
|
|
|
||
|
|
|
||
|
|
@router.get("/list", response_model=BaseResponse)
|
||
|
|
async def list_configs(
|
||
|
|
db: Session = Depends(get_db),
|
||
|
|
current_user: Optional[User] = Depends(get_current_user)
|
||
|
|
):
|
||
|
|
"""获取所有配置"""
|
||
|
|
configs = config_service.get_all_configs(db)
|
||
|
|
|
||
|
|
return BaseResponse(data={
|
||
|
|
"configs": [
|
||
|
|
{
|
||
|
|
"id": c.id,
|
||
|
|
"config_key": c.config_key,
|
||
|
|
"config_value": c.config_value,
|
||
|
|
"config_type": c.config_type,
|
||
|
|
"description": c.description
|
||
|
|
}
|
||
|
|
for c in configs
|
||
|
|
],
|
||
|
|
"total": len(configs)
|
||
|
|
})
|
||
|
|
|
||
|
|
|
||
|
|
@router.get("/{key}", response_model=BaseResponse)
|
||
|
|
async def get_config(
|
||
|
|
key: str,
|
||
|
|
db: Session = Depends(get_db),
|
||
|
|
current_user: Optional[User] = Depends(get_current_user)
|
||
|
|
):
|
||
|
|
"""获取单个配置"""
|
||
|
|
config = config_service.get_config(db, key)
|
||
|
|
if not config:
|
||
|
|
raise HTTPException(status_code=404, detail="Config not found")
|
||
|
|
|
||
|
|
return BaseResponse(data={
|
||
|
|
"id": config.id,
|
||
|
|
"config_key": config.config_key,
|
||
|
|
"config_value": config.config_value,
|
||
|
|
"config_type": config.config_type,
|
||
|
|
"description": config.description
|
||
|
|
})
|
||
|
|
|
||
|
|
|
||
|
|
@router.put("/{key}", response_model=BaseResponse)
|
||
|
|
async def update_config(
|
||
|
|
key: str,
|
||
|
|
request: ConfigUpdateRequest,
|
||
|
|
db: Session = Depends(get_db),
|
||
|
|
current_user: User = Depends(require_admin)
|
||
|
|
):
|
||
|
|
"""更新配置(需要管理员权限)"""
|
||
|
|
config = config_service.update_config(db, key, request.config_value)
|
||
|
|
if not config:
|
||
|
|
raise HTTPException(status_code=404, detail="Config not found")
|
||
|
|
|
||
|
|
return BaseResponse(data={
|
||
|
|
"id": config.id,
|
||
|
|
"config_key": config.config_key,
|
||
|
|
"config_value": config.config_value,
|
||
|
|
"config_type": config.config_type,
|
||
|
|
"description": config.description
|
||
|
|
})
|
||
|
|
|
||
|
|
|
||
|
|
@router.put("/batch", response_model=BaseResponse)
|
||
|
|
async def batch_update_configs(
|
||
|
|
configs: Dict[str, str],
|
||
|
|
db: Session = Depends(get_db),
|
||
|
|
current_user: User = Depends(require_admin)
|
||
|
|
):
|
||
|
|
"""批量更新配置(需要管理员权限)"""
|
||
|
|
success = config_service.batch_update_configs(db, configs)
|
||
|
|
if not success:
|
||
|
|
return BaseResponse(code=500, message="Batch update failed")
|
||
|
|
|
||
|
|
return BaseResponse(message="Batch update successful")
|
||
|
|
|
||
|
|
|
||
|
|
@router.get("/amazing-data/config", response_model=BaseResponse)
|
||
|
|
async def get_amazing_data_config(
|
||
|
|
db: Session = Depends(get_db),
|
||
|
|
current_user: Optional[User] = Depends(get_current_user)
|
||
|
|
):
|
||
|
|
"""获取 AmazingData 连接配置"""
|
||
|
|
config = config_service.get_amazing_data_config(db)
|
||
|
|
# 隐藏密码
|
||
|
|
config["password"] = "***"
|
||
|
|
return BaseResponse(data=config)
|
||
|
|
|
||
|
|
|
||
|
|
@router.post("/test-connection", response_model=TestConnectionResponse)
|
||
|
|
async def test_connection(
|
||
|
|
current_user: Optional[User] = Depends(get_current_user)
|
||
|
|
):
|
||
|
|
"""测试 AmazingData 连接"""
|
||
|
|
result = data_service.test_connection()
|
||
|
|
return TestConnectionResponse(**result)
|
||
|
|
|
||
|
|
|
||
|
|
@router.get("/system/info", response_model=BaseResponse)
|
||
|
|
async def get_system_info(
|
||
|
|
current_user: Optional[User] = Depends(get_current_user)
|
||
|
|
):
|
||
|
|
"""获取系统信息"""
|
||
|
|
import platform
|
||
|
|
import sys
|
||
|
|
|
||
|
|
return BaseResponse(data={
|
||
|
|
"platform": platform.system(),
|
||
|
|
"platform_version": platform.version(),
|
||
|
|
"python_version": platform.python_version(),
|
||
|
|
"app_env": "development",
|
||
|
|
"app_name": "AmazingData Platform"
|
||
|
|
})
|