From ed49bb17b9e93a1406ab51f7dca5906661863627 Mon Sep 17 00:00:00 2001 From: Stas Medvedev Date: Wed, 12 Jun 2024 16:05:12 +0300 Subject: refactore utils refactore html about, client_geo --- .vscode/launch.json | 20 ++++++++++++++++++-- app/main.py | 18 ++++++++++++------ app/utils.py | 32 -------------------------------- static/style.css | 23 ++++++++++++++--------- templates/partials/about.html | 37 ++++++++++++++----------------------- templates/partials/avatars.html | 2 +- templates/partials/client_geo.html | 18 ++++++++++++++++++ utils/__init__.py | 32 ++++++++++++++++++++++++++++++++ 8 files changed, 109 insertions(+), 73 deletions(-) delete mode 100644 app/utils.py create mode 100644 templates/partials/client_geo.html create mode 100644 utils/__init__.py diff --git a/.vscode/launch.json b/.vscode/launch.json index a24f6a9..4e95206 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -4,13 +4,29 @@ // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ + { + "name": "Python Debugger: Current File", + "type": "debugpy", + "request": "launch", + "program": "${file}", + "console": "integratedTerminal", + "justMyCode": false + }, { "name": "uvicorn", "type": "debugpy", "request": "launch", "module": "uvicorn", - "args": ["app.main:app", "--host", "0.0.0.0", "--log-config", "./log_config.json"], + "args": [ + "app.main:app", + "--host", + "0.0.0.0", + "--log-config", + "./log_config.json", + "--workers", + "1" + ], "console": "integratedTerminal" } ] -} \ No newline at end of file +} diff --git a/app/main.py b/app/main.py index 7cf756c..59e7a8b 100644 --- a/app/main.py +++ b/app/main.py @@ -5,8 +5,7 @@ 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 -from app.logging import logger +from utils import get_avatar_urls, get_client_geo templates = Jinja2Templates(directory="templates") @@ -17,13 +16,20 @@ app.mount("/static", StaticFiles(directory="static"), name="static") @app.get("/", response_class=HTMLResponse) async def index( request: Request, - client_geo: Annotated[dict, Depends(get_client_geo)], avatar_urls: Annotated[list[str], Depends(get_avatar_urls)], ): + return templates.TemplateResponse( + "index.html", + {"request": request, "avatar_urls": avatar_urls}, + ) - logger.info(str(dict(request.headers))) +@app.get("/client_geo", response_class=HTMLResponse) +async def client_addr( + request: Request, + client_geo: Annotated[dict, Depends(get_client_geo)], +): return templates.TemplateResponse( - "index.html", - {"request": request, "client_geo": client_geo, "avatar_urls": avatar_urls}, + "partials/client_geo.html", + {"request": request, "client_geo": client_geo}, ) diff --git a/app/utils.py b/app/utils.py deleted file mode 100644 index 22d7058..0000000 --- a/app/utils.py +++ /dev/null @@ -1,32 +0,0 @@ -from pathlib import Path -from typing import Annotated - -from fastapi import Request, Header, Depends -import httpx - - -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")]) - - -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: 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"}, - ) - data = response.json() - return data diff --git a/static/style.css b/static/style.css index 70482a5..500a6e4 100644 --- a/static/style.css +++ b/static/style.css @@ -673,11 +673,6 @@ div { margin-bottom: 2.5rem; } -.my-2 { - margin-top: 0.5rem; - margin-bottom: 0.5rem; -} - .ml-6 { margin-left: 1.5rem; } @@ -702,6 +697,14 @@ div { height: 100%; } +.min-h-20 { + min-height: 5rem; +} + +.min-h-16 { + min-height: 4rem; +} + .w-full { width: 100%; } @@ -714,6 +717,12 @@ div { flex-grow: 1; } +.select-none { + -webkit-user-select: none; + -moz-user-select: none; + user-select: none; +} + .list-outside { list-style-position: outside; } @@ -722,10 +731,6 @@ div { list-style-type: disc; } -.flex-row { - flex-direction: row; -} - .flex-col { flex-direction: column; } diff --git a/templates/partials/about.html b/templates/partials/about.html index b9dc26d..238d895 100644 --- a/templates/partials/about.html +++ b/templates/partials/about.html @@ -1,10 +1,15 @@ -
+ diff --git a/templates/partials/avatars.html b/templates/partials/avatars.html index a6f84ff..4a5c05f 100644 --- a/templates/partials/avatars.html +++ b/templates/partials/avatars.html @@ -1,5 +1,5 @@
-
+
{% for url in avatar_urls %} {% endfor %} diff --git a/templates/partials/client_geo.html b/templates/partials/client_geo.html new file mode 100644 index 0000000..fac4371 --- /dev/null +++ b/templates/partials/client_geo.html @@ -0,0 +1,18 @@ +
+

Уже вычислил тебя по ip:

+ + + {% if 'city' in client_geo %} +

{{client_geo['country']}}

+

{{client_geo['city']}}

+ {% endif %} +
diff --git a/utils/__init__.py b/utils/__init__.py new file mode 100644 index 0000000..22d7058 --- /dev/null +++ b/utils/__init__.py @@ -0,0 +1,32 @@ +from pathlib import Path +from typing import Annotated + +from fastapi import Request, Header, Depends +import httpx + + +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")]) + + +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: 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"}, + ) + data = response.json() + return data -- cgit v1.2.3