#!/usr/bin/env bash # WeChat iLink Gateway — one-click start (Linux / macOS) set -euo pipefail cd "$(dirname "$0")" ensure_bun() { if command -v bun >/dev/null 2>&1; then return fi echo "未检测到 Bun,正在安装..." curl -fsSL https://bun.sh/install | bash export BUN_INSTALL="${HOME}/.bun" export PATH="$BUN_INSTALL/bin:$PATH" if ! command -v bun >/dev/null 2>&1; then echo "Bun 安装后仍不可用,请重新打开终端后再运行。" exit 1 fi } ensure_env() { if [[ ! -f .env ]]; then cp .env.example .env echo "已从 .env.example 创建 .env" fi local api_key api_key="$(grep -E '^\s*API_KEY\s*=' .env | head -n1 | sed 's/^[^=]*=//;s/^[[:space:]]*//;s/[[:space:]]*$//;s/^["'\'']//;s/["'\'']$//')" if [[ -z "$api_key" || ${#api_key} -le 12 ]]; then local generated generated="$(openssl rand -hex 12 2>/dev/null || head -c 24 /dev/urandom | xxd -p | head -c 24)" if grep -qE '^\s*API_KEY\s*=' .env; then # portable in-place replace tmp="$(mktemp)" sed "s/^[[:space:]]*API_KEY=.*/API_KEY=${generated}/" .env >"$tmp" mv "$tmp" .env else echo "API_KEY=${generated}" >>.env fi echo "已自动生成 API_KEY(写入 .env): ${generated}" fi } ensure_bun ensure_env if [[ ! -d node_modules ]]; then echo "正在安装依赖 (bun install)..." bun install fi port="$(grep -E '^\s*PORT\s*=' .env | head -n1 | sed 's/^[^=]*=//;s/[[:space:]]//g' || true)" port="${port:-3456}" echo "" echo "Gateway 启动中..." echo " 控制台: http://localhost:${port}/console" echo " 状态: http://localhost:${port}/status" echo "按 Ctrl+C 停止" echo "" exec bun run start