from fastapi import FastAPI, status, Depends from fastapi.responses import RedirectResponse from app.shemas import SURL, SNewUrl, SGetStats from app.r import get_redis_client from app.service import get_visit_times, add_visit, get_original_url, add_url app = FastAPI( title="Сокращатель ссылок", ) @app.post("/zip_url", response_model=SNewUrl) async def zip_url(url: SURL): url = str(url.url) return await add_url(url) @app.get("/{url_hash}", response_class=RedirectResponse, status_code=status.HTTP_307_TEMPORARY_REDIRECT) async def redirect(url_hash: int, r=Depends(get_redis_client)): url = await get_original_url(url_hash, r) list_key = f"visit_times:{url_hash}" await add_visit(list_key, r) return RedirectResponse(url=url) @app.get("/stats/{url_hash}", response_model=SGetStats) async def get_stats(url_hash: int, r=Depends(get_redis_client)): list_key = f"visit_times:{url_hash}" return await get_visit_times(list_key, r)