feat: 引入 godotenv 管理环境变量,优化 Dockerfile 构建

- 新增 .env.example 和 .gitignore
- config.go 使用 godotenv 加载 .env,移除硬编码凭据
- docker-compose.yml 改用 env_file 引用 .env
- Dockerfile 使用国内镜像源 + 缓存加速构建
- README.md Hook URL 改为服务器地址,配置说明更新
This commit is contained in:
2026-06-21 22:01:38 +08:00
parent 2566f9f4aa
commit 075a8bf40b
8 changed files with 78 additions and 32 deletions
+26 -20
View File
@@ -35,13 +35,18 @@ cd cc-hook
### 2. 配置环境变量
编辑 `docker-compose.yml`,修改以下配置:
复制 `.env.example` 并填写实际配置:
```yaml
environment:
- GOTIFY_URL=http://your-gotify-server:port # Gotify 服务器地址
- GOTIFY_TOKEN=your-token-here # Gotify 应用 Token
- PORT=:8082 # 服务监听端口(默认 8082)
```bash
cp .env.example .env
```
编辑 `.env`:
```env
GOTIFY_URL=http://your-gotify-server:port # Gotify 服务器地址
GOTIFY_TOKEN=your-token-here # Gotify 应用 Token
PORT=:8082 # 服务监听端口(默认 8082)
```
### 3. 启动服务
@@ -60,13 +65,13 @@ docker-compose up -d
"Notification": [
{
"type": "command",
"command": "curl -s -X POST http://localhost:8082/hooks -H 'Content-Type: application/json' -d @-"
"command": "curl -s -X POST http://47.121.181.112:8082/hooks -H 'Content-Type: application/json' -d @-"
}
],
"Stop": [
{
"type": "command",
"command": "curl -s -X POST http://localhost:8082/hooks -H 'Content-Type: application/json' -d @-"
"command": "curl -s -X POST http://47.121.181.112:8082/hooks -H 'Content-Type: application/json' -d @-"
}
]
}
@@ -93,11 +98,11 @@ docker-compose up -d
## 配置说明
| 环境变量 | 默认值 | 说明 |
|----------|--------|------|
| `GOTIFY_URL` | `http://47.121.181.112:40266` | Gotify 服务器地址 |
| `GOTIFY_TOKEN` | `AiousrPBE4Cn04C` | Gotify 应用 Token |
| `PORT` | `:8082` | 服务监听端口 |
| 环境变量 | 必填 | 说明 |
|----------|------|------|
| `GOTIFY_URL` | 是 | Gotify 服务器地址 |
| `GOTIFY_TOKEN` | 是 | Gotify 应用 Token |
| `PORT` | 否 | 服务监听端口(默认 `:8082`) |
## API 端点
@@ -137,11 +142,10 @@ docker-compose up -d
# 安装依赖
go mod tidy
# 设置环境变量
export GOTIFY_URL=http://your-gotify-server:port
export GOTIFY_TOKEN=your-token
# 复制并编辑配置
cp .env.example .env
# 运行
# 运行(自动加载 .env)
go run .
```
@@ -149,15 +153,15 @@ go run .
```bash
# 健康检查
curl http://localhost:8082/health
curl http://47.121.181.112:8082/health
# 模拟 Notification 事件
curl -X POST http://localhost:8082/hooks \
curl -X POST http://47.121.181.112:8082/hooks \
-H 'Content-Type: application/json' \
-d '{"session_id":"test","cwd":"/tmp","hook_event_name":"Notification","matcher":"permission_prompt"}'
# 模拟 Stop 事件
curl -X POST http://localhost:8082/hooks \
curl -X POST http://47.121.181.112:8082/hooks \
-H 'Content-Type: application/json' \
-d '{"session_id":"test","cwd":"/tmp","hook_event_name":"Stop"}'
```
@@ -166,6 +170,8 @@ curl -X POST http://localhost:8082/hooks \
```
cc-hook/
├── .env.example # 环境变量示例
├── .gitignore # Git 忽略规则
├── config.go # 配置管理(环境变量读取)
├── gotify.go # Gotify HTTP 客户端
├── handler.go # Hook 事件处理器