Initial commit: 000-platform project skeleton
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
45
backend/app/routers/logs.py
Normal file
45
backend/app/routers/logs.py
Normal file
@@ -0,0 +1,45 @@
|
||||
"""日志路由"""
|
||||
from fastapi import APIRouter, Depends, Header, HTTPException
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from ..database import get_db
|
||||
from ..config import get_settings
|
||||
from ..models.logs import PlatformLog
|
||||
from ..schemas.logs import LogCreate, LogResponse, BatchLogRequest
|
||||
|
||||
router = APIRouter(prefix="/api/logs", tags=["logs"])
|
||||
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.post("/write", response_model=LogResponse)
|
||||
async def write_log(
|
||||
log: LogCreate,
|
||||
db: Session = Depends(get_db),
|
||||
_: str = Depends(verify_api_key)
|
||||
):
|
||||
"""写入日志"""
|
||||
db_log = PlatformLog(**log.model_dump())
|
||||
db.add(db_log)
|
||||
db.commit()
|
||||
db.refresh(db_log)
|
||||
return db_log
|
||||
|
||||
|
||||
@router.post("/write/batch")
|
||||
async def batch_write_logs(
|
||||
request: BatchLogRequest,
|
||||
db: Session = Depends(get_db),
|
||||
_: str = Depends(verify_api_key)
|
||||
):
|
||||
"""批量写入日志"""
|
||||
logs = [PlatformLog(**l.model_dump()) for l in request.logs]
|
||||
db.add_all(logs)
|
||||
db.commit()
|
||||
return {"success": True, "count": len(logs)}
|
||||
Reference in New Issue
Block a user