add logging

This commit is contained in:
austnv
2026-06-14 16:29:01 +03:00
parent cb86a92246
commit 5dc98f2881
2 changed files with 12 additions and 28 deletions
+3
View File
@@ -1,8 +1,11 @@
from fastapi import FastAPI
import tomllib
import logging
from routers import ip
logging.basicConfig(level=logging.INFO)
def get_version_from_pyproject():
with open("pyproject.toml", "rb") as f:
data = tomllib.load(f)
+9 -28
View File
@@ -6,6 +6,10 @@ from fastapi import APIRouter, Request
from httpx import AsyncClient
import logging
logging.basicConfig(level=logging.INFO)
# ==================== Pydantic модели ====================
@@ -153,41 +157,18 @@ class IPInfo:
# ==================== Роутер ====================
router = APIRouter(prefix='/api/v1', tags=['ip'])
import logging
logging.basicConfig(level=logging.DEBUG)
@router.get('/ip')
async def get_ip(request: Request):
# 1. Проверяем заголовки
forwarded = request.headers.get('X-Forwarded-For')
print(f"[DEBUG] X-Forwarded-For: {forwarded}")
print(f"[DEBUG] All headers: {dict(request.headers)}")
# 2. Определяем IP
if forwarded:
# Берем ПЕРВЫЙ IP из списка (это реальный клиент)
client_ip = forwarded.split(',')[0].strip()
else:
client_ip = request.client.host
print(f"[DEBUG] Client IP: {client_ip}")
# 3. Проверяем запрос к ipinfo.io
async with AsyncClient() as client:
url = f'https://ipinfo.io/{client_ip}'
print(f"[DEBUG] Request URL: {url}")
response = await client.get(url)
print(f"[DEBUG] Response status: {response.status_code}")
print(f"[DEBUG] Response headers: {response.headers}")
print(f"[DEBUG] Response text (first 500 chars): {response.text[:500]}")
response = await client.get(f'https://ipinfo.io/{client_ip}')
html = response.text
ipinfo = IPInfo(html)
print(f"[DEBUG] IPInfo is_valid: {ipinfo.is_valid}")
print(f"[DEBUG] IPInfo._data: {ipinfo._data}")
if ipinfo._data is None:
return {"error": "Parse failed", "ip": client_ip, "raw_html_preview": html[:200]}
return ipinfo.dict()