Files
2026-09-24 17:20:27 +08:00

212 lines
5.5 KiB
Bash
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
set -Eeuo pipefail
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
BACKEND_DIR="$ROOT_DIR/backend"
FRONTEND_DIR="$ROOT_DIR/frontend"
RUNTIME_DIR="$ROOT_DIR/.runtime"
PID_DIR="$RUNTIME_DIR/pids"
LOG_DIR="$RUNTIME_DIR/logs"
BACKEND_JAR="$BACKEND_DIR/target/momatchengine-0.0.1-SNAPSHOT.jar"
LOCAL_ENV_FILE="${LOCAL_ENV_FILE:-$ROOT_DIR/.env.local}"
command_name="${1:-start}"
if [[ -f "$LOCAL_ENV_FILE" ]]; then
set -a
# shellcheck disable=SC1090
source "$LOCAL_ENV_FILE"
set +a
fi
require_command() {
local command="$1"
if ! command -v "$command" >/dev/null 2>&1; then
echo "缺少命令:$command" >&2
exit 1
fi
}
port_in_use() {
lsof -nP -iTCP:"$1" -sTCP:LISTEN >/dev/null 2>&1
}
select_frontend_port() {
if [[ -n "${FRONTEND_PORT:-}" ]]; then
if port_in_use "$FRONTEND_PORT"; then
echo "FRONTEND_PORT=$FRONTEND_PORT 已被占用" >&2
exit 1
fi
printf '%s\n' "$FRONTEND_PORT"
return
fi
local candidate
for candidate in {8080..8090}; do
if ! port_in_use "$candidate"; then
printf '%s\n' "$candidate"
return
fi
done
echo "8080-8090 均被占用,请通过 FRONTEND_PORT 指定可用端口" >&2
exit 1
}
stop_process() {
local name="$1"
local pid_file="$PID_DIR/$name.pid"
if [[ ! -f "$pid_file" ]]; then
return
fi
local pid
pid="$(<"$pid_file")"
if kill -0 "$pid" >/dev/null 2>&1; then
echo "停止 $name (PID $pid)"
kill "$pid"
for _ in {1..20}; do
if ! kill -0 "$pid" >/dev/null 2>&1; then
break
fi
sleep 1
done
fi
rm -f "$pid_file"
}
stop_launchctl_service() {
local label="$1"
if command -v launchctl >/dev/null 2>&1; then
launchctl remove "$label" >/dev/null 2>&1 || true
fi
}
wait_for_service() {
local name="$1"
local url="$2"
local pid="$3"
local log_file="$4"
for _ in {1..60}; do
if curl --silent --show-error --output /dev/null "$url"; then
return
fi
if ! kill -0 "$pid" >/dev/null 2>&1; then
echo "$name 启动失败,最近日志:" >&2
tail -n 80 "$log_file" >&2 || true
exit 1
fi
sleep 1
done
echo "$name 启动超时,最近日志:" >&2
tail -n 80 "$log_file" >&2 || true
exit 1
}
case "$command_name" in
stop)
# 兼容由本机 launchctl 托管的开发服务实例。
stop_launchctl_service com.robotquan.momatchengine.frontend.local
stop_launchctl_service com.robotquan.momatchengine.local
stop_process frontend
stop_process backend
exit 0
;;
start)
;;
*)
echo "用法:$0 [start|stop]" >&2
exit 1
;;
esac
require_command java
require_command mvn
require_command node
require_command npm
require_command curl
require_command lsof
if [[ ! -f "$BACKEND_DIR/pom.xml" || ! -f "$FRONTEND_DIR/package.json" ]]; then
echo "未找到 backend 或 frontend 项目目录" >&2
exit 1
fi
mkdir -p "$PID_DIR" "$LOG_DIR"
if [[ -f "$PID_DIR/backend.pid" ]] && kill -0 "$(<"$PID_DIR/backend.pid")" >/dev/null 2>&1; then
echo "后端已经由 $0 启动,先执行 $0 stop 后再重启" >&2
exit 1
fi
if [[ -f "$PID_DIR/frontend.pid" ]] && kill -0 "$(<"$PID_DIR/frontend.pid")" >/dev/null 2>&1; then
echo "前端已经由 $0 启动,先执行 $0 stop 后再重启" >&2
exit 1
fi
rm -f "$PID_DIR/backend.pid" "$PID_DIR/frontend.pid"
BACKEND_PORT="${BACKEND_PORT:-8082}"
if port_in_use "$BACKEND_PORT"; then
echo "BACKEND_PORT=$BACKEND_PORT 已被占用" >&2
exit 1
fi
FRONTEND_PORT="$(select_frontend_port)"
if [[ ! -d "$FRONTEND_DIR/node_modules" || "${INSTALL_DEPS:-0}" == "1" ]]; then
echo "安装前端依赖..."
(cd "$FRONTEND_DIR" && npm ci)
fi
echo "构建后端..."
(cd "$BACKEND_DIR" && mvn -DskipTests package)
if [[ ! -f "$BACKEND_JAR" ]]; then
echo "未找到后端构建产物:$BACKEND_JAR" >&2
exit 1
fi
BACKEND_LOG="$LOG_DIR/backend.log"
FRONTEND_LOG="$LOG_DIR/frontend.log"
SPRING_PROFILES_ACTIVE="${SPRING_PROFILES_ACTIVE:-local}"
APP_WORKSPACE_ROOT="${APP_WORKSPACE_ROOT:-$BACKEND_DIR/data/projects}"
if [[ "$APP_WORKSPACE_ROOT" != /* ]]; then
echo "APP_WORKSPACE_ROOT 必须是绝对路径:$APP_WORKSPACE_ROOT" >&2
exit 1
fi
mkdir -p "$APP_WORKSPACE_ROOT"
APP_WORKSPACE_ROOT="$(cd "$APP_WORKSPACE_ROOT" && pwd -P)"
if [[ "$APP_WORKSPACE_ROOT" == "/" ]]; then
echo "APP_WORKSPACE_ROOT 不能是文件系统根目录" >&2
exit 1
fi
APP_SMS_ENABLED="${APP_SMS_ENABLED:-true}"
echo "启动后端,端口:${BACKEND_PORT},Profile:${SPRING_PROFILES_ACTIVE}"
nohup env \
SERVER_PORT="$BACKEND_PORT" \
SPRING_PROFILES_ACTIVE="$SPRING_PROFILES_ACTIVE" \
APP_WORKSPACE_ROOT="$APP_WORKSPACE_ROOT" \
APP_SMS_ENABLED="$APP_SMS_ENABLED" \
LOG_PATH="$LOG_DIR" \
java -jar "$BACKEND_JAR" >"$BACKEND_LOG" 2>&1 &
BACKEND_PID=$!
printf '%s\n' "$BACKEND_PID" > "$PID_DIR/backend.pid"
wait_for_service "后端" "http://127.0.0.1:$BACKEND_PORT/api-docs" "$BACKEND_PID" "$BACKEND_LOG"
echo "启动前端,端口:$FRONTEND_PORT"
(
cd "$FRONTEND_DIR"
export VUE_APP_API_BASE_URL=/api
export VUE_APP_API_PROXY_TARGET="${VUE_APP_API_PROXY_TARGET:-https://cadworld-api.robotquan.com}"
exec nohup npm run dev -- --host 127.0.0.1 --port "$FRONTEND_PORT"
) >"$FRONTEND_LOG" 2>&1 &
FRONTEND_PID=$!
printf '%s\n' "$FRONTEND_PID" > "$PID_DIR/frontend.pid"
wait_for_service "前端" "http://127.0.0.1:$FRONTEND_PORT" "$FRONTEND_PID" "$FRONTEND_LOG"
printf '\n前端地址:http://127.0.0.1:%s\n后端 Swagger:http://127.0.0.1:%s/swagger-ui.html\n日志目录:%s\n停止命令:%s stop\n' \
"$FRONTEND_PORT" "$BACKEND_PORT" "$LOG_DIR" "$0"