Browse Source

🎨 [pre-commit.ci] Auto format from pre-commit.com hooks

pull/13907/head
pre-commit-ci[bot] 2 weeks ago
parent
commit
d7f439e757
  1. 2
      .copier/update_dotenv.py
  2. 1
      backend/Dockerfile
  3. 2
      backend/app/api/main.py
  4. 33
      backend/app/api/routes/ip.py
  5. 12
      backend/app/tests/scripts/test_backend_pre_start.py
  6. 12
      backend/app/tests/scripts/test_test_pre_start.py
  7. 1
      hooks/post_gen_project.py

2
.copier/update_dotenv.py

@ -1,5 +1,5 @@
from pathlib import Path
import json import json
from pathlib import Path
# Update the .env file with the answers from the .copier-answers.yml file # Update the .env file with the answers from the .copier-answers.yml file
# without using Jinja2 templates in the .env file, this way the code works as is # without using Jinja2 templates in the .env file, this way the code works as is

1
backend/Dockerfile

@ -42,4 +42,3 @@ RUN --mount=type=cache,target=/root/.cache/uv \
# CMD ["fastapi", "run", "--workers", "1", "app/main.py"] # CMD ["fastapi", "run", "--workers", "1", "app/main.py"]
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000", "--workers", "1", "--proxy-headers", "--forwarded-allow-ips", "*"] CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000", "--workers", "1", "--proxy-headers", "--forwarded-allow-ips", "*"]

2
backend/app/api/main.py

@ -1,6 +1,6 @@
from fastapi import APIRouter from fastapi import APIRouter
from app.api.routes import items, login, private, users, utils, ip from app.api.routes import ip, items, login, private, users, utils
from app.core.config import settings from app.core.config import settings
api_router = APIRouter() api_router = APIRouter()

33
backend/app/api/routes/ip.py

@ -1,6 +1,7 @@
from fastapi import APIRouter, Request, Response, HTTPException
import ipaddress import ipaddress
from typing import Dict, Any from typing import Any
from fastapi import APIRouter, HTTPException, Request, Response
from app.api.deps import CurrentUser from app.api.deps import CurrentUser
@ -57,11 +58,11 @@ async def get_secure_ip(request: Request, current_user: CurrentUser):
return { return {
"ip": client_ip, "ip": client_ip,
"user_id": str(current_user.id), "user_id": str(current_user.id),
"user_email": current_user.email "user_email": current_user.email,
} }
@router.get("/analyze/{ip_address}", response_model=Dict[str, Any]) @router.get("/analyze/{ip_address}", response_model=dict[str, Any])
async def analyze_ip(ip_address: str): async def analyze_ip(ip_address: str):
""" """
分析指定的IP地址 分析指定的IP地址
@ -84,26 +85,26 @@ async def analyze_ip(ip_address: str):
# IPv6特有属性 # IPv6特有属性
if ip.version == 6: if ip.version == 6:
ipv6 = ipaddress.IPv6Address(ip_address) ipv6 = ipaddress.IPv6Address(ip_address)
result.update({ result.update(
"is_site_local": ipv6.is_site_local, {
"ipv4_mapped": ipv6.ipv4_mapped is not None, "is_site_local": ipv6.is_site_local,
"teredo": ipv6.teredo is not None, "ipv4_mapped": ipv6.ipv4_mapped is not None,
"sixtofour": ipv6.sixtofour is not None "teredo": ipv6.teredo is not None,
}) "sixtofour": ipv6.sixtofour is not None,
}
)
# 尝试获取反向DNS查询名称 # 尝试获取反向DNS查询名称
try: try:
import socket import socket
result["reverse_pointer"] = socket.gethostbyaddr(ip_address)[0] result["reverse_pointer"] = socket.gethostbyaddr(ip_address)[0]
except (socket.herror, socket.gaierror): except (socket.herror, socket.gaierror):
result["reverse_pointer"] = None result["reverse_pointer"] = None
return result return result
except ValueError: except ValueError:
raise HTTPException( raise HTTPException(status_code=400, detail=f"无效的IP地址: {ip_address}")
status_code=400,
detail=f"无效的IP地址: {ip_address}"
)
def _extract_client_ip(request: Request) -> str: def _extract_client_ip(request: Request) -> str:
@ -117,8 +118,8 @@ def _extract_client_ip(request: Request) -> str:
"X-Forwarded-For", "X-Forwarded-For",
"X-Real-IP", "X-Real-IP",
"CF-Connecting-IP", # Cloudflare "CF-Connecting-IP", # Cloudflare
"True-Client-IP", # Akamai/Cloudflare "True-Client-IP", # Akamai/Cloudflare
"X-Client-IP" "X-Client-IP",
]: ]:
if header_value := request.headers.get(header): if header_value := request.headers.get(header):
# 如果是X-Forwarded-For,可能包含多个IP,取第一个 # 如果是X-Forwarded-For,可能包含多个IP,取第一个

12
backend/app/tests/scripts/test_backend_pre_start.py

@ -24,10 +24,10 @@ def test_init_successful_connection() -> None:
except Exception: except Exception:
connection_successful = False connection_successful = False
assert ( assert connection_successful, (
connection_successful "The database connection should be successful and not raise an exception."
), "The database connection should be successful and not raise an exception." )
assert session_mock.exec.called_once_with( assert session_mock.exec.called_once_with(select(1)), (
select(1) "The session should execute a select statement once."
), "The session should execute a select statement once." )

12
backend/app/tests/scripts/test_test_pre_start.py

@ -24,10 +24,10 @@ def test_init_successful_connection() -> None:
except Exception: except Exception:
connection_successful = False connection_successful = False
assert ( assert connection_successful, (
connection_successful "The database connection should be successful and not raise an exception."
), "The database connection should be successful and not raise an exception." )
assert session_mock.exec.called_once_with( assert session_mock.exec.called_once_with(select(1)), (
select(1) "The session should execute a select statement once."
), "The session should execute a select statement once." )

1
hooks/post_gen_project.py

@ -1,6 +1,5 @@
from pathlib import Path from pathlib import Path
path: Path path: Path
for path in Path(".").glob("**/*.sh"): for path in Path(".").glob("**/*.sh"):
data = path.read_bytes() data = path.read_bytes()

Loading…
Cancel
Save