From 5a9757e9de0de5526685b9d3d880e239cd0cab30 Mon Sep 17 00:00:00 2001 From: zhangtao <9480807882@qq.com> Date: Tue, 12 May 2026 21:05:05 +0800 Subject: [PATCH] =?UTF-8?q?feat(config):=20=E5=A2=9E=E5=8A=A0=20IP=20?= =?UTF-8?q?=E5=BD=92=E5=B1=9E=E5=9C=B0=E6=9F=A5=E8=AF=A2=E5=BC=80=E5=85=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新增 IP_LOCATION_ENABLE 配置项,允许在生产环境中关闭登录时的第三方 IP 查询请求,并统一使用 HTTPX_DEFAULT_TIMEOUT 超时设置。 --- backend/app/config/setting.py | 1 + backend/app/utils/ip_local_util.py | 4 ++-- backend/env/.env.dev.example | 3 +++ backend/env/.env.prod.example | 3 +++ docker/.env.example | 23 ----------------------- 5 files changed, 9 insertions(+), 25 deletions(-) delete mode 100644 docker/.env.example diff --git a/backend/app/config/setting.py b/backend/app/config/setting.py index 35c29a33..24dddef6 100755 --- a/backend/app/config/setting.py +++ b/backend/app/config/setting.py @@ -140,6 +140,7 @@ class Settings(BaseSettings): # ******************* 外部 HTTP(httpx)******************* # # ================================================= # HTTPX_DEFAULT_TIMEOUT: float = 10.0 # 对外请求默认超时(秒),见 app/common/httpx_defaults.py + IP_LOCATION_ENABLE: bool = True # 是否启用 IP 归属地查询(登录时对外发起 HTTP 请求) # ================================================= # # ********************* 日志配置 ******************* # diff --git a/backend/app/utils/ip_local_util.py b/backend/app/utils/ip_local_util.py index f047d4ae..06e3951a 100644 --- a/backend/app/utils/ip_local_util.py +++ b/backend/app/utils/ip_local_util.py @@ -57,7 +57,7 @@ class IpLocalUtil: """ if not ip: return None - if settings.DEBUG: + if settings.DEBUG or not settings.IP_LOCATION_ENABLE: return ( "内网IP" if cls.is_private_ip(ip) @@ -86,7 +86,7 @@ class IpLocalUtil: return "内网IP" try: - async with httpx.AsyncClient(timeout=5) as client: + async with httpx.AsyncClient(timeout=settings.HTTPX_DEFAULT_TIMEOUT) as client: # 首选:ip9.com.cn API url = f"https://ip9.com.cn/get?ip={ip}" response = await cls._make_api_request(client, url) diff --git a/backend/env/.env.dev.example b/backend/env/.env.dev.example index d285f63a..c8c4d4ce 100644 --- a/backend/env/.env.dev.example +++ b/backend/env/.env.dev.example @@ -47,3 +47,6 @@ LOGGER_LEVEL = "DEBUG" # 日志级别 OPENAI_BASE_URL = "https://api.deepseek.com" OPENAI_API_KEY = "your_api_key" OPENAI_MODEL = "deepseek-chat" + +# IP 归属地查询(登录时对外请求第三方 API,生产建议关闭) +IP_LOCATION_ENABLE=false \ No newline at end of file diff --git a/backend/env/.env.prod.example b/backend/env/.env.prod.example index 9f533d7f..1d824e09 100644 --- a/backend/env/.env.prod.example +++ b/backend/env/.env.prod.example @@ -47,3 +47,6 @@ LOGGER_LEVEL = 'INFO' OPENAI_BASE_URL = "https://api.deepseek.com" OPENAI_API_KEY = "your_api_key" OPENAI_MODEL = "deepseek-chat" + +# IP 归属地查询(登录时对外请求第三方 API,生产建议关闭) +IP_LOCATION_ENABLE=false \ No newline at end of file diff --git a/docker/.env.example b/docker/.env.example deleted file mode 100644 index 0d056a1d..00000000 --- a/docker/.env.example +++ /dev/null @@ -1,23 +0,0 @@ -# 数据库配置 -MYSQL_ROOT_PASSWORD=your_mysql_root_password -MYSQL_DATABASE=fastapiadmin -MYSQL_USER=fastapiadmin -MYSQL_PASSWORD=your_mysql_password -MYSQL_PORT=3306 - -# Redis配置 -REDIS_PASSWORD=your_redis_password -REDIS_PORT=6379 - -# 后端配置 -BACKEND_PORT=8001 - -# Nginx配置 -HTTP_PORT=80 -HTTPS_PORT=443 - -# 项目配置 -PROJECT_NAME=FastapiAdmin - -# 部署环境 (dev/prod) -DEPLOY_ENV=prod