summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.vscode/launch.json16
-rw-r--r--app/main.py28
-rw-r--r--templates/index.html8
3 files changed, 49 insertions, 3 deletions
diff --git a/.vscode/launch.json b/.vscode/launch.json
new file mode 100644
index 0000000..f85c840
--- /dev/null
+++ b/.vscode/launch.json
@@ -0,0 +1,16 @@
+{
+ // Use IntelliSense to learn about possible attributes.
+ // Hover to view descriptions of existing attributes.
+ // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
+ "version": "0.2.0",
+ "configurations": [
+ {
+ "name": "uvicorn",
+ "type": "debugpy",
+ "request": "launch",
+ "module": "uvicorn",
+ "args": ["app.main:app", "--host", "0.0.0.0"],
+ "console": "integratedTerminal"
+ }
+ ]
+} \ No newline at end of file
diff --git a/app/main.py b/app/main.py
index 1c4590e..d9ea01c 100644
--- a/app/main.py
+++ b/app/main.py
@@ -1,12 +1,36 @@
from fastapi import FastAPI, Request
from fastapi.responses import HTMLResponse
from starlette.templating import Jinja2Templates
+import httpx
app = FastAPI()
templates = Jinja2Templates(directory="templates")
+def get_client_host(request: Request):
+ return request.client.host
+
+
+async def get_client_geo(client_host: str):
+ 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
+
+
@app.get("/", response_class=HTMLResponse)
-async def read_root(request: Request):
- return templates.TemplateResponse("index.html", {"request": request})
+async def index(request: Request):
+ return templates.TemplateResponse(
+ "index.html",
+ {
+ "request": request,
+ "client_geo": await get_client_geo(
+ get_client_host(request)
+ )
+ }
+ )
diff --git a/templates/index.html b/templates/index.html
index c254629..a2e6d0b 100644
--- a/templates/index.html
+++ b/templates/index.html
@@ -3,9 +3,15 @@
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
+ <script src="https://cdn.tailwindcss.com"></script>
<title>Document</title>
</head>
<body>
- Привет мир
+ Привет
+ {% if 'city' in client_geo %}
+ <p>{{client_geo['country']}}</p>
+ <p>{{client_geo['city']}}</p>
+ <p>{{client_geo['query']}}</p>
+ {% endif %}
</body>
</html> \ No newline at end of file