diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..589875b --- /dev/null +++ b/.dockerignore @@ -0,0 +1,3 @@ +* +!Dockerfile +!docker-entrypoint.sh diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..90e4911 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +docker-entrypoint.sh text eol=lf diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..f5d34c9 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,12 @@ +FROM golang:1.24-alpine + +RUN apk add --no-cache ca-certificates + +WORKDIR /app + +COPY docker-entrypoint.sh /usr/local/bin/docker-entrypoint +RUN chmod 0755 /usr/local/bin/docker-entrypoint + +EXPOSE 8080 + +ENTRYPOINT ["/usr/local/bin/docker-entrypoint"] diff --git a/README.md b/README.md index e4c0eec..3a54e2e 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,41 @@ go build -o opencode2api ./ ``` +## Docker Compose 部署 + +服务器安装 Docker 与 Docker Compose 后,可以在克隆项目后直接启动: + +```bash +git clone <项目地址> +cd opencode2api +docker compose up -d +``` + +容器使用 `golang:1.24-alpine`,并将当前项目目录映射到容器内的 `/app`。首次启动会在容器内编译源码,并把生成的二进制文件保存到持久化 Docker volume。后续启动时如果 Go 源码、`go.mod`、`go.sum` 与 Go 版本均未变化,会直接运行已有二进制;源码发生变化时会自动重新编译。 + +首次启动没有 `config.json` 时,容器会使用 `config.example.json`,因此服务可以正常启动,但示例 key 不能用于实际请求。配置真实 key: + +```bash +cp config.example.json config.json +# 编辑 config.json 中的 server_keys、zen_keys 或 go_keys +docker compose restart +``` + +Compose 会让服务在容器内监听 `0.0.0.0:8080`,无需修改配置文件里的 `listen`。默认映射到宿主机的 `8080` 端口: + +```bash +curl http://127.0.0.1:8080/healthz +docker compose logs -f +``` + +如需修改宿主机端口: + +```bash +OPENCODE2API_PORT=18080 docker compose up -d +``` + +普通的 `docker compose down` 会保留已编译二进制和 Go 构建缓存;使用 `docker compose down -v` 才会同时删除这些持久化 volume。 + ## 配置 复制示例配置: diff --git a/compose.yaml b/compose.yaml new file mode 100644 index 0000000..fab3c19 --- /dev/null +++ b/compose.yaml @@ -0,0 +1,28 @@ +services: + opencode2api: + build: + context: . + dockerfile: Dockerfile + image: opencode2api:local + restart: unless-stopped + environment: + CONFIG_PATH: /app/config.json + LISTEN_ADDRESS: 0.0.0.0:8080 + ports: + - "${OPENCODE2API_PORT:-8080}:8080" + volumes: + - .:/app + - opencode2api-state:/var/lib/opencode2api + - go-build-cache:/root/.cache/go-build + - go-module-cache:/go/pkg/mod + healthcheck: + test: ["CMD", "wget", "-q", "-O", "/dev/null", "http://127.0.0.1:8080/healthz"] + interval: 30s + timeout: 5s + retries: 3 + start_period: 20s + +volumes: + opencode2api-state: + go-build-cache: + go-module-cache: diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh new file mode 100644 index 0000000..ab6bc8c --- /dev/null +++ b/docker-entrypoint.sh @@ -0,0 +1,52 @@ +#!/bin/sh +set -eu + +app_dir=${APP_DIR:-/app} +state_dir=${STATE_DIR:-/var/lib/opencode2api} +config_path=${CONFIG_PATH:-$app_dir/config.json} +listen_address=${LISTEN_ADDRESS:-0.0.0.0:8080} +binary_path=$state_dir/opencode2api +fingerprint_path=$state_dir/source.sha256 + +mkdir -p "$state_dir" + +if [ -f "$config_path" ]; then + active_config=$config_path +else + active_config=$app_dir/config.example.json + printf '%s\n' "config.json not found; starting with config.example.json. Copy it to config.json and set real keys before sending API requests." +fi + +source_fingerprint() { + { + go version + go env GOOS GOARCH CGO_ENABLED + find "$app_dir" \ + -path "$app_dir/.git" -prune -o \ + -type f \( -name '*.go' -o -name 'go.mod' -o -name 'go.sum' \) -print \ + | LC_ALL=C sort \ + | while IFS= read -r source_file; do + sha256sum "$source_file" + done + } | sha256sum | awk '{print $1}' +} + +current_fingerprint=$(source_fingerprint) +stored_fingerprint="" +if [ -f "$fingerprint_path" ]; then + stored_fingerprint=$(cat "$fingerprint_path") +fi + +if [ ! -x "$binary_path" ] || [ "$current_fingerprint" != "$stored_fingerprint" ]; then + printf '%s\n' "Source changed or no cached binary exists; building opencode2api..." + temporary_binary=$state_dir/opencode2api.new + cd "$app_dir" + go build -trimpath -o "$temporary_binary" ./ + mv "$temporary_binary" "$binary_path" + printf '%s\n' "$current_fingerprint" > "$fingerprint_path" + printf '%s\n' "Build completed and cached in the persistent Docker volume." +else + printf '%s\n' "Source is unchanged; starting the cached opencode2api binary." +fi + +exec "$binary_path" -config "$active_config" -listen "$listen_address" diff --git a/main.go b/main.go index 451fc58..2e9cc3c 100644 --- a/main.go +++ b/main.go @@ -15,6 +15,7 @@ var version = "dev" func main() { configPath := flag.String("config", "config.json", "path to config.json") + listen := flag.String("listen", "", "override the configured listen address") flag.Parse() cfg, err := LoadConfig(*configPath) @@ -22,6 +23,9 @@ func main() { slog.Error("configuration error", "error", err) os.Exit(1) } + if *listen != "" { + cfg.Listen = *listen + } level := slog.LevelInfo if cfg.Logging.Level == "debug" { level = slog.LevelDebug