50 lines
1.5 KiB
Python
50 lines
1.5 KiB
Python
"""配置路由"""
|
|
from typing import Optional
|
|
from fastapi import APIRouter, Depends, Header, HTTPException, Query
|
|
from sqlalchemy.orm import Session
|
|
|
|
from ..database import get_db
|
|
from ..config import get_settings
|
|
from ..models.tenant import Config
|
|
from ..schemas.config import ConfigValue, ConfigWrite
|
|
from ..services.crypto import decrypt_value
|
|
|
|
router = APIRouter(prefix="/api/config", tags=["config"])
|
|
settings = get_settings()
|
|
|
|
|
|
def verify_api_key(x_api_key: str = Header(..., alias="X-API-Key")):
|
|
"""验证API Key"""
|
|
if x_api_key != settings.API_KEY:
|
|
raise HTTPException(status_code=401, detail="Invalid API Key")
|
|
return x_api_key
|
|
|
|
|
|
@router.get("/{config_type}/{config_key}", response_model=ConfigValue)
|
|
async def get_config(
|
|
config_type: str,
|
|
config_key: str,
|
|
tenant_id: int = Query(..., description="租户ID"),
|
|
db: Session = Depends(get_db),
|
|
_: str = Depends(verify_api_key)
|
|
):
|
|
"""读取配置"""
|
|
config = db.query(Config).filter(
|
|
Config.tenant_id == tenant_id,
|
|
Config.config_type == config_type,
|
|
Config.config_key == config_key
|
|
).first()
|
|
|
|
if not config:
|
|
raise HTTPException(status_code=404, detail="Config not found")
|
|
|
|
value = config.config_value
|
|
if config.is_encrypted:
|
|
value = decrypt_value(value)
|
|
|
|
return ConfigValue(
|
|
config_type=config.config_type,
|
|
config_key=config.config_key,
|
|
config_value=value
|
|
)
|