#!/usr/bin/env bash # Helper: create KV + print next steps for Email Worker set -euo pipefail ROOT="$(cd "$(dirname "$0")/.." && pwd)" cd "$ROOT/cf-email-worker" if ! command -v npx >/dev/null 2>&1; then echo "需要 Node.js / npx。先安装: https://nodejs.org/" exit 1 fi echo "[1/4] wrangler login (如已登录可跳过)" npx wrangler login || true echo "[2/4] 创建 KV namespace GROK_MAIL_CODES" OUT=$(npx wrangler kv namespace create GROK_MAIL_CODES 2>&1 || true) echo "$OUT" ID=$(echo "$OUT" | sed -n 's/.*id *= *"\([^"]*\)".*/\1/p' | head -1) if [[ -z "${ID}" ]]; then ID=$(echo "$OUT" | grep -oE '[a-f0-9]{32}' | head -1 || true) fi if [[ -n "${ID}" ]]; then echo "检测到 KV id=$ID — 写入 wrangler.toml" if grep -q 'REPLACE_WITH_KV_NAMESPACE_ID' wrangler.toml; then sed -i "s/REPLACE_WITH_KV_NAMESPACE_ID/${ID}/g" wrangler.toml fi else echo "未能自动解析 KV id,请手动填 wrangler.toml" fi echo "[3/4] 设置 CODE_API_KEY secret" echo "将提示你输入密钥(与 config gmail_worker_api_key 一致)" npx wrangler secret put CODE_API_KEY if [[ -n "${GMAIL_FORWARD_TO:-}" ]]; then echo "[3b] 设置 GMAIL_FORWARD_TO=$GMAIL_FORWARD_TO" printf '%s' "$GMAIL_FORWARD_TO" | npx wrangler secret put GMAIL_FORWARD_TO else echo "[3b] 跳过 GMAIL_FORWARD_TO(可 export GMAIL_FORWARD_TO=you@gmail.com 后重跑)" fi echo "[4/4] deploy" npx wrangler deploy echo echo "完成。接下来:" echo " 1) Email Routing catch-all → 绑定本 Worker" echo " 2) config.json 设置:" echo " gmail_worker_enabled=true" echo " gmail_worker_url=https://.workers.dev" echo " gmail_worker_api_key=<你的 CODE_API_KEY>" echo " 3) 测试: python scripts/test_gmail_worker.py --to test@yourdomain.com"