dnfs2: 彻底移除 dnfs.ini 依赖,改纯 CLI + ENV + 默认值
- 删除 confs/configs/dnfs.ini - dnfs2.cpp: 移除 INI 解析器、confs 拉取逻辑、--config/--no-confs 参数 - dnfs2.cpp: 移除 confs_host/confs_port/confs_config_name/no_confs 字段 - dnfs2.cpp: 移除 <map>/<sstream>/<fstream> 未使用头文件 - services.yaml: 移除 --no-confs(不再需要) - 更新 dnfs2/README.md: 配置说明改为 CLI 参数表 - 更新 README.md / confs/README.md: 移除 dnfs.ini 引用
This commit is contained in:
1 parent
22ade06114
commit
01e67648e0
6 files changed
+24
-182
No files matched your search
@@ -136,7 +136,7 @@
|
||||
- 数据操作:libmsg `RpcClient` 调 db2 `execute` 路由(与 Python 版调 db 路径一致)。
|
||||
- 内容审核:HttpClient POST ai_review `/review`,fail-closed。
|
||||
- 异步日志:libmsg `AsyncQueue` 写入 db2 Logs 表。
|
||||
- 配置从 confs 拉取 `dnfs.ini`,环境变量覆盖,命令行参数最高优先级;详见 [dnfs2/README.md](dnfs2/README.md)。
|
||||
- 配置通过 CLI 参数 + 环境变量 + 默认值,不依赖 confs;详见 [dnfs2/README.md](dnfs2/README.md)。
|
||||
|
||||
### db(通用数据库服务)
|
||||
|
||||
|
||||
@@ -236,7 +236,6 @@ except RpcError as e:
|
||||
| 服务 | 配置文件 | 接入方式 |
|
||||
|------|---------|---------|
|
||||
| [static](../static/) | [static.ini](configs/static.ini) | confs 强依赖(pymsg 自动阻塞等待 confs 就绪)→ 环境变量覆盖(最高优先级,Docker 兼容) |
|
||||
| [dnfs](../dnfs/) | [dnfs.ini](configs/dnfs.ini) | confs 强依赖(pymsg 自动阻塞等待 confs 就绪)→ 环境变量覆盖(最高优先级,Docker 兼容) |
|
||||
| [db](../db/) | [db.ini](configs/db.ini) | confs 强依赖(pymsg 自动阻塞等待 confs 就绪)→ 环境变量覆盖(最高优先级,Docker 兼容) |
|
||||
|
||||
static 的接入实现见 [static/fsys.py](../static/fsys.py):
|
||||
@@ -248,13 +247,6 @@ static 的接入实现见 [static/fsys.py](../static/fsys.py):
|
||||
- 支持的环境变量:`LISTEN_ADDR`/`LISTEN_PORT`/`STATIC_ROOT`/`CONFS_HOST`/`CONFS_PORT`
|
||||
- 已删除 `--config` 命令行参数和本地 `config.ini` 回退逻辑
|
||||
|
||||
dnfs 的接入实现见 [dnfs/dnfs.py](../dnfs/dnfs.py):
|
||||
- 两层加载顺序:confs(dnfs.ini,强依赖)→ 环境变量覆盖(优先级最高)
|
||||
- pymsg `retry_on_refused=True` 自动阻塞等待 confs 就绪,不严格要求启动顺序
|
||||
- 配置项涵盖:`[server]` host/port、`[database]` host/port/timeout、`[ai_review]` url/timeout
|
||||
- confs 连接地址为引导配置:环境变量 `CONFS_HOST`/`CONFS_PORT` 优先,默认 `127.0.0.1:8010`
|
||||
- 支持的环境变量:`LISTEN_ADDR`/`LISTEN_PORT`/`DB_HOST`/`DB_PORT`/`REVIEW_URL`/`CONFS_HOST`/`CONFS_PORT`
|
||||
|
||||
db 的接入实现见 [db/dbv2.py](../db/dbv2.py):
|
||||
- 两层加载顺序:confs(db.ini,强依赖)→ 环境变量覆盖(优先级最高)
|
||||
- pymsg `retry_on_refused=True` 自动阻塞等待 confs 就绪,不严格要求启动顺序
|
||||
@@ -274,7 +266,6 @@ confs/
|
||||
├── configs/ # 配置文件存放目录(被分发对象)
|
||||
│ ├── example.ini # 示例配置
|
||||
│ ├── static.ini # static 服务配置(已接入分发)
|
||||
│ ├── dnfs.ini # dnfs 服务配置(已接入分发)
|
||||
│ └── db.ini # db 服务配置(已接入分发)
|
||||
├── docs/
|
||||
│ └── SECURITY_AUDIT_20260801.md # 安全审计报告
|
||||
|
||||
@@ -1,16 +0,0 @@
|
||||
[server]
|
||||
host = 127.0.0.1
|
||||
port = 8006
|
||||
|
||||
[database]
|
||||
host = 127.0.0.1
|
||||
port = 8005
|
||||
timeout = 10
|
||||
|
||||
[ai_review]
|
||||
url = http://127.0.0.1:8007/review
|
||||
timeout = 10
|
||||
|
||||
[gateway]
|
||||
url = http://127.0.0.1:8443/whoami
|
||||
timeout = 5
|
||||
@@ -29,7 +29,6 @@ services:
|
||||
- name: dnfs2
|
||||
cmd: bash /opt/micro_servers/dnfs2/_run.sh
|
||||
args:
|
||||
- "--no-confs"
|
||||
- "--instance"
|
||||
- "dnfs2"
|
||||
- "--log-level"
|
||||
|
||||
+16
-22
@@ -33,30 +33,24 @@ dnfs2 :8006 ──pymsg──► db2 :8005 (execute 路由) ──► SQLite
|
||||
|
||||
## 配置
|
||||
|
||||
从 confs 拉取 `dnfs.ini`,环境变量覆盖,命令行参数最高优先级。
|
||||
纯 CLI + 环境变量 + 默认值,不依赖 confs。
|
||||
|
||||
```ini
|
||||
[server]
|
||||
host = 127.0.0.1
|
||||
port = 8006
|
||||
| 配置项 | CLI 参数 | 环境变量 | 默认值 |
|
||||
|--------|---------|---------|--------|
|
||||
| 绑定地址 | `--host` / `--addr` / `-a` | `LISTEN_ADDR` | 127.0.0.1 |
|
||||
| 监听端口 | `--port` / `-p` | `LISTEN_PORT` | 8006 |
|
||||
| 工作线程 | `--threads` / `-t` | — | 4 |
|
||||
| 数据库地址 | `--db-host` | `DB_HOST` | 127.0.0.1 |
|
||||
| 数据库端口 | `--db-port` | `DB_PORT` | 8005 |
|
||||
| 数据库超时 | `--db-timeout` | — | 10 |
|
||||
| AI 审核URL | `--review-url` | `REVIEW_URL` | http://127.0.0.1:8007/review |
|
||||
| AI 审核超时 | `--review-timeout` | — | 10 |
|
||||
| 网关验签URL | `--gateway-url` | `GATEWAY_URL` | http://127.0.0.1:8443/whoami |
|
||||
| 网关超时 | `--gateway-timeout` | — | 5 |
|
||||
| 日志级别 | `--log-level` | — | info |
|
||||
| 实例名 | `--instance` | — | dnfs2 |
|
||||
|
||||
[database]
|
||||
host = 127.0.0.1
|
||||
port = 8005
|
||||
timeout = 10
|
||||
|
||||
[ai_review]
|
||||
url = http://127.0.0.1:8007/review
|
||||
timeout = 10
|
||||
|
||||
[gateway]
|
||||
url = http://127.0.0.1:8443/whoami
|
||||
timeout = 5
|
||||
```
|
||||
|
||||
环境变量: `CONFS_HOST`, `CONFS_PORT`, `LISTEN_ADDR`, `LISTEN_PORT`, `DB_HOST`, `DB_PORT`, `REVIEW_URL`, `GATEWAY_URL`
|
||||
|
||||
命令行: `--addr`, `--port`, `--threads`
|
||||
优先级: CLI > 环境变量 > 默认值
|
||||
|
||||
## 构建
|
||||
|
||||
|
||||
+7
-133
@@ -9,7 +9,7 @@
|
||||
* 安全修复(相比 Python dnfs):
|
||||
* P0-4 统一封装 _call_db() 检查 success 后再访问 data
|
||||
* P1-6 server.setMaxBodySize(10MB) 限制请求体大小
|
||||
* P1-7 日志客户端复用主配置(confs 拉取的 db host/port)
|
||||
* P1-7 日志客户端复用主配置(CLI/ENV 的 db host/port)
|
||||
* P1-8 update_note 改 pid≠0 时校验父节点存在
|
||||
* P1-9 RpcClient 启用 retry_on_refused
|
||||
* P1-11 note_id 显式 as_int64() + 类型校验
|
||||
@@ -34,14 +34,11 @@
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <ctime>
|
||||
#include <fstream>
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <optional>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <unordered_map>
|
||||
@@ -106,44 +103,6 @@ static int safe_stoi(const std::string& s, const char* field_name) {
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// INI 解析(白名单模式,与 db2 一致)
|
||||
// ============================================================================
|
||||
|
||||
using IniMap = std::map<std::string, std::map<std::string, std::string>>;
|
||||
|
||||
static IniMap parse_ini(const std::string& text) {
|
||||
IniMap result;
|
||||
std::string section;
|
||||
std::istringstream ss(text);
|
||||
std::string line;
|
||||
while (std::getline(ss, line)) {
|
||||
auto b = line.find_first_not_of(" \t\r\n");
|
||||
if (b == std::string::npos) continue;
|
||||
line = line.substr(b);
|
||||
auto e = line.find_last_not_of(" \t\r\n");
|
||||
if (e != std::string::npos) line = line.substr(0, e + 1);
|
||||
|
||||
if (line.empty() || line[0] == '#' || line[0] == ';') continue;
|
||||
if (line.front() == '[' && line.back() == ']') {
|
||||
section = line.substr(1, line.size() - 2);
|
||||
continue;
|
||||
}
|
||||
auto eq = line.find('=');
|
||||
if (eq == std::string::npos || section.empty()) continue;
|
||||
std::string key = line.substr(0, eq);
|
||||
std::string val = line.substr(eq + 1);
|
||||
auto trim = [](std::string& s) {
|
||||
auto bb = s.find_first_not_of(" \t");
|
||||
auto ee = s.find_last_not_of(" \t");
|
||||
s = (bb == std::string::npos) ? "" : s.substr(bb, ee - bb + 1);
|
||||
};
|
||||
trim(key); trim(val);
|
||||
result[section][key] = val;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// 配置
|
||||
// ============================================================================
|
||||
@@ -163,21 +122,15 @@ struct Config {
|
||||
std::string gateway_url = "http://127.0.0.1:8443/whoami";
|
||||
double gateway_timeout = 5.0;
|
||||
|
||||
// confs 引导
|
||||
std::string confs_host = "127.0.0.1";
|
||||
int confs_port = 8010;
|
||||
|
||||
// CLI 标准化参数
|
||||
std::string instance = "dnfs2";
|
||||
std::string log_level = "info"; // debug|info|warn|error
|
||||
std::string confs_config_name = "dnfs.ini"; // --config 覆盖
|
||||
bool no_confs = false; // --no-confs: 跳过 confs 拉取
|
||||
};
|
||||
|
||||
static Config load_config(int argc, char* argv[]) {
|
||||
Config cfg;
|
||||
|
||||
// 0. 预扫描 --help/-h 和 --config(在 confs 拉取前处理)
|
||||
// 0. 预扫描 --help/-h(在正式解析前处理)
|
||||
for (int i = 1; i < argc; ++i) {
|
||||
std::string arg = argv[i];
|
||||
if (arg == "--help" || arg == "-h") {
|
||||
@@ -195,88 +148,15 @@ static Config load_config(int argc, char* argv[]) {
|
||||
" --review-timeout <sec> AI 审核超时秒数,默认 10\n"
|
||||
" --gateway-url <url> 网关验签 URL,默认 http://127.0.0.1:8443/whoami\n"
|
||||
" --gateway-timeout <sec> 网关超时秒数,默认 5\n"
|
||||
" --config <name> confs 拉取的配置名,默认 dnfs.ini\n"
|
||||
" --no-confs 跳过 confs 拉取,完全依赖 CLI + ENV + 默认值\n"
|
||||
" --log-level <level> debug|info|warn|error,默认 info\n"
|
||||
" --instance <name> 实例名(注册中心用),默认 dnfs2\n"
|
||||
" --help, -h 显示此帮助并退出\n\n"
|
||||
"配置优先级: CLI > 环境变量 > confs > 默认值\n"
|
||||
"使用 --no-confs 时跳过 confs,优先级变为: CLI > 环境变量 > 默认值\n");
|
||||
"配置优先级: CLI > 环境变量 > 默认值\n");
|
||||
std::exit(0);
|
||||
}
|
||||
if (arg == "--config" && i + 1 < argc) cfg.confs_config_name = argv[++i];
|
||||
if (arg == "--no-confs") cfg.no_confs = true;
|
||||
}
|
||||
|
||||
// 1. confs 引导地址
|
||||
if (const char* h = std::getenv("CONFS_HOST")) cfg.confs_host = h;
|
||||
if (const char* p = std::getenv("CONFS_PORT")) {
|
||||
cfg.confs_port = safe_stoi(p, "CONFS_PORT");
|
||||
if (cfg.confs_port < 1 || cfg.confs_port > 65535) {
|
||||
std::fprintf(stderr, "[dnfs2] invalid CONFS_PORT: %d (must be 1-65535)\n", cfg.confs_port);
|
||||
std::exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
// 2. 从 confs 拉取配置文件(retry_on_refused=true,等待 confs 就绪)
|
||||
// --no-confs 模式下跳过,直接用 ENV + CLI + 默认值
|
||||
if (!cfg.no_confs) {
|
||||
std::string ini_text;
|
||||
try {
|
||||
RpcClient confs_client(cfg.confs_host, cfg.confs_port, "dnfs2",
|
||||
/*timeout=*/5.0, /*retry_on_refused=*/true);
|
||||
json::value data = confs_client.call("get", {{"name", cfg.confs_config_name}});
|
||||
if (data.is_object() && data.as_object().contains("content")) {
|
||||
ini_text = std::string(data.as_object().at("content").as_string());
|
||||
}
|
||||
} catch (const std::exception& e) {
|
||||
std::fprintf(stderr, "[dnfs2] 从 confs 获取 %s 失败: %s\n", cfg.confs_config_name.c_str(), e.what());
|
||||
std::exit(1);
|
||||
}
|
||||
|
||||
// 3. 解析 INI
|
||||
IniMap ini = parse_ini(ini_text);
|
||||
|
||||
// 校验必需配置项
|
||||
static const std::pair<const char*, const char*> required_keys[] = {
|
||||
{"server", "host"},
|
||||
{"server", "port"},
|
||||
{"database", "host"},
|
||||
{"database", "port"},
|
||||
{"database", "timeout"},
|
||||
{"ai_review", "url"},
|
||||
{"ai_review", "timeout"},
|
||||
{"gateway", "url"},
|
||||
{"gateway", "timeout"},
|
||||
};
|
||||
for (auto& [sec, key] : required_keys) {
|
||||
auto si = ini.find(sec);
|
||||
if (si == ini.end() || si->second.find(key) == si->second.end()) {
|
||||
std::fprintf(stderr, "[dnfs2] 缺少必需配置: [%s] %s\n", sec, key);
|
||||
std::exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
auto get = [&](const std::string& sec, const std::string& key,
|
||||
const std::string& def = "") -> std::string {
|
||||
auto si = ini.find(sec);
|
||||
if (si == ini.end()) return def;
|
||||
auto ki = si->second.find(key);
|
||||
return (ki == si->second.end()) ? def : ki->second;
|
||||
};
|
||||
|
||||
cfg.listen_host = get("server", "host", cfg.listen_host);
|
||||
cfg.listen_port = safe_stoi(get("server", "port", std::to_string(cfg.listen_port)), "[server] port");
|
||||
cfg.db_host = get("database", "host", cfg.db_host);
|
||||
cfg.db_port = safe_stoi(get("database", "port", std::to_string(cfg.db_port)), "[database] port");
|
||||
cfg.db_timeout = std::stod(get("database", "timeout", std::to_string(cfg.db_timeout)));
|
||||
cfg.review_url = get("ai_review", "url", cfg.review_url);
|
||||
cfg.review_timeout = std::stod(get("ai_review", "timeout", std::to_string(cfg.review_timeout)));
|
||||
cfg.gateway_url = get("gateway", "url", cfg.gateway_url);
|
||||
cfg.gateway_timeout = std::stod(get("gateway", "timeout", std::to_string(cfg.gateway_timeout)));
|
||||
}
|
||||
|
||||
// 4. 环境变量覆盖(Docker 兼容)
|
||||
// 1. 环境变量覆盖(Docker 兼容)
|
||||
if (const char* v = std::getenv("LISTEN_ADDR")) cfg.listen_host = v;
|
||||
if (const char* v = std::getenv("LISTEN_PORT")) cfg.listen_port = safe_stoi(v, "LISTEN_PORT");
|
||||
if (const char* v = std::getenv("DB_HOST")) cfg.db_host = v;
|
||||
@@ -284,7 +164,7 @@ static Config load_config(int argc, char* argv[]) {
|
||||
if (const char* v = std::getenv("REVIEW_URL")) cfg.review_url = v;
|
||||
if (const char* v = std::getenv("GATEWAY_URL")) cfg.gateway_url = v;
|
||||
|
||||
// 5. 命令行参数(最高优先级)
|
||||
// 2. 命令行参数(最高优先级)
|
||||
for (int i = 1; i < argc; ++i) {
|
||||
std::string arg = argv[i];
|
||||
if ((arg == "--addr" || arg == "-a" || arg == "--host") && i + 1 < argc) cfg.listen_host = argv[++i];
|
||||
@@ -301,7 +181,7 @@ static Config load_config(int argc, char* argv[]) {
|
||||
else if (arg == "--instance" && i + 1 < argc) cfg.instance = argv[++i];
|
||||
}
|
||||
|
||||
// 6. 数值范围校验
|
||||
// 3. 数值范围校验
|
||||
if (cfg.listen_port < 1 || cfg.listen_port > 65535) {
|
||||
std::fprintf(stderr, "[dnfs2] invalid listen_port: %d (must be 1-65535)\n", cfg.listen_port);
|
||||
std::exit(1);
|
||||
@@ -560,13 +440,7 @@ public:
|
||||
);
|
||||
_log_queue->start();
|
||||
|
||||
if (cfg.no_confs) {
|
||||
std::printf("[dnfs2] 配置来源: CLI + ENV + 默认值(--no-confs 模式)\n");
|
||||
} else {
|
||||
std::printf("[dnfs2] 配置来源: confs (%s @ %s:%d)\n",
|
||||
_cfg.confs_config_name.c_str(),
|
||||
_cfg.confs_host.c_str(), _cfg.confs_port);
|
||||
}
|
||||
std::printf("[dnfs2] 配置来源: CLI + ENV + 默认值\n");
|
||||
std::printf("[dnfs2] instance=%s log-level=%s\n",
|
||||
_cfg.instance.c_str(), _cfg.log_level.c_str());
|
||||
std::printf("[dnfs2] Token 验签: gateway %s\n", _cfg.gateway_url.c_str());
|
||||
|
||||
Reference in new issue
Block a user