Coverage for app / main.py: 69%

26 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-03-21 22:41 +0300

1from collections.abc import AsyncIterator 

2from contextlib import asynccontextmanager 

3 

4from fastapi import FastAPI 

5from fastapi_cache import FastAPICache 

6from fastapi_cache.backends.redis import RedisBackend 

7from redis import asyncio as aioredis 

8 

9from app.core.config import settings 

10from app.core.tasks import delete_expired_links, scheduler 

11from app.routers import auth_router, link_router 

12 

13 

14@asynccontextmanager 

15async def lifespan(_: FastAPI) -> AsyncIterator[None]: 

16 redis = aioredis.from_url(settings.redis_url) 

17 FastAPICache.init(RedisBackend(redis), prefix="fastapi-cache") 

18 

19 # проверка наличия просроченных ссылок в базе данных 

20 # (с периодичностью 1 минута) и их удаление 

21 scheduler.add_job(delete_expired_links, "interval", minutes=1) 

22 scheduler.start() 

23 yield 

24 scheduler.shutdown() 

25 

26 

27app = FastAPI(title="Link Shortener API", version="1.0.0", lifespan=lifespan) 

28 

29app.include_router(auth_router.router) 

30app.include_router(link_router.router) 

31 

32 

33@app.get("/") 

34async def root(): 

35 return {"msg": "Welcome to the Link Shortener API"} 

36 

37 

38@app.get("/health") 

39async def health_check(): 

40 return {"status": "healthy"}