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:
12
backend/app/routers/__init__.py
Normal file
12
backend/app/routers/__init__.py
Normal file
@@ -0,0 +1,12 @@
|
||||
"""API路由"""
|
||||
from .stats import router as stats_router
|
||||
from .logs import router as logs_router
|
||||
from .config import router as config_router
|
||||
from .health import router as health_router
|
||||
|
||||
__all__ = [
|
||||
"stats_router",
|
||||
"logs_router",
|
||||
"config_router",
|
||||
"health_router"
|
||||
]
|
||||
49
backend/app/routers/config.py
Normal file
49
backend/app/routers/config.py
Normal file
@@ -0,0 +1,49 @@
|
||||
"""配置路由"""
|
||||
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
|
||||
)
|
||||
17
backend/app/routers/health.py
Normal file
17
backend/app/routers/health.py
Normal file
@@ -0,0 +1,17 @@
|
||||
"""健康检查路由"""
|
||||
from fastapi import APIRouter
|
||||
|
||||
from ..config import get_settings
|
||||
|
||||
router = APIRouter(tags=["health"])
|
||||
settings = get_settings()
|
||||
|
||||
|
||||
@router.get("/health")
|
||||
async def health_check():
|
||||
"""健康检查"""
|
||||
return {
|
||||
"status": "ok",
|
||||
"app": settings.APP_NAME,
|
||||
"version": settings.APP_VERSION
|
||||
}
|
||||
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)}
|
||||
45
backend/app/routers/stats.py
Normal file
45
backend/app/routers/stats.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.stats import AICallEvent
|
||||
from ..schemas.stats import AICallEventCreate, AICallEventResponse, BatchReportRequest
|
||||
|
||||
router = APIRouter(prefix="/api/stats", tags=["stats"])
|
||||
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("/report", response_model=AICallEventResponse)
|
||||
async def report_ai_call(
|
||||
event: AICallEventCreate,
|
||||
db: Session = Depends(get_db),
|
||||
_: str = Depends(verify_api_key)
|
||||
):
|
||||
"""上报AI调用事件"""
|
||||
db_event = AICallEvent(**event.model_dump())
|
||||
db.add(db_event)
|
||||
db.commit()
|
||||
db.refresh(db_event)
|
||||
return db_event
|
||||
|
||||
|
||||
@router.post("/report/batch")
|
||||
async def batch_report_ai_calls(
|
||||
request: BatchReportRequest,
|
||||
db: Session = Depends(get_db),
|
||||
_: str = Depends(verify_api_key)
|
||||
):
|
||||
"""批量上报AI调用事件"""
|
||||
events = [AICallEvent(**e.model_dump()) for e in request.events]
|
||||
db.add_all(events)
|
||||
db.commit()
|
||||
return {"success": True, "count": len(events)}
|
||||
Reference in New Issue
Block a user