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 ++++++++++++++------------- log_config.json | 2 +- static/request.log | 1 - 5 files changed, 27 insertions(+), 27 deletions(-) delete mode 100644 static/request.log 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 - diff --git a/log_config.json b/log_config.json index 3752055..e399c32 100644 --- a/log_config.json +++ b/log_config.json @@ -48,7 +48,7 @@ }, "app.request": { "handlers": [ - "request", "default" + "request" ], "level": "INFO" } diff --git a/static/request.log b/static/request.log deleted file mode 100644 index 4169222..0000000 --- a/static/request.log +++ /dev/null @@ -1 +0,0 @@ -INFO: {'host': 'localhost:8000', 'connection': 'keep-alive', 'sec-ch-ua': '"Google Chrome";v="125", "Chromium";v="125", "Not.A/Brand";v="24"', 'sec-ch-ua-mobile': '?0', 'sec-ch-ua-platform': '"Linux"', 'upgrade-insecure-requests': '1', 'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36', 'sec-purpose': 'prefetch;prerender', 'purpose': 'prefetch', 'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7', 'sec-fetch-site': 'none', 'sec-fetch-mode': 'navigate', 'sec-fetch-user': '?1', 'sec-fetch-dest': 'document', 'accept-encoding': 'gzip, deflate, br, zstd', 'accept-language': 'en-US,en;q=0.9'} -- cgit v1.2.3