#!/usr/bin/env bash # ============================================================ # go-sms 一键启动前后端开发环境(scripts/dev_up.sh / make dev) # 链路(全程幂等,可重复执行): # 依赖检查 → 中间件(make up) → MySQL 建库/DDL → Kafka Topics # → seed-admin(迁移+内置超管) → 网关(:GW_PORT) → Web(:WEB_PORT) # 变量(均可环境变量覆盖): # API_MODE=real|mock 前端数据模式,默认 real(mock=零依赖,仅起 Web) # GW_PORT=8080 网关端口,须与 web/vite.config.ts 的 proxy 一致 # WEB_PORT=5173 Web 端口 # WITH_CONSUMER=1 附带启动 submit 消费者(日志 logs/consumer.log) # SKIP_INIT=1 跳过 MySQL/Kafka 初始化(重复启动提速) # 停止:bash scripts/dev_down.sh 或 make dev-stop(中间件不动,make down 另清) # ============================================================ set -euo pipefail ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" cd "$ROOT" GW_PORT="${GW_PORT:-8080}" WEB_PORT="${WEB_PORT:-5173}" API_MODE="${API_MODE:-real}" C_INFO=$'\033[36m'; C_OK=$'\033[32m'; C_WARN=$'\033[33m'; C_ERR=$'\033[31m'; C_RST=$'\033[0m' info() { printf '%s\n' "${C_INFO}>>${C_RST} $*"; } ok() { printf '%s\n' "${C_OK}>>${C_RST} $*"; } warn() { printf '%s\n' "${C_WARN}!!${C_RST} $*" >&2; } die() { printf '%s\n' "${C_ERR}!!${C_RST} $*" >&2; exit 1; } case "$API_MODE" in real | mock) ;; *) die "非法 API_MODE=${API_MODE}(仅 real|mock)" ;; esac port_busy() { ss -tln 2>/dev/null | grep -q ":$1 "; } mkdir -p logs bin # ---------- 前置依赖检查 ---------- command -v curl >/dev/null 2>&1 || die "缺少 curl" command -v node >/dev/null 2>&1 || die "缺少 node(前端依赖)" if [ "$API_MODE" != mock ]; then command -v docker >/dev/null 2>&1 || die "缺少 docker(中间件容器依赖)" command -v go >/dev/null 2>&1 || die "缺少 go(后端编译依赖)" fi if [ ! -x web/node_modules/.bin/vite ]; then info "安装前端依赖(web/node_modules 缺失)..." if command -v pnpm >/dev/null 2>&1; then pnpm -C web install else npm --prefix web install; fi fi if [ "$API_MODE" = mock ]; then info "API_MODE=mock:零依赖模式,跳过中间件/网关,仅启动 Web" else # ---------- 中间件 ---------- info "[1/6] 启动开发中间件容器(make up,幂等)..." make up if docker ps --format '{{.Names}}' | grep -qx go-sms-kafka; then ok "外部 Kafka 容器 go-sms-kafka 运行中" elif docker ps -a --format '{{.Names}}' | grep -qx go-sms-kafka; then info "启动外部 Kafka 容器 go-sms-kafka(存在但未运行)..." docker start go-sms-kafka else warn "外部 Kafka 容器 go-sms-kafka 不存在,短信收发链路不可用" warn " (Kafka 自 2026-09-11 起由手动 docker run 承载,背景见 docker-compose.dev.yaml 头注释)" fi # ---------- 存储与队列初始化(幂等) ---------- if [ "${SKIP_INIT:-0}" = 1 ]; then info "[2/6] SKIP_INIT=1:跳过 MySQL/Kafka 初始化" info "[3/6] SKIP_INIT=1:跳过 Kafka Topics 预创建" else info "[2/6] 初始化 MySQL(建库 + 幂等 DDL)..." make init-mysql init-schema info "[3/6] 预创建 Kafka Topics(幂等,--if-not-exists)..." make init-topics fi # ---------- 迁移 + 内置超管 ---------- info "[4/6] 执行迁移 + 内置超管引导(已有账号则跳过)..." go run ./cmd/migrate -config configs/config.dev.yaml seed-admin 2>&1 | tee logs/seed-admin.log # ---------- 网关 ---------- info "[5/6] 启动网关(:${GW_PORT})..." if curl -sf -m 2 -o /dev/null "http://127.0.0.1:${GW_PORT}/health"; then ok "网关已在 :${GW_PORT} 运行(health OK),跳过" elif port_busy "$GW_PORT"; then die ":${GW_PORT} 被其他进程占用且 /health 不通。处理:释放该端口,或 GW_PORT=<其他端口> 并同步修改 web/vite.config.ts 的 proxy target" else make gw-start GW_PORT="$GW_PORT" fi # ---------- submit 消费者(可选) ---------- if [ "${WITH_CONSUMER:-0}" = 1 ]; then if [ -f logs/consumer.pid ] && kill -0 "$(cat logs/consumer.pid)" 2>/dev/null; then ok "submit 消费者已在运行(pid=$(cat logs/consumer.pid)),跳过" else info "[+] 启动 submit 消费者(WITH_CONSUMER=1)..." go build -o bin/consumer ./cmd/consumer nohup env GOSMS_LOG_FILE_PATH=./logs/consumer.log ./bin/consumer \ > logs/consumer-run.log 2>&1 & echo $! > logs/consumer.pid ok "submit 消费者已启动(pid=$(cat logs/consumer.pid),日志 logs/consumer.log)" fi fi fi # ---------- Web ---------- info "[6/6] 启动 Web(:${WEB_PORT},API_MODE=${API_MODE})..." printf 'VITE_API_MODE=%s\n' "$API_MODE" > web/.env.development.local if curl -sf -m 2 -o /dev/null "http://127.0.0.1:${WEB_PORT}/"; then ok "Web 已在 :${WEB_PORT} 运行,跳过(vite 监听 .env 变更自动重启,若页面异常请强刷)" elif port_busy "$WEB_PORT"; then die ":${WEB_PORT} 被其他进程占用且非 vite。处理:释放端口或 WEB_PORT=<其他端口>" else make web-dev WEB_PORT="$WEB_PORT" fi # ---------- 收尾 ---------- echo ok "===== 一键启动完成 =====" make svc-status GW_PORT="$GW_PORT" WEB_PORT="$WEB_PORT" || true echo info "访问: http://127.0.0.1:${WEB_PORT}(局域网用服务器 IP)" if [ "$API_MODE" = real ]; then info "登录: admin / (见上方输出或 logs/seed-admin.log)" else info "登录: Mock 模式 admin/admin123 或 customer/123456" fi info "日志: make web-log | logs/gateway-run.log | make logs(中间件)" info "停止: make dev-stop(中间件不动,make down 可完全清理)"