redirect80: 移除confs依赖,配置全部经CLI传递(CLI > ENV > 默认值)
- redirect80.py: 移除 configparser/pymsg.RpcClient 导入和 confs 拉取逻辑, 配置改为三层加载(默认值 → 环境变量 → CLI 参数) - core/services.yaml: redirect80 条目展开为完整 CLI 声明 (--host/--port/--redirect-port/--redirect-code) - redirect80/README.md: 配置表新增 CLI 参数列,init.txt 章节重写为 services.yaml 示例 - README.md: 配置链路移除 redirect80,confs 配置表删除 redirect80.ini 行 与 ca v1.1.0 保持一致的配置模式,redirect80 不再依赖 confs 服务。
This commit is contained in:
1 parent
9d548c064f
commit
0954644ee3
4 files changed
+51
-65
No files matched your search
+28
-21
@@ -8,18 +8,18 @@
|
||||
- **智能重定向**:GET/HEAD → 301(搜索引擎友好永久重定向),POST/PUT/DELETE/PATCH → 308(保留方法和 body)
|
||||
- **Host 保留**:读取请求 `Host` 头构造重定向 URL(剥离端口),回退用配置的监听地址
|
||||
- **日志集成**:通过 `log_client.py` 异步批量推送运行日志到 `db:8005` 的 Logs 表(fail-safe)
|
||||
- **配置分层**:confs 强依赖 → 环境变量覆盖(`REDIRECT80_*`),confs 不可用时直接退出不做本地回退
|
||||
- **配置分层**:CLI 参数(由 core/services.yaml 传入) > 环境变量覆盖(`REDIRECT80_*`) > 代码默认值
|
||||
|
||||
## 配置
|
||||
|
||||
配置通过 confs 统一配置服务分发(`confs/configs/redirect80.ini`),启动时经 pymsg 阻塞等待拉取(retry_on_refused),confs 不可用时进程直接退出。环境变量优先级最高。
|
||||
配置通过三层加载(CLI > 环境变量 > 默认值),全部配置项由 core/services.yaml 经 CLI 参数传入,不依赖 confs。
|
||||
|
||||
| 配置项 | 环境变量 | 默认值 | 说明 |
|
||||
|--------|----------|--------|------|
|
||||
| `host` | `REDIRECT80_HOST` | `0.0.0.0` | 监听地址 |
|
||||
| `port` | `REDIRECT80_PORT` | `80` | 监听端口 |
|
||||
| `redirect_port` | `REDIRECT80_REDIRECT_PORT` | `443` | 重定向目标 HTTPS 端口 |
|
||||
| `redirect_code` | `REDIRECT80_REDIRECT_CODE` | `301` | 非 GET/HEAD 重定向状态码(308 vs 307) |
|
||||
| 配置项 | CLI 参数 | 环境变量 | 默认值 | 说明 |
|
||||
|--------|----------|----------|--------|------|
|
||||
| `host` | `--host` | `REDIRECT80_HOST` | `0.0.0.0` | 监听地址 |
|
||||
| `port` | `--port` | `REDIRECT80_PORT` | `80` | 监听端口 |
|
||||
| `redirect_port` | `--redirect-port` | `REDIRECT80_REDIRECT_PORT` | `443` | 重定向目标 HTTPS 端口 |
|
||||
| `redirect_code` | `--redirect-code` | `REDIRECT80_REDIRECT_CODE` | `301` | GET/HEAD 重定向状态码 |
|
||||
|
||||
## 安装 & 运行
|
||||
|
||||
@@ -45,21 +45,28 @@ AmbientCapabilities=CAP_NET_BIND_SERVICE
|
||||
ExecStart=/opt/micro_servers/redirect80/_run.sh
|
||||
```
|
||||
|
||||
## 在 core 的 init.txt 中
|
||||
## 在 core 的 services.yaml 中
|
||||
|
||||
```
|
||||
bash /opt/micro_servers/redirect80/_run.sh
|
||||
```
|
||||
redirect80 作为最后一个服务启动,配置全部经 CLI 传入:
|
||||
|
||||
建议放在 waf 之前:
|
||||
|
||||
```
|
||||
bash /opt/micro_servers/db/_run.sh
|
||||
bash /opt/micro_servers/confs/_run.sh
|
||||
bash /opt/micro_servers/redirect80/_run.sh # ← 80→443 重定向
|
||||
bash /opt/micro_servers/waf/_run.sh
|
||||
bash /opt/micro_servers/gateway/_run.sh
|
||||
...
|
||||
```yaml
|
||||
- name: redirect80
|
||||
cmd: bash /opt/micro_servers/redirect80/_run.sh
|
||||
args:
|
||||
- "--instance"
|
||||
- "redirect80"
|
||||
- "--log-level"
|
||||
- "info"
|
||||
- "--host"
|
||||
- "0.0.0.0"
|
||||
- "--port"
|
||||
- "80"
|
||||
- "--redirect-port"
|
||||
- "443"
|
||||
- "--redirect-code"
|
||||
- "301"
|
||||
depends_on: [waf]
|
||||
sleep_before: 0
|
||||
```
|
||||
|
||||
## 端口一览
|
||||
|
||||
@@ -8,14 +8,12 @@ redirect80 — HTTP 80 端口重定向服务。
|
||||
日志通过 log_client 异步推送到 db 的 Logs 表(fail-safe)。
|
||||
|
||||
配置(三层加载):
|
||||
1. confs 强依赖拉取 redirect80.ini(retry_on_refused 阻塞等待)
|
||||
2. 环境变量覆盖(Docker/k8s 兼容)
|
||||
3. 命令行参数(最高优先级)
|
||||
配置不可用时直接退出,不做本地回退。
|
||||
1. 代码默认值
|
||||
2. 环境变量覆盖(REDIRECT80_* 前缀,Docker/k8s 兼容)
|
||||
3. 命令行参数(最高优先级,由 core/services.yaml 传入)
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import configparser
|
||||
import os
|
||||
import sys as _sys
|
||||
from http.server import HTTPServer, BaseHTTPRequestHandler
|
||||
@@ -26,14 +24,11 @@ _here = os.path.dirname(os.path.abspath(__file__))
|
||||
_sys.path.insert(0, os.path.join(_here, ".."))
|
||||
from log_client import logs, set_log_level # noqa: E402
|
||||
|
||||
_sys.path.insert(0, os.path.join(_here, "..", "pymsg"))
|
||||
from pymsg import RpcClient # noqa: E402
|
||||
|
||||
|
||||
# ── 配置加载 ─────────────────────────────────────────────────────────
|
||||
|
||||
def load_config(argv):
|
||||
"""三层加载: confs → ENV → CLI(最高优先级)。"""
|
||||
"""三层加载: 默认值 → ENV → CLI(最高优先级)。"""
|
||||
|
||||
# argparse 先行:--help 在任何网络调用前处理
|
||||
parser = argparse.ArgumentParser(
|
||||
@@ -41,7 +36,7 @@ def load_config(argv):
|
||||
description="HTTP 80 端口重定向服务",
|
||||
add_help=False,
|
||||
formatter_class=argparse.RawDescriptionHelpFormatter,
|
||||
epilog="配置优先级: CLI > 环境变量 > confs > 默认值",
|
||||
epilog="配置优先级: CLI > 环境变量 > 默认值",
|
||||
)
|
||||
parser.add_argument("--host", default=None,
|
||||
help="绑定地址,默认 0.0.0.0")
|
||||
@@ -69,35 +64,7 @@ def load_config(argv):
|
||||
"instance": "redirect80",
|
||||
}
|
||||
|
||||
# 1. confs 强依赖拉取配置(阻塞等待 confs 就绪,失败则退出)
|
||||
confs_host = os.environ.get("CONFS_HOST", "127.0.0.1")
|
||||
confs_port = int(os.environ.get("CONFS_PORT", "8010"))
|
||||
try:
|
||||
client = RpcClient(
|
||||
confs_host, confs_port, "redirect80",
|
||||
retry_on_refused=True, timeout=5,
|
||||
)
|
||||
data = client.call("get", {"name": "redirect80.ini"})
|
||||
ini_text = data.get("content", "")
|
||||
ini = configparser.ConfigParser(interpolation=None)
|
||||
ini.read_string(ini_text)
|
||||
|
||||
_merge = {
|
||||
("server", "host"): "host",
|
||||
("server", "port"): "port",
|
||||
("server", "redirect_port"): "redirect_port",
|
||||
("server", "redirect_code"): "redirect_code",
|
||||
}
|
||||
for (sec, key), var in _merge.items():
|
||||
if ini.has_option(sec, key):
|
||||
val = ini.get(sec, key)
|
||||
config[var] = type(config[var])(val)
|
||||
logs.info("redirect80", "配置已从 confs 加载")
|
||||
except Exception as e:
|
||||
print(f"[redirect80] confs 获取失败: {e}", file=_sys.stderr)
|
||||
_sys.exit(1)
|
||||
|
||||
# 2. 环境变量覆盖
|
||||
# 1. 环境变量覆盖
|
||||
_env_map = {
|
||||
"REDIRECT80_HOST": "host",
|
||||
"REDIRECT80_PORT": ("port", int),
|
||||
@@ -112,7 +79,7 @@ def load_config(argv):
|
||||
else:
|
||||
config[mapping] = val
|
||||
|
||||
# 3. CLI 参数(最高优先级)
|
||||
# 2. CLI 参数(最高优先级)
|
||||
if args.host is not None:
|
||||
config["host"] = args.host
|
||||
if args.port is not None:
|
||||
|
||||
Reference in new issue
Block a user