Skip to content

Commit 5691ea9

Browse files
committed
refactor: закрыть замечания Sonar в main, redis и health
- Вынес API v1 префикс в константу - Упростил get_redis до синхронной зависимости - Перевел health dependencies на Annotated
1 parent 69496dd commit 5691ea9

File tree

3 files changed

+12
-8
lines changed

3 files changed

+12
-8
lines changed

app/main.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
from app.routes.tasks import router as tasks_router
1414
from app.telemetry import setup_telemetry, shutdown_telemetry
1515

16+
API_V1_PREFIX = "/api/v1"
17+
1618

1719
@asynccontextmanager
1820
async def lifespan(app: FastAPI):
@@ -42,7 +44,7 @@ async def lifespan(app: FastAPI):
4244
app.add_middleware(RequestIDMiddleware)
4345

4446
app.include_router(metrics_router)
45-
app.include_router(auth_router, prefix="/api/v1")
46-
app.include_router(health_router, prefix="/api/v1")
47-
app.include_router(tasks_router, prefix="/api/v1")
48-
app.include_router(tags_router, prefix="/api/v1")
47+
app.include_router(auth_router, prefix=API_V1_PREFIX)
48+
app.include_router(health_router, prefix=API_V1_PREFIX)
49+
app.include_router(tasks_router, prefix=API_V1_PREFIX)
50+
app.include_router(tags_router, prefix=API_V1_PREFIX)

app/redis.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@
99
)
1010

1111

12-
async def get_redis() -> Redis:
12+
def get_redis() -> Redis:
1313
return redis_client

app/routes/health.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Awaitable, cast
1+
from typing import Annotated, Awaitable, cast
22

33
from fastapi import APIRouter, Depends
44
from redis.asyncio import Redis
@@ -9,15 +9,17 @@
99
from app.redis import get_redis
1010

1111
router = APIRouter(prefix="/health", tags=["health"])
12+
RedisClient = Annotated[Redis, Depends(get_redis)]
13+
DbSession = Annotated[AsyncSession, Depends(get_db)]
1214

1315

1416
@router.get("/redis")
15-
async def redis_health(redis: Redis = Depends(get_redis)) -> dict[str, str]:
17+
async def redis_health(redis: RedisClient) -> dict[str, str]:
1618
ok = await cast(Awaitable[bool], redis.ping())
1719
return {"redis": "ok" if ok else "down"}
1820

1921

2022
@router.get("/db")
21-
async def db_health(session: AsyncSession = Depends(get_db)) -> dict[str, str]:
23+
async def db_health(session: DbSession) -> dict[str, str]:
2224
await session.execute(text("SELECT 1"))
2325
return {"db": "ok"}

0 commit comments

Comments
 (0)