From 075a8bf40bb7f4f378378f4a3337cca2d3ac0a20 Mon Sep 17 00:00:00 2001 From: wonder Date: Sun, 21 Jun 2026 22:01:38 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=BC=95=E5=85=A5=20godotenv=20?= =?UTF-8?q?=E7=AE=A1=E7=90=86=E7=8E=AF=E5=A2=83=E5=8F=98=E9=87=8F=EF=BC=8C?= =?UTF-8?q?=E4=BC=98=E5=8C=96=20Dockerfile=20=E6=9E=84=E5=BB=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增 .env.example 和 .gitignore - config.go 使用 godotenv 加载 .env,移除硬编码凭据 - docker-compose.yml 改用 env_file 引用 .env - Dockerfile 使用国内镜像源 + 缓存加速构建 - README.md Hook URL 改为服务器地址,配置说明更新 --- .env.example | 8 ++++++++ .gitignore | 1 + Dockerfile | 30 +++++++++++++++++++++++++----- README.md | 46 ++++++++++++++++++++++++++-------------------- config.go | 15 ++++++++++++--- docker-compose.yml | 6 ++---- go.mod | 2 ++ go.sum | 2 ++ 8 files changed, 78 insertions(+), 32 deletions(-) create mode 100644 .env.example create mode 100644 .gitignore create mode 100644 go.sum diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..8bd6248 --- /dev/null +++ b/.env.example @@ -0,0 +1,8 @@ +# Gotify 服务器地址 +GOTIFY_URL=http://47.121.181.112:40266 + +# Gotify 应用 Token +GOTIFY_TOKEN=your-gotify-token + +# 服务监听端口 +PORT=:8082 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4c49bd7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.env diff --git a/Dockerfile b/Dockerfile index 8f8034e..26001a5 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,12 +1,32 @@ FROM golang:1.22-alpine AS builder -WORKDIR /app -COPY go.mod ./ -COPY *.go ./ -RUN go build -o cc-hook . +# 使用国内镜像源加速 apk +RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories +# 设置 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 -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/ EXPOSE 8082 diff --git a/README.md b/README.md index 65fb253..782e1ec 100644 --- a/README.md +++ b/README.md @@ -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 事件处理器 diff --git a/config.go b/config.go index f4ec87e..2899dc6 100644 --- a/config.go +++ b/config.go @@ -1,6 +1,15 @@ package main -import "os" +import ( + "os" + + "github.com/joho/godotenv" +) + +func init() { + // 加载 .env 文件(如果存在),环境变量优先 + _ = godotenv.Load() +} type Config struct { GotifyURL string @@ -10,8 +19,8 @@ type Config struct { func LoadConfig() Config { return Config{ - GotifyURL: getEnv("GOTIFY_URL", "http://47.121.181.112:40266"), - GotifyToken: getEnv("GOTIFY_TOKEN", "AiousrPBE4Cn04C"), + GotifyURL: getEnv("GOTIFY_URL", ""), + GotifyToken: getEnv("GOTIFY_TOKEN", ""), Port: getEnv("PORT", ":8082"), } } diff --git a/docker-compose.yml b/docker-compose.yml index 99466b1..81180d7 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -4,8 +4,6 @@ services: container_name: cc-hook ports: - "8082:8082" - environment: - - GOTIFY_URL=http://47.121.181.112:40266 - - GOTIFY_TOKEN=AiousrPBE4Cn04C - - PORT=:8082 + env_file: + - .env restart: unless-stopped diff --git a/go.mod b/go.mod index 1cadad4..270311c 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module cc-hook go 1.22.2 + +require github.com/joho/godotenv v1.5.1 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..d61b19e --- /dev/null +++ b/go.sum @@ -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=