Skip to content

Commit a0a632b

Browse files
authored
Merge pull request #111 from openChatGpts/master
fixbug: 在 f-string 中统一使用单引号替代双引号,避免嵌套引号问题
2 parents ab80fe3 + 31d7d08 commit a0a632b

9 files changed

Lines changed: 117 additions & 32 deletions

File tree

.env.example

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
# 监听主机(默认 0.0.0.0)
66
# APP_HOST=0.0.0.0
77

8-
# 监听端口(默认 8000
9-
# APP_PORT=8000
8+
# 监听端口(默认 15555
9+
# APP_PORT=15555
1010

1111
# Web UI 访问密钥(默认 admin123,强烈建议修改)
1212
# APP_ACCESS_PASSWORD=your_secret_password

Dockerfile

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ FROM python:3.11-slim
44
# 设置工作目录
55
WORKDIR /app
66

7+
ARG DEFAULT_WEBUI_PORT=15555
8+
79
# 设置环境变量
810
ENV PYTHONDONTWRITEBYTECODE=1 \
911
PYTHONUNBUFFERED=1 \
1012
# WebUI 默认配置
1113
WEBUI_HOST=0.0.0.0 \
12-
WEBUI_PORT=15555 \
14+
WEBUI_PORT=${DEFAULT_WEBUI_PORT} \
1315
LOG_LEVEL=info \
1416
DEBUG=0
1517

@@ -30,7 +32,7 @@ RUN pip install --no-cache-dir --upgrade pip \
3032
COPY . .
3133

3234
# 暴露端口
33-
EXPOSE 15555
35+
EXPOSE ${DEFAULT_WEBUI_PORT}
3436

3537
# 启动 WebUI
3638
CMD ["python", "webui.py"]

README.md

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,20 @@ cp .env.example .env
104104

105105
> 优先级:命令行参数 > 环境变量(`.env`)> 数据库设置 > 默认值
106106
107+
### 修改端口
108+
109+
默认端口是 `15555`。现在已经收敛到少数几个固定入口:
110+
111+
- 本地临时启动改端口:直接用 `python webui.py --port 18080`
112+
- 本地通过 `.env` 改端口:设置 `APP_PORT=18080`
113+
- 源码里的默认端口:修改 `src/config/constants.py` 里的 `DEFAULT_WEBUI_PORT`
114+
- Docker Compose 默认端口:修改 `docker-compose.yml` 顶部的 `x-webui-port`
115+
- Docker 镜像构建默认端口:修改 `Dockerfile` 里的 `ARG DEFAULT_WEBUI_PORT`
116+
117+
补充说明:
118+
- `src/config/constants.py``DEFAULT_WEBUI_PORT` 会同时影响默认 Web UI 端口、默认回调地址和 e2e 脚本默认地址。
119+
- `docker-compose.yml` 里已经把端口映射、容器内 `WEBUI_PORT` 和健康检查统一绑到同一个 `x-webui-port`,改一处就够。
120+
107121
### 启动 Web UI
108122

109123
```bash
@@ -141,6 +155,18 @@ docker-compose up -d
141155
```
142156
你可以在 `docker-compose.yml` 中修改相关的环境变量,例如配置端口或者设置 `WEBUI_ACCESS_PASSWORD` 访问密码。
143157

158+
如果要修改 Docker Compose 对外端口,直接改文件顶部这一行即可:
159+
160+
```yaml
161+
x-webui-port: &webui-port 15555
162+
```
163+
164+
这一个值会同时同步到:
165+
166+
- 宿主机端口映射
167+
- 容器内 `WEBUI_PORT`
168+
- 健康检查访问地址
169+
144170
#### 直接使用 docker run
145171

146172
如果你不想使用 docker-compose,也可以直接拉取并运行镜像:
@@ -165,6 +191,19 @@ docker run -d \
165191

166192
> **注意**:`-v $(pwd)/data:/app/data` 挂载参数非常重要,它确保了你的数据库文件和账户信息在容器重启或更新后不会丢失。
167193

194+
如果你要把容器端口改成 `18080`,`-p` 和 `WEBUI_PORT` 需要一起改:
195+
196+
```bash
197+
docker run -d \
198+
-p 18080:18080 \
199+
-e WEBUI_HOST=0.0.0.0 \
200+
-e WEBUI_PORT=18080 \
201+
-e WEBUI_ACCESS_PASSWORD=your_secure_password \
202+
-v $(pwd)/data:/app/data \
203+
--name codex-register \
204+
ghcr.io/yunxilyf/codex-register:latest
205+
```
206+
168207
### 使用远程 PostgreSQL
169208

170209
通过环境变量指定数据库连接字符串:
@@ -335,7 +374,7 @@ docker-compose up -d
335374

336375
### 配置说明
337376

338-
**端口映射**:默认 `15555` 端口,可在 `docker-compose.yml` 中修改
377+
**端口映射**:默认 `15555` 端口,修改 `docker-compose.yml` 顶部的 `x-webui-port` 即可
339378

340379
**数据持久化**
341380
```yaml
@@ -347,9 +386,9 @@ volumes:
347386
**环境变量配置**:
348387
```yaml
349388
environment:
350-
- APP_ACCESS_PASSWORD=mypassword
351-
- APP_HOST=0.0.0.0
352-
- APP_PORT=15555
389+
WEBUI_ACCESS_PASSWORD: mypassword
390+
WEBUI_HOST: 0.0.0.0
391+
WEBUI_PORT: 15555
353392
```
354393
355394
### 常用命令

docker-compose.yml

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
1+
x-webui-port: &webui-port 15555
2+
13
services:
24
webui:
35
build: .
46
ports:
5-
- "15555:15555"
7+
- target: *webui-port
8+
published: *webui-port
9+
protocol: tcp
610
environment:
7-
- WEBUI_HOST=0.0.0.0
8-
- WEBUI_PORT=15555
9-
- WEBUI_ACCESS_PASSWORD=admin123
10-
- DEBUG=0
11-
- LOG_LEVEL=info
11+
WEBUI_HOST: 0.0.0.0
12+
WEBUI_PORT: *webui-port
13+
WEBUI_ACCESS_PASSWORD: admin123
14+
DEBUG: 0
15+
LOG_LEVEL: info
1216
volumes:
1317
# 挂载数据目录以持久化数据库和日志
1418
- ./data:/app/data
1519
- ./logs:/app/logs
1620
healthcheck:
1721
test:
18-
- CMD
19-
- python
20-
- -c
21-
- import urllib.request; urllib.request.urlopen('http://127.0.0.1:15555/', timeout=5).read()
22+
- CMD-SHELL
23+
- python -c "import os, urllib.request; urllib.request.urlopen('http://127.0.0.1:' + os.environ['WEBUI_PORT'] + '/', timeout=5).read()"
2224
interval: 10s
2325
timeout: 5s
2426
retries: 5

src/config/constants.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,25 @@ class EmailServiceType(str, Enum):
4848
APP_NAME = "OpenAI/Codex CLI 自动注册系统"
4949
APP_VERSION = "2.0.0"
5050
APP_DESCRIPTION = "自动注册 OpenAI/Codex CLI 账号的系统"
51+
DEFAULT_WEBUI_HOST = "0.0.0.0"
52+
DEFAULT_WEBUI_PORT = 15555
53+
DEFAULT_WEBUI_LOCAL_HOST = "127.0.0.1"
54+
55+
56+
def build_http_url(host: str, port: int, path: str = "") -> str:
57+
"""构造本地 HTTP URL。"""
58+
normalized_path = path if not path or path.startswith("/") else f"/{path}"
59+
return f"http://{host}:{port}{normalized_path}"
60+
61+
62+
def build_ws_url(host: str, port: int, path: str = "") -> str:
63+
"""构造本地 WebSocket URL。"""
64+
normalized_path = path if not path or path.startswith("/") else f"/{path}"
65+
return f"ws://{host}:{port}{normalized_path}"
66+
67+
68+
DEFAULT_WEBUI_BASE_URL = build_http_url(DEFAULT_WEBUI_LOCAL_HOST, DEFAULT_WEBUI_PORT)
69+
DEFAULT_WEBUI_WS_BASE_URL = build_ws_url(DEFAULT_WEBUI_LOCAL_HOST, DEFAULT_WEBUI_PORT)
5170

5271
# ============================================================================
5372
# OpenAI OAuth 相关常量
@@ -57,7 +76,7 @@ class EmailServiceType(str, Enum):
5776
OAUTH_CLIENT_ID = "app_EMoamEEZ73f0CkXaXp7hrann"
5877
OAUTH_AUTH_URL = "https://auth.openai.com/oauth/authorize"
5978
OAUTH_TOKEN_URL = "https://auth.openai.com/oauth/token"
60-
OAUTH_REDIRECT_URI = "http://localhost:15555/auth/callback"
79+
OAUTH_REDIRECT_URI = build_http_url("localhost", DEFAULT_WEBUI_PORT, "/auth/callback")
6180
OAUTH_SCOPE = "openid email profile offline_access"
6281

6382
# Codex CLI 专用 OAuth 参数(用于生成 Codex 兼容的 auth.json)
@@ -280,8 +299,8 @@ def generate_random_user_info() -> dict:
280299
("registration.max_retries", "3", "最大重试次数", "registration"),
281300
("registration.timeout", "120", "超时时间(秒)", "registration"),
282301
("registration.default_password_length", "12", "默认密码长度", "registration"),
283-
("webui.host", "0.0.0.0", "Web UI 监听主机", "webui"),
284-
("webui.port", "15555", "Web UI 监听端口", "webui"),
302+
("webui.host", DEFAULT_WEBUI_HOST, "Web UI 监听主机", "webui"),
303+
("webui.port", str(DEFAULT_WEBUI_PORT), "Web UI 监听端口", "webui"),
285304
("webui.debug", "true", "调试模式", "webui"),
286305
]
287306

src/config/settings.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
from pydantic.types import SecretStr
1111
from dataclasses import dataclass
1212

13+
from .constants import APP_NAME, APP_VERSION, DEFAULT_WEBUI_HOST, DEFAULT_WEBUI_PORT
14+
1315

1416
class SettingCategory(str, Enum):
1517
"""设置分类"""
@@ -42,13 +44,13 @@ class SettingDefinition:
4244
# 应用信息
4345
"app_name": SettingDefinition(
4446
db_key="app.name",
45-
default_value="OpenAI/Codex CLI 自动注册系统",
47+
default_value=APP_NAME,
4648
category=SettingCategory.GENERAL,
4749
description="应用名称"
4850
),
4951
"app_version": SettingDefinition(
5052
db_key="app.version",
51-
default_value="2.0.0",
53+
default_value=APP_VERSION,
5254
category=SettingCategory.GENERAL,
5355
description="应用版本"
5456
),
@@ -70,13 +72,13 @@ class SettingDefinition:
7072
# Web UI 配置
7173
"webui_host": SettingDefinition(
7274
db_key="webui.host",
73-
default_value="0.0.0.0",
75+
default_value=DEFAULT_WEBUI_HOST,
7476
category=SettingCategory.WEBUI,
7577
description="Web UI 监听地址"
7678
),
7779
"webui_port": SettingDefinition(
7880
db_key="webui.port",
79-
default_value=15555,
81+
default_value=DEFAULT_WEBUI_PORT,
8082
category=SettingCategory.WEBUI,
8183
description="Web UI 监听端口"
8284
),
@@ -584,8 +586,8 @@ class Settings(BaseModel):
584586
"""
585587

586588
# 应用信息
587-
app_name: str = "OpenAI/Codex CLI 自动注册系统"
588-
app_version: str = "2.0.0"
589+
app_name: str = APP_NAME
590+
app_version: str = APP_VERSION
589591
debug: bool = False
590592

591593
# 数据库配置
@@ -608,8 +610,8 @@ def validate_database_url(cls, v):
608610
return v
609611

610612
# Web UI 配置
611-
webui_host: str = "0.0.0.0"
612-
webui_port: int = 15555
613+
webui_host: str = DEFAULT_WEBUI_HOST
614+
webui_port: int = DEFAULT_WEBUI_PORT
613615
webui_secret_key: SecretStr = SecretStr("your-secret-key-change-in-production")
614616
webui_access_password: SecretStr = SecretStr("admin123")
615617

src/core/openai/payment.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ def generate_team_link(
202202
"&elements_session_client[is_aggregation_expected]=false"
203203
"&client_attribution_metadata[merchant_integration_additional_elements][0]=payment"
204204
"&client_attribution_metadata[merchant_integration_additional_elements][1]=address"
205-
f"&key={data["publishable_key"]}"
205+
f"&key={data['publishable_key']}"
206206
,
207207
proxies=_build_proxies(proxy),
208208
timeout=30,

tests/e2e/runtime_functionality_check.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import httpx
1111
import websockets
1212

13+
from src.config.constants import DEFAULT_WEBUI_BASE_URL, DEFAULT_WEBUI_WS_BASE_URL
14+
1315

1416
STALE_ERROR = "服务启动时检测到未完成的历史任务,已标记失败,请重新发起。"
1517

@@ -254,8 +256,8 @@ def run_verify_recovery_mode(base_url: str, db_path: Path, state_path: Path, rep
254256
def main() -> None:
255257
parser = argparse.ArgumentParser(description="真实服务功能可用性验证脚本")
256258
parser.add_argument("--mode", choices=["live", "prepare-recovery", "verify-recovery"], required=True)
257-
parser.add_argument("--base-url", default="http://127.0.0.1:15555")
258-
parser.add_argument("--ws-url", default="ws://127.0.0.1:15555")
259+
parser.add_argument("--base-url", default=DEFAULT_WEBUI_BASE_URL)
260+
parser.add_argument("--ws-url", default=DEFAULT_WEBUI_WS_BASE_URL)
259261
parser.add_argument("--db-path", required=True)
260262
parser.add_argument("--report-path", default="tests_runtime/runtime_functionality_report.json")
261263
parser.add_argument("--state-path", default="tests_runtime/runtime_recovery_state.json")
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from src.config.constants import (
2+
DEFAULT_SETTINGS,
3+
DEFAULT_WEBUI_BASE_URL,
4+
DEFAULT_WEBUI_PORT,
5+
DEFAULT_WEBUI_WS_BASE_URL,
6+
OAUTH_REDIRECT_URI,
7+
)
8+
from src.config.settings import SETTING_DEFINITIONS, Settings
9+
10+
11+
def test_default_webui_port_is_shared_from_one_constant():
12+
default_settings_map = {key: value for key, value, *_ in DEFAULT_SETTINGS}
13+
14+
assert SETTING_DEFINITIONS["webui_port"].default_value == DEFAULT_WEBUI_PORT
15+
assert Settings().webui_port == DEFAULT_WEBUI_PORT
16+
assert default_settings_map["webui.port"] == str(DEFAULT_WEBUI_PORT)
17+
assert DEFAULT_WEBUI_BASE_URL == f"http://127.0.0.1:{DEFAULT_WEBUI_PORT}"
18+
assert DEFAULT_WEBUI_WS_BASE_URL == f"ws://127.0.0.1:{DEFAULT_WEBUI_PORT}"
19+
assert OAUTH_REDIRECT_URI == f"http://localhost:{DEFAULT_WEBUI_PORT}/auth/callback"

0 commit comments

Comments
 (0)