From fc29744db8eba827802bdb5c5932f91ae79ea152 Mon Sep 17 00:00:00 2001 From: Stas Medvedev Date: Tue, 11 Jun 2024 02:13:28 +0300 Subject: =?UTF-8?q?=D1=80=D0=B5=D0=B0=D0=BB=D1=8C=D0=BD=D1=8B=D0=B9=20ip?= =?UTF-8?q?=20=D0=B8=D0=B7=20=D0=BF=D0=BE=D0=B4=20=D0=BF=D1=80=D0=BE=D0=BA?= =?UTF-8?q?=D1=81=D0=B8=20=D1=85=D0=B5=D0=B4=D0=B5=D1=80=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/logging.py | 4 ++-- app/main.py | 20 ++++++++++---------- app/utils.py | 27 ++++++++++++++------------- 3 files changed, 26 insertions(+), 25 deletions(-) (limited to 'app') diff --git a/app/logging.py b/app/logging.py index e24328d..de5fb56 100644 --- a/app/logging.py +++ b/app/logging.py @@ -3,7 +3,7 @@ import logging class RequestFileHandler(logging.FileHandler): def __init__(self) -> None: - super().__init__('./static/request.log') + super().__init__("./static/request.log") -logger = logging.getLogger('app.request') +logger = logging.getLogger("app.request") diff --git a/app/main.py b/app/main.py index d643447..7cf756c 100644 --- a/app/main.py +++ b/app/main.py @@ -1,9 +1,11 @@ -from fastapi import FastAPI, Request +from typing import Annotated + +from fastapi import FastAPI, Request, Depends from fastapi.responses import HTMLResponse from fastapi.staticfiles import StaticFiles from starlette.templating import Jinja2Templates -from app.utils import get_avatar_urls, get_client_geo, get_client_host +from app.utils import get_avatar_urls, get_client_geo from app.logging import logger templates = Jinja2Templates(directory="templates") @@ -13,17 +15,15 @@ app.mount("/static", StaticFiles(directory="static"), name="static") @app.get("/", response_class=HTMLResponse) -async def index(request: Request): +async def index( + request: Request, + client_geo: Annotated[dict, Depends(get_client_geo)], + avatar_urls: Annotated[list[str], Depends(get_avatar_urls)], +): logger.info(str(dict(request.headers))) return templates.TemplateResponse( "index.html", - { - "request": request, - "client_geo": await get_client_geo( - get_client_host(request) - ), - "avatar_urls": get_avatar_urls() - } + {"request": request, "client_geo": client_geo, "avatar_urls": avatar_urls}, ) diff --git a/app/utils.py b/app/utils.py index 08373e0..22d7058 100644 --- a/app/utils.py +++ b/app/utils.py @@ -1,31 +1,32 @@ from pathlib import Path -from fastapi import Request +from typing import Annotated + +from fastapi import Request, Header, Depends import httpx -def get_avatar_urls(): - path = Path('./static') / 'avatars' +def get_avatar_urls() -> list[str]: + path = Path("./static") / "avatars" if not path.exists(): path.mkdir() - return sorted([ - str(jpg_avatar) - for jpg_avatar - in path.glob('*.jpg') - ]) + return sorted([str(jpg_avatar) for jpg_avatar in path.glob("*.jpg")]) -def get_client_host(request: Request): +def get_client_host( + request: Request, x_real_ip: Annotated[str | None, Header()] = None +) -> str: + if x_real_ip: + return x_real_ip return request.client.host -async def get_client_geo(client_host: str): +async def get_client_geo(client_host: Annotated[str, Depends(get_client_host)]) -> dict: async with httpx.AsyncClient() as client: response = await client.get( # использование https платная опция сервиса, инфо тут https://members.ip-api.com/ - url='http://ip-api.com/json/{}'.format(client_host), - params={"lang": "ru"} + url="http://ip-api.com/json/{}".format(client_host), + params={"lang": "ru"}, ) data = response.json() return data - -- cgit v1.2.3