Files
grok2api/app/api/v1/admin_api/config.py
T

59 lines
1.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import os
from fastapi import APIRouter, Depends, HTTPException
from app.core.auth import verify_app_key
from app.core.config import config
from app.core.storage import (
get_storage as get_storage_backend,
LocalStorage,
RedisStorage,
SQLStorage,
)
router = APIRouter()
@router.get("/verify", dependencies=[Depends(verify_app_key)])
async def admin_verify():
"""验证后台访问密钥(app_key)"""
return {"status": "success"}
@router.get("/config", dependencies=[Depends(verify_app_key)])
async def get_config():
"""获取当前配置"""
# 暴露原始配置字典
return config._config
@router.post("/config", dependencies=[Depends(verify_app_key)])
async def update_config(data: dict):
"""更新配置"""
try:
await config.update(data)
return {"status": "success", "message": "配置已更新"}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@router.get("/storage", dependencies=[Depends(verify_app_key)])
async def admin_get_storage():
"""获取当前存储模式"""
storage_type = os.getenv("SERVER_STORAGE_TYPE", "").lower()
if not storage_type:
storage = get_storage_backend()
if isinstance(storage, LocalStorage):
storage_type = "local"
elif isinstance(storage, RedisStorage):
storage_type = "redis"
elif isinstance(storage, SQLStorage):
storage_type = {
"mysql": "mysql",
"mariadb": "mysql",
"postgres": "pgsql",
"postgresql": "pgsql",
"pgsql": "pgsql",
}.get(storage.dialect, storage.dialect)
return {"type": storage_type or "local"}