Skip to content

Commit 5006323

Browse files
committed
update:healthy接口增加访问密钥
1 parent 8df11ea commit 5006323

File tree

3 files changed

+26
-50
lines changed

3 files changed

+26
-50
lines changed

app/api/v1/health.py

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
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
33
from ...core.logging import get_logger
4+
from ...core.config import settings
45

56
logger = get_logger(__name__)
67

@@ -10,16 +11,32 @@
1011
@router.get(
1112
"/health",
1213
summary="健康检查",
13-
response_model=HealthResponse,
14-
description="检查服务运行状态",
14+
response_model=dict,
15+
description="检查服务运行状态,需要提供正确的密钥",
1516
)
16-
async def health_check():
17+
async def health_check(
18+
key: str = Query(..., description="访问密钥", example="your-secret-key")
19+
):
1720
"""
1821
健康检查接口
1922
23+
Args:
24+
key: 访问密钥,必须与配置中的authorization密钥匹配
25+
2026
Returns:
21-
HealthResponse: 服务状态信息
27+
dict: 服务状态信息
28+
29+
Raises:
30+
HTTPException: 当密钥不正确时返回401错误
2231
"""
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)}")

app/api/v1/voiceprint.py

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -143,29 +143,3 @@ async def delete_voiceprint(
143143
except Exception as e:
144144
logger.error(f"删除声纹异常 {speaker_id}: {e}")
145145
raise HTTPException(status_code=500, detail=f"删除声纹失败: {str(e)}")
146-
147-
148-
@router.get(
149-
"/stats",
150-
summary="获取统计信息",
151-
description="获取声纹数据库统计信息",
152-
dependencies=[Depends(security)],
153-
)
154-
async def get_stats(
155-
token: AuthorizationToken,
156-
):
157-
"""
158-
获取统计信息接口
159-
160-
Args:
161-
token: 接口令牌(Header)
162-
163-
Returns:
164-
dict: 统计信息
165-
"""
166-
try:
167-
count = voiceprint_service.get_voiceprint_count()
168-
return {"total_voiceprints": count, "status": "healthy"}
169-
except Exception as e:
170-
logger.error(f"获取统计信息异常: {e}")
171-
raise HTTPException(status_code=500, detail=f"获取统计信息失败: {str(e)}")

app/models/voiceprint.py

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,3 @@ class VoiceprintIdentifyResponse(BaseModel):
3838

3939
class Config:
4040
schema_extra = {"example": {"speaker_id": "user_001", "score": 0.85}}
41-
42-
43-
class HealthResponse(BaseModel):
44-
"""健康检查响应模型"""
45-
46-
status: str
47-
message: str
48-
49-
class Config:
50-
schema_extra = {
51-
"example": {
52-
"status": "healthy",
53-
"message": "3D-Speaker voiceprint API service running.",
54-
}
55-
}

0 commit comments

Comments
 (0)