Files
2026-10-29 16:27:16 +08:00

82 lines
2.6 KiB
Bash
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 -euo pipefail
cd "$(dirname "$0")"
if [ ! -f backend/main.py ]; then
echo "[LinkUp] 未找到 backend/main.py,请确认部署包已完整解压"
exit 1
fi
PYTHON_BIN="${PYTHON_BIN:-python3}"
if ! command -v "${PYTHON_BIN}" >/dev/null 2>&1; then
PYTHON_BIN="python"
fi
if ! command -v "${PYTHON_BIN}" >/dev/null 2>&1; then
echo "[LinkUp] 未找到 Python 3.11+,请先安装 Python"
exit 1
fi
if ! "${PYTHON_BIN}" -c 'import sys; raise SystemExit(0 if sys.version_info >= (3, 11) else 1)'; then
echo "[LinkUp] 需要 Python 3.11+"
exit 1
fi
if ! "${PYTHON_BIN}" -c 'import fastapi, sqlalchemy, aiosqlite' >/dev/null 2>&1; then
echo "[LinkUp] 缺少 Python 依赖,请执行:${PYTHON_BIN} -m pip install -r backend/requirements.txt"
exit 1
fi
RUN_DIR="$(pwd)/.run"
PID_FILE="${RUN_DIR}/linkup.pid"
LOG_FILE="${RUN_DIR}/linkup.log"
PORT="${PORT:-34173}"
WORKERS="${APP_WORKERS:-2}"
is_running(){ [ -f "${PID_FILE}" ] && kill -0 "$(cat "${PID_FILE}")" 2>/dev/null; }
start_fg(){
echo "[LinkUp] FastAPI 前台启动(端口 ${PORT},workers ${WORKERS})"
(cd backend && PORT="${PORT}" APP_WORKERS="${WORKERS}" exec "${PYTHON_BIN}" -m uvicorn main:app --host 0.0.0.0 --port "${PORT}" --workers "${WORKERS}") &
SERVER_PID=$!
cleanup(){ kill "${SERVER_PID}" 2>/dev/null || true; }
trap cleanup EXIT INT TERM
echo "[LinkUp] 就绪:http://<主机IP>:${PORT} · 管理端 /admin.html · Ctrl+C 停止"
wait
}
start_bg(){
if is_running; then
echo "[LinkUp] 已在运行(PID $(cat "${PID_FILE}")),如需重启请先执行 bash start.sh stop"
exit 0
fi
mkdir -p "${RUN_DIR}"
(cd backend && PORT="${PORT}" APP_WORKERS="${WORKERS}" nohup "${PYTHON_BIN}" -m uvicorn main:app --host 0.0.0.0 --port "${PORT}" --workers "${WORKERS}" >> "${LOG_FILE}" 2>&1 & echo $! > "${PID_FILE}")
sleep 1.5
if is_running && curl -s -m 3 "http://127.0.0.1:${PORT}/api/health" | grep -q '"ok":true'; then
echo "[LinkUp] FastAPI 已就绪:http://<主机IP>:${PORT} · /admin.html"
else
echo "[LinkUp] 启动失败,请查看日志:tail -30 ${LOG_FILE}"
exit 1
fi
}
stop_svc(){
if is_running; then
kill "$(cat "${PID_FILE}")" 2>/dev/null || true
sleep 1
fi
rm -f "${PID_FILE}"
echo "[LinkUp] 已停止"
}
status_svc(){
if is_running; then echo "[LinkUp] 运行中(PID $(cat "${PID_FILE}"),端口 ${PORT})"; else echo "[LinkUp] 未运行"; fi
}
case "${1:-fg}" in
start) start_bg ;;
stop) stop_svc ;;
restart) stop_svc; start_bg ;;
status) status_svc ;;
log) tail -f "${LOG_FILE}" ;;
*) start_fg ;;
esac