Skip to content

Commit 82040f5

Browse files
committed
chore: Enhanced configuration management and hot-reload capabilities
1 parent 3cbe727 commit 82040f5

21 files changed

Lines changed: 426 additions & 191 deletions

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ redeploy.sh
1515

1616
OPTIMIZATION_REPORT.md
1717
test.log
18+
hooks/*

.goreleaser.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ builds:
1818
- "7"
1919
- "6"
2020

21-
# Config UI binary (same code as main binary; run with -config-ui and no -hooks for config-ui-only mode)
21+
# Config UI binary (same code as main binary; run with -config-ui to enable UI on webhook server)
2222
- <<: *build_defaults
2323
id: config-ui-macos
2424
main: .

README-zhCN.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ go build
7878

7979
### 基础示例
8080

81-
创建一个 `hooks.json` 文件(或使用 `hooks.yaml` 作为 YAML 格式)来定义你的 webhook:
81+
默认模式下,程序会使用 `./hooks` 目录扫描配置文件。你可以创建 `./hooks/hooks.yaml`(或 `./hooks/hooks.json`)来定义 webhook:
8282

8383
**示例:简单的部署钩子**
8484

@@ -100,10 +100,10 @@ go build
100100
command-working-directory: "/var/webhook"
101101
```
102102
103-
### 运行 WebHook
103+
### 运行 WebHook(默认目录模式)
104104
105105
```bash
106-
./webhook -hooks hooks.json -verbose
106+
./webhook -verbose
107107
```
108108

109109
服务器将在默认的 9000 端口启动。你的钩子将在以下地址可用:
@@ -112,6 +112,12 @@ go build
112112
http://yourserver:9000/hooks/redeploy-webhook
113113
```
114114

115+
如需继续使用单文件模式,仍可显式指定:
116+
117+
```bash
118+
./webhook -hooks hooks.json -verbose
119+
```
120+
115121
### 保护你的钩子
116122

117123
**重要提示**:上面的示例没有身份验证。在生产环境中请始终使用触发规则!
@@ -148,7 +154,7 @@ http://yourserver:9000/hooks/redeploy-webhook
148154

149155
- **表单数据支持**:解析 multipart 表单数据和文件上传 - 查看 [表单数据](docs/zh-CN/Referencing-Request-Values.md)
150156
- **模板支持**:使用 `-template` 标志在配置文件中使用 Go 模板 - 查看 [配置模版](docs/zh-CN/Templates.md)
151-
- **Config UI**:同一二进制,按参数切换。使用 `-config-ui` 启用配置生成 Web UI(建议仅在调试或内网使用);仅 Config UI:`go run . -config-ui`(不传 `-hooks`,默认端口 9080);或与 `-hooks` 同时使用在主服务上挂载。可用 `-config-ui-path` 修改路径(尾斜杠会归一化)。配合 `-hooks-dir`,UI 可将生成的配置保存到该目录`-urlprefix` 会影响 UI 中展示的调用 URL。详见 [配置参数](docs/zh-CN/Webhook-Parameters.md)[Config UI 说明](cmd/README.md)
157+
- **Config UI**:同一二进制,按参数切换。使用 `-config-ui` 启用配置生成 Web UI(建议仅在调试或内网使用);与主服务共用端口(默认 `9000`),可用 `-config-ui-path` 修改路径(尾斜杠会归一化)。目录模式(默认 `./hooks` 或显式 `-hooks-dir`)下,UI 可将生成的配置直接保存到目录,保存后可立即调用生成的 URL 验证;显式 `-hooks` 单文件模式下,仍可生成/下载但不会提供目录保存`-urlprefix` 会影响 UI 中展示的调用 URL。详见 [配置参数](docs/zh-CN/Webhook-Parameters.md)[Config UI 说明](cmd/README.md)
152158
- **HTTPS**:使用反向代理(nginx、Traefik、Caddy)提供 HTTPS 支持
153159
- **CORS**:使用 `-header name=value` 设置自定义响应头,包括 CORS 响应头
154160
- **热重载**:使用 `-hotreload``kill -USR1` 无需重启即可更新配置
@@ -159,7 +165,7 @@ http://yourserver:9000/hooks/redeploy-webhook
159165

160166
### 核心文档
161167
- [钩子定义](docs/zh-CN/Hook-Definition.md) - 完整的钩子配置参考
162-
- [Config UI](cmd/README.md) - 配置生成器(仅 Config UI 模式:运行 `go run . -config-ui`
168+
- [Config UI](cmd/README.md) - 配置生成器(运行 `go run . -config-ui` 启用
163169
- [钩子匹配规则](docs/zh-CN/Hook-Rules.md) - 触发规则和条件
164170
- [配置参数](docs/zh-CN/Webhook-Parameters.md) - 命令行参数和配置
165171
- [配置模版](docs/zh-CN/Templates.md) - 在配置中使用 Go 模板

README.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ go build
7878

7979
### Basic Example
8080

81-
Create a `hooks.json` file (or `hooks.yaml` for YAML format) to define your webhooks:
81+
By default, webhook scans config files from the `./hooks` directory. Create `./hooks/hooks.yaml` (or `./hooks/hooks.json`) to define your webhooks:
8282

8383
**Example: Simple Deployment Hook**
8484

@@ -100,10 +100,10 @@ If you prefer YAML, the equivalent `hooks.yaml` file would look like this:
100100
command-working-directory: "/var/webhook"
101101
```
102102
103-
### Running WebHook
103+
### Running WebHook (default directory mode)
104104
105105
```bash
106-
./webhook -hooks hooks.json -verbose
106+
./webhook -verbose
107107
```
108108

109109
The server will start on port `9000` by default. Your hook will be available at:
@@ -112,6 +112,12 @@ The server will start on port `9000` by default. Your hook will be available at:
112112
http://yourserver:9000/hooks/redeploy-webhook
113113
```
114114

115+
Single-file mode is still supported when explicitly set:
116+
117+
```bash
118+
./webhook -hooks hooks.json -verbose
119+
```
120+
115121
### Securing Your Hooks
116122

117123
**Important**: The example above has no authentication. Always use trigger rules in production!
@@ -148,7 +154,7 @@ For more security options, see:
148154

149155
- **Form Data Support**: Parse multipart form data and file uploads - see [Form Data](docs/en-US/Referencing-Request-Values.md)
150156
- **Template Support**: Use Go templates in configuration files with `-template` flag - see [Templates](docs/en-US/Templates.md)
151-
- **Config UI**: Same binary, behavior by flags. Enable config generator Web UI with `-config-ui` (recommend debugging or intranet only). Config-ui-only: `go run . -config-ui` (no `-hooks`, default port 9080); or mount on webhook server with `-hooks` and `-config-ui`. Use `-config-ui-path` to change the path (trailing slash is normalized). With `-hooks-dir`, the UI can save generated configs into that directory. The `-urlprefix` value is used for the call URL shown in the UI. See [Webhook Parameters](docs/en-US/Webhook-Parameters.md) and [Config UI](cmd/README.md).
157+
- **Config UI**: Same binary, behavior by flags. Enable config generator Web UI with `-config-ui` (recommend debugging or intranet only). It runs on the same server port (default `9000`) and can be mounted with `-config-ui-path` (trailing slash normalized). In directory mode (default `./hooks` or explicit `-hooks-dir`), the UI can save generated configs directly to that directory and you can validate by calling the generated endpoint immediately after save. In explicit single-file mode (`-hooks`), generation/download still works but save-to-directory is not exposed. The `-urlprefix` value is used for the call URL shown in the UI. See [Webhook Parameters](docs/en-US/Webhook-Parameters.md) and [Config UI](cmd/README.md).
152158
- **HTTPS**: Use a reverse proxy (nginx, Traefik, Caddy) for HTTPS support
153159
- **CORS**: Set custom headers including CORS headers with `-header name=value`
154160
- **Hot Reload**: Update configurations without restarting using `-hotreload` or `kill -USR1`
@@ -159,7 +165,7 @@ For more examples and use cases, check out [Hook Examples](docs/en-US/Hook-Examp
159165

160166
### Core Documentation
161167
- [Hook Definition](docs/en-US/Hook-Definition.md) - Complete hook configuration reference
162-
- [Config UI](cmd/README.md) - Config generator (run with `go run . -config-ui` for config-ui-only mode)
168+
- [Config UI](cmd/README.md) - Config generator (enable with `go run . -config-ui`)
163169
- [Hook Rules](docs/en-US/Hook-Rules.md) - Trigger rules and conditions
164170
- [Webhook Parameters](docs/en-US/Webhook-Parameters.md) - Command-line arguments and configuration
165171
- [Templates](docs/en-US/Templates.md) - Using Go templates in configurations

SECURITY.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Webhook includes several security features to help prevent command injection att
3232
- Set appropriate limits for argument length and count
3333
- Regularly review and update your whitelist
3434
- Never enable `--allow-auto-chmod` in production (it's a security risk)
35-
- Do not enable `--openapi` or `--config-ui` on publicly reachable servers (they have no authentication; use only for debugging or intranet). Config UI can run as config-ui-only (`-config-ui` without `-hooks`) or mounted on the webhook server; in both cases restrict access.
35+
- Do not enable `--openapi` or `--config-ui` on publicly reachable servers (they have no authentication; use only for debugging or intranet). Config UI is mounted on the webhook server; restrict access with firewall/reverse proxy.
3636

3737
For more details, see the [Webhook Parameters (English)](docs/en-US/Webhook-Parameters.md) or [配置参数 (中文)](docs/zh-CN/Webhook-Parameters.md).
3838

cmd/README.md

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,27 @@
11
# Webhook Config UI
22

3-
在网页中快速生成可调用的 webhook 配置(YAML/JSON 片段)及调用 URL、curl 示例,便于复制到 `hooks.yaml` / `hooks.json` 或直接调用
3+
在网页中快速生成可调用的 webhook 配置(YAML/JSON 片段)及调用 URL、curl 示例,便于复制到 `./hooks` 目录(默认)或单文件配置中使用
44

5-
**同一套 UI 也可在主程序中启用**:使用 `./webhook -hooks hooks.json -config-ui` 启动 webhook 后,访问 `http://localhost:9000/config-ui` 即可。详见 [配置参数](../docs/zh-CN/Webhook-Parameters.md)
5+
**同一套 UI 也可在主程序中启用**:使用 `./webhook -config-ui`(默认目录模式)或 `./webhook -hooks hooks.json -config-ui`(单文件模式)启动后,访问页面即可。详见 [配置参数](../docs/zh-CN/Webhook-Parameters.md)
66

77
## 能力边界与适用场景
88

99
- **适合谁**:需要在本地或内网快速生成/调整 hook 配置的开发者或运维;希望用表单和「插入示例」减少手写 JSON/YAML 的情况。
10-
- **做什么**:生成单条 hook 的 YAML/JSON 片段、调用 URL 与 curl 示例;在启用 `-hooks-dir` 时可将生成结果保存到该目录。不提供配置版本管理、回滚、多用户或权限控制。
10+
- **做什么**:生成单条 hook 的 YAML/JSON 片段、调用 URL 与 curl 示例;在目录模式(默认 `./hooks` 或显式 `-hooks-dir`)下可将生成结果保存到该目录。不提供配置版本管理、回滚、多用户或权限控制。
1111
- **不做啥**:Config UI 是**配置生成器**,不是完整的配置托管或管控平台;不会替代 `-hooks` 指定的文件、不会自动重载或校验已有配置的语法。
1212

1313
## 运行方式
1414

15-
与主程序共用同一二进制,**仅根据参数决定模式**
16-
17-
- **仅 Config UI 模式**:不指定 `-hooks` 且启用 `-config-ui` 时,只启动 Config UI 服务(默认端口 9080)。
18-
- **Webhook + Config UI**:指定 `-hooks` 并启用 `-config-ui` 时,在主服务上挂载 Config UI(如 `http://localhost:9000/config-ui`)。
15+
与主程序共用同一二进制,启用后会在主服务上挂载 Config UI(默认端口 `9000`):
1916

2017
```bash
21-
# 仅 Config UI:使用默认端口 9080(不传 -hooks)
18+
# 默认端口 9000
2219
go run . -config-ui
2320

24-
# 仅 Config UI,指定端口
21+
# 指定端口
2522
go run . -config-ui -port 9090
2623

27-
# 通过环境变量(不设置 HOOKS 时即为仅 Config UI 模式,默认端口 9080)
24+
# 通过环境变量
2825
CONFIG_UI_ENABLED=true go run .
2926
PORT=9090 CONFIG_UI_ENABLED=true go run .
3027
```
@@ -34,14 +31,14 @@ PORT=9090 CONFIG_UI_ENABLED=true go run .
3431
```bash
3532
go build -o webhook .
3633
./webhook -config-ui
37-
./webhook -config-ui -port 9080
34+
./webhook -config-ui -port 9090
3835
```
3936

40-
发布产物中的 `webhook-config-ui``webhook` 为同一程序,用法一致。仅 Config UI 时运行 `./webhook -config-ui``./webhook-config-ui -config-ui`(均不传 `-hooks`即可,默认监听 9080
37+
发布产物中的 `webhook-config-ui``webhook` 为同一程序,用法一致。运行 `./webhook -config-ui``./webhook-config-ui -config-ui` 即可,默认监听 9000
4138

4239
## 使用说明
4340

44-
1. 浏览器打开 `http://localhost:9080`(或你指定的端口)。
41+
1. 浏览器打开 `http://localhost:9000/config-ui`(或你指定的端口/路径)。
4542
2. 填写表单:
4643
- **必填**:Hook ID、执行命令
4744
- **可选**:工作目录、响应消息、**Webhook 服务地址**(如 `http://localhost:9000`,用于生成正确的调用 URL)、HTTP 方法、成功状态码、是否返回命令输出等
@@ -52,14 +49,15 @@ go build -o webhook .
5249
- curl 示例
5350
- YAML 与 JSON 配置片段
5451
5. 可复制或下载 YAML/JSON 片段,粘贴到 webhook 的 `hooks.yaml` / `hooks.json` 中使用。可选区块(响应头、传递参数、触发规则等)默认折叠,点击「可选」展开;高级 JSON 字段旁有「插入示例」可填入最小合法示例。YAML/JSON 结果块可折叠以节省空间。成功生成后会记住 Hook ID 与 Webhook 服务地址(localStorage),下次打开页面时自动回填(若为空)。
55-
6. **下一步**:将配置复制到 hooks 文件;若启动时使用了 `-hooks-dir`,可在结果区直接保存到该目录;请确认 `-urlprefix` 与页面上方生成的调用 URL 前缀一致,避免调用地址与真实端点不符。
52+
6. **下一步**:将配置复制到 hooks 文件;目录模式(默认 `./hooks` 或显式 `-hooks-dir`)下可在结果区直接保存到该目录;显式 `-hooks` 单文件模式下请复制/下载后手动保存。请确认 `-urlprefix` 与页面上方生成的调用 URL 前缀一致,避免调用地址与真实端点不符。
5653

5754
## 与 webhook 同机部署
5855

59-
仅 Config UI 与 webhook 主程序可分开监听端口(同一二进制不同参数)
56+
与 webhook 主服务共用监听端口
6057

61-
- webhook 主服务:例如 `./webhook -hooks hooks.json``:9000`(hooks 端点 `/hooks/:id`
62-
- 仅 Config UI:例如 `./webhook -config-ui``:9080`(仅提供配置生成页)
58+
- webhook 主服务(默认目录模式):例如 `./webhook``:9000`(hooks 端点 `/hooks/:id`
59+
- webhook 主服务 + Config UI:例如 `./webhook -config-ui``:9000`(UI 路径默认 `/config-ui`
60+
- webhook 主服务(单文件兼容模式):例如 `./webhook -hooks hooks.json -config-ui``:9000`
6361

6462
可编写 `docker-compose.yml` 或 systemd 单元,分别启动两个进程;生成结果中的「调用 URL」需与 webhook 实际监听地址一致(可在生成后手动替换 host/port)。
6563

docs/en-US/API-Reference.md

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -111,24 +111,20 @@ You can also print the spec to stdout at startup with `-openapi-print` (e.g. `./
111111

112112
**Endpoints:** `GET /config-ui`, `GET /config-ui/`, `POST /config-ui/api/generate` (or custom path via `-config-ui-path`)
113113

114-
**Availability:** Config UI is available in two ways: (1) Config-ui-only mode: `-config-ui` without `-hooks` (default port 9080); (2) Mounted on webhook server: `-hooks` with `-config-ui` (path via `-config-ui-path`). Not exposed by default; recommend use only for debugging or intranet.
114+
**Availability:** Config UI is mounted on the webhook server when `-config-ui` is enabled (path via `-config-ui-path`, default `/config-ui`). Not exposed by default; recommend use only for debugging or intranet.
115115

116-
**Description:** Web UI and API for generating hook configuration (YAML/JSON) and call examples. Same functionality as config-ui-only mode (run with `-config-ui` and no `-hooks`).
116+
**Description:** Web UI and API for generating hook configuration (YAML/JSON) and call examples.
117117

118118
- **GET** `{config-ui-path}` or `{config-ui-path}/`: Returns the config generator HTML page.
119119
- **GET** `{config-ui-path}/static/*`: Static assets (CSS, JS).
120120
- **POST** `{config-ui-path}/api/generate`: Accepts JSON body with form fields (e.g. `id`, `execute-command`, `response-message`, `trigger-rule`). Returns `{ "yaml", "json", "callUrl", "curlExample" }` on success, or `{ "error": "..." }` with 4xx on validation error.
121-
- **GET** `{config-ui-path}/api/capabilities`: Returns `{ "saveToDir": true|false }`. When `true`, the UI shows a "Save to directory" option (requires `-hooks-dir`).
122-
- **POST** `{config-ui-path}/api/save`: Writes generated config to the directory set by `-hooks-dir`. Body: `{ "filename": "name.yaml", "content": "..." }`. Returns `{ "ok": "<absolute-path>" }` on success, or `{ "error": "..." }` with 4xx when disabled or invalid (e.g. path traversal). Filename must have extension `.json`, `.yaml`, or `.yml`.
121+
- **GET** `{config-ui-path}/api/capabilities`: Returns `{ "saveToDir": true|false }`. When `true`, the UI shows a "Save to directory" option (directory mode: default `./hooks` or explicit `-hooks-dir`).
122+
- **POST** `{config-ui-path}/api/save`: Writes generated config to hooks directory. Body: `{ "filename": "name.yaml", "content": "...", "format": "yaml|json" }`. Returns `{ "ok": "<absolute-path>" }` on success, or `{ "error": "..." }` with 4xx when disabled or invalid (e.g. path traversal, format mismatch, invalid hook config). Filename must have extension `.json`, `.yaml`, or `.yml`.
123123

124124
**Example:**
125125
```bash
126-
# Config-ui-only mode (default port 9080)
126+
# Enable Config UI on webhook server (default port 9000)
127127
./webhook -config-ui
128-
# Open http://localhost:9080 in a browser
129-
130-
# Or mount Config UI on the webhook server
131-
./webhook -hooks hooks.json -config-ui
132128
# Open http://localhost:9000/config-ui in a browser
133129
```
134130

0 commit comments

Comments
 (0)