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. 25
      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
from pathlib import Path
# 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

1
backend/Dockerfile

@ -42,4 +42,3 @@ RUN --mount=type=cache,target=/root/.cache/uv \
# 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", "*"]

2
backend/app/api/main.py

@ -1,6 +1,6 @@
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
api_router = APIRouter()

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

@ -1,6 +1,7 @@
from fastapi import APIRouter, Request, Response, HTTPException
import ipaddress
from typing import Dict, Any
from typing import Any
from fastapi import APIRouter, HTTPException, Request, Response
from app.api.deps import CurrentUser
@ -57,11 +58,11 @@ async def get_secure_ip(request: Request, current_user: CurrentUser):
return {
"ip": client_ip,
"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):
"""
分析指定的IP地址
@ -84,26 +85,26 @@ async def analyze_ip(ip_address: str):
# IPv6特有属性
if ip.version == 6:
ipv6 = ipaddress.IPv6Address(ip_address)
result.update({
result.update(
{
"is_site_local": ipv6.is_site_local,
"ipv4_mapped": ipv6.ipv4_mapped is not None,
"teredo": ipv6.teredo is not None,
"sixtofour": ipv6.sixtofour is not None
})
"sixtofour": ipv6.sixtofour is not None,
}
)
# 尝试获取反向DNS查询名称
try:
import socket
result["reverse_pointer"] = socket.gethostbyaddr(ip_address)[0]
except (socket.herror, socket.gaierror):
result["reverse_pointer"] = None
return result
except ValueError:
raise HTTPException(
status_code=400,
detail=f"无效的IP地址: {ip_address}"
)
raise HTTPException(status_code=400, detail=f"无效的IP地址: {ip_address}")
def _extract_client_ip(request: Request) -> str:
@ -118,7 +119,7 @@ def _extract_client_ip(request: Request) -> str:
"X-Real-IP",
"CF-Connecting-IP", # Cloudflare
"True-Client-IP", # Akamai/Cloudflare
"X-Client-IP"
"X-Client-IP",
]:
if header_value := request.headers.get(header):
# 如果是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:
connection_successful = False
assert (
connection_successful
), "The database connection should be successful and not raise an exception."
assert connection_successful, (
"The database connection should be successful and not raise an exception."
)
assert session_mock.exec.called_once_with(
select(1)
), "The session should execute a select statement once."
assert session_mock.exec.called_once_with(select(1)), (
"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:
connection_successful = False
assert (
connection_successful
), "The database connection should be successful and not raise an exception."
assert connection_successful, (
"The database connection should be successful and not raise an exception."
)
assert session_mock.exec.called_once_with(
select(1)
), "The session should execute a select statement once."
assert session_mock.exec.called_once_with(select(1)), (
"The session should execute a select statement once."
)

1
hooks/post_gen_project.py

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

Loading…
Cancel
Save