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
+8
View File
@@ -0,0 +1,8 @@
# Gotify 服务器地址
GOTIFY_URL=http://47.121.181.112:40266
# Gotify 应用 Token
GOTIFY_TOKEN=your-gotify-token
# 服务监听端口
PORT=:8082
+1
View File
@@ -0,0 +1 @@
.env
+25 -5
View File
@@ -1,12 +1,32 @@
FROM golang:1.22-alpine AS builder FROM golang:1.22-alpine AS builder
WORKDIR /app # 使用国内镜像源加速 apk
COPY go.mod ./ RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
COPY *.go ./
RUN go build -o cc-hook .
# 设置 Go 模块代理和国内镜像
ENV GOPROXY=https://goproxy.cn,direct
ENV GO111MODULE=on
WORKDIR /app
# 先复制依赖文件,利用 Docker 缓存层
COPY go.mod go.sum ./
RUN --mount=type=cache,target=/go/pkg/mod \
go mod download
# 复制源码并构建
COPY *.go ./
RUN --mount=type=cache,target=/go/pkg/mod \
--mount=type=cache,target=/root/.cache/go-build \
go build -o cc-hook .
# 运行阶段
FROM alpine:latest FROM alpine:latest
RUN apk --no-cache add ca-certificates
# 运行阶段也使用国内镜像源
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories \
&& apk --no-cache add ca-certificates
COPY --from=builder /app/cc-hook /usr/local/bin/ COPY --from=builder /app/cc-hook /usr/local/bin/
EXPOSE 8082 EXPOSE 8082
+26 -20
View File
@@ -35,13 +35,18 @@ cd cc-hook
### 2. 配置环境变量 ### 2. 配置环境变量
编辑 `docker-compose.yml`,修改以下配置: 复制 `.env.example` 并填写实际配置:
```yaml ```bash
environment: cp .env.example .env
- GOTIFY_URL=http://your-gotify-server:port # Gotify 服务器地址 ```
- GOTIFY_TOKEN=your-token-here # Gotify 应用 Token
- PORT=:8082 # 服务监听端口(默认 8082) 编辑 `.env`:
```env
GOTIFY_URL=http://your-gotify-server:port # Gotify 服务器地址
GOTIFY_TOKEN=your-token-here # Gotify 应用 Token
PORT=:8082 # 服务监听端口(默认 8082)
``` ```
### 3. 启动服务 ### 3. 启动服务
@@ -60,13 +65,13 @@ docker-compose up -d
"Notification": [ "Notification": [
{ {
"type": "command", "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": [ "Stop": [
{ {
"type": "command", "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_URL` | 是 | Gotify 服务器地址 |
| `GOTIFY_TOKEN` | `AiousrPBE4Cn04C` | Gotify 应用 Token | | `GOTIFY_TOKEN` | 是 | Gotify 应用 Token |
| `PORT` | `:8082` | 服务监听端口 | | `PORT` | 否 | 服务监听端口(默认 `:8082`) |
## API 端点 ## API 端点
@@ -137,11 +142,10 @@ docker-compose up -d
# 安装依赖 # 安装依赖
go mod tidy go mod tidy
# 设置环境变量 # 复制并编辑配置
export GOTIFY_URL=http://your-gotify-server:port cp .env.example .env
export GOTIFY_TOKEN=your-token
# 运行 # 运行(自动加载 .env)
go run . go run .
``` ```
@@ -149,15 +153,15 @@ go run .
```bash ```bash
# 健康检查 # 健康检查
curl http://localhost:8082/health curl http://47.121.181.112:8082/health
# 模拟 Notification 事件 # 模拟 Notification 事件
curl -X POST http://localhost:8082/hooks \ curl -X POST http://47.121.181.112:8082/hooks \
-H 'Content-Type: application/json' \ -H 'Content-Type: application/json' \
-d '{"session_id":"test","cwd":"/tmp","hook_event_name":"Notification","matcher":"permission_prompt"}' -d '{"session_id":"test","cwd":"/tmp","hook_event_name":"Notification","matcher":"permission_prompt"}'
# 模拟 Stop 事件 # 模拟 Stop 事件
curl -X POST http://localhost:8082/hooks \ curl -X POST http://47.121.181.112:8082/hooks \
-H 'Content-Type: application/json' \ -H 'Content-Type: application/json' \
-d '{"session_id":"test","cwd":"/tmp","hook_event_name":"Stop"}' -d '{"session_id":"test","cwd":"/tmp","hook_event_name":"Stop"}'
``` ```
@@ -166,6 +170,8 @@ curl -X POST http://localhost:8082/hooks \
``` ```
cc-hook/ cc-hook/
├── .env.example # 环境变量示例
├── .gitignore # Git 忽略规则
├── config.go # 配置管理(环境变量读取) ├── config.go # 配置管理(环境变量读取)
├── gotify.go # Gotify HTTP 客户端 ├── gotify.go # Gotify HTTP 客户端
├── handler.go # Hook 事件处理器 ├── handler.go # Hook 事件处理器
+12 -3
View File
@@ -1,6 +1,15 @@
package main package main
import "os" import (
"os"
"github.com/joho/godotenv"
)
func init() {
// 加载 .env 文件(如果存在),环境变量优先
_ = godotenv.Load()
}
type Config struct { type Config struct {
GotifyURL string GotifyURL string
@@ -10,8 +19,8 @@ type Config struct {
func LoadConfig() Config { func LoadConfig() Config {
return Config{ return Config{
GotifyURL: getEnv("GOTIFY_URL", "http://47.121.181.112:40266"), GotifyURL: getEnv("GOTIFY_URL", ""),
GotifyToken: getEnv("GOTIFY_TOKEN", "AiousrPBE4Cn04C"), GotifyToken: getEnv("GOTIFY_TOKEN", ""),
Port: getEnv("PORT", ":8082"), Port: getEnv("PORT", ":8082"),
} }
} }
+2 -4
View File
@@ -4,8 +4,6 @@ services:
container_name: cc-hook container_name: cc-hook
ports: ports:
- "8082:8082" - "8082:8082"
environment: env_file:
- GOTIFY_URL=http://47.121.181.112:40266 - .env
- GOTIFY_TOKEN=AiousrPBE4Cn04C
- PORT=:8082
restart: unless-stopped restart: unless-stopped
+2
View File
@@ -1,3 +1,5 @@
module cc-hook module cc-hook
go 1.22.2 go 1.22.2
require github.com/joho/godotenv v1.5.1
+2
View File
@@ -0,0 +1,2 @@
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=