|
1 |
| -from fastapi import APIRouter |
2 |
| -from ...models.voiceprint import HealthResponse |
| 1 | +from fastapi import APIRouter, HTTPException, Query |
| 2 | +from ...services.voiceprint_service import voiceprint_service |
3 | 3 | from ...core.logging import get_logger
|
| 4 | +from ...core.config import settings |
4 | 5 |
|
5 | 6 | logger = get_logger(__name__)
|
6 | 7 |
|
|
10 | 11 | @router.get(
|
11 | 12 | "/health",
|
12 | 13 | summary="健康检查",
|
13 |
| - response_model=HealthResponse, |
14 |
| - description="检查服务运行状态", |
| 14 | + response_model=dict, |
| 15 | + description="检查服务运行状态,需要提供正确的密钥", |
15 | 16 | )
|
16 |
| -async def health_check(): |
| 17 | +async def health_check( |
| 18 | + key: str = Query(..., description="访问密钥", example="your-secret-key") |
| 19 | +): |
17 | 20 | """
|
18 | 21 | 健康检查接口
|
19 | 22 |
|
| 23 | + Args: |
| 24 | + key: 访问密钥,必须与配置中的authorization密钥匹配 |
| 25 | +
|
20 | 26 | Returns:
|
21 |
| - HealthResponse: 服务状态信息 |
| 27 | + dict: 服务状态信息 |
| 28 | +
|
| 29 | + Raises: |
| 30 | + HTTPException: 当密钥不正确时返回401错误 |
22 | 31 | """
|
23 |
| - return HealthResponse( |
24 |
| - status="healthy", message="3D-Speaker voiceprint API service running." |
25 |
| - ) |
| 32 | + # 验证密钥 |
| 33 | + if key != settings.api_token: |
| 34 | + logger.warning(f"健康检查接口收到无效密钥: {key}") |
| 35 | + raise HTTPException(status_code=401, detail="密钥验证失败") |
| 36 | + |
| 37 | + try: |
| 38 | + count = voiceprint_service.get_voiceprint_count() |
| 39 | + return {"total_voiceprints": count, "status": "healthy"} |
| 40 | + except Exception as e: |
| 41 | + logger.error(f"获取统计信息异常: {e}") |
| 42 | + raise HTTPException(status_code=500, detail=f"获取统计信息失败: {str(e)}") |
0 commit comments