unis_manager/run.sh

101 lines
3.4 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#!/usr/bin/env bash
# DMS管理平台 · 启动脚本
# 用法: ./run.sh 正常启动(默认开热更新)
# PORT=8771 ./run.sh 换端口启动
# DEV=0 ./run.sh 关闭热更新(改代码不自动重启、页面不自动刷新)
# NO_OPEN=1 ./run.sh 启动后不自动打开浏览器
set -euo pipefail
cd "$(dirname "$0")"
# 热更新默认开启:改 Python / 前端 / .env 自动重启服务,页面发现重启后自动刷新。
# 需要 DEV 也传给 app.config(/health 的 dev 标记决定页面是否轮询),所以这里 export 出去。
DEV="${DEV:-1}"
export DEV
PORT="${PORT:-8770}"
XLSX="${XLSX:-/Users/jiliu/WorkSpace/定开管理/软件开发部管理工作执行表.xlsx}"
STAFF_XLSX="${STAFF_XLSX:-/Users/jiliu/WorkSpace/unissense/部门管理/软件开发部人员名单.xlsx}"
echo "==> DMS管理平台"
# ---------- 1. 选择带依赖的 Python 解释器 ----------
CANDIDATES=(
"./.venv/bin/python"
"/Users/jiliu/.workbuddy/binaries/python/envs/default/bin/python"
"$(command -v python3 2>/dev/null || true)"
"/usr/bin/python3"
)
PY=""
FALLBACK=""
for c in "${CANDIDATES[@]}"; do
[ -n "$c" ] && [ -x "$c" ] || continue
[ -z "$FALLBACK" ] && FALLBACK="$c"
if "$c" -c "import fastapi, uvicorn, sqlalchemy" >/dev/null 2>&1; then
PY="$c"; break
fi
done
if [ -z "$PY" ]; then
PY="$FALLBACK"
if [ -z "$PY" ]; then
echo "!! 没有找到 Python 3,请先安装后再运行。" >&2
exit 1
fi
echo "==> 缺少依赖,正在安装到 $PY"
"$PY" -m pip install -r requirements.txt || {
echo "!! 依赖安装失败,请手动执行:" >&2
echo " $PY -m pip install -r requirements.txt" >&2
exit 1
}
fi
echo " 使用解释器:$PY"
# ---------- 2. 端口占用检查(已在运行就直接给出地址) ----------
if curl -s -o /dev/null --max-time 2 "http://127.0.0.1:${PORT}/health" 2>/dev/null; then
echo "==> 端口 ${PORT} 上已有服务在运行"
echo " 看板地址: http://127.0.0.1:${PORT}"
echo " (要重启请先在原窗口按 Ctrl+C,或执行:lsof -ti:${PORT} | xargs kill)"
exit 0
fi
# ---------- 3. 首次运行:建库 + 导入历史数据 ----------
if [ ! -f data/board.db ]; then
echo "==> 首次运行,初始化数据库"
"$PY" scripts/init_db.py
if [ -f "$XLSX" ]; then
echo "==> 导入历史数据:$XLSX"
"$PY" scripts/import_excel.py "$XLSX"
echo "==> 拆分定开项目"
"$PY" scripts/migrate_custom.py
else
echo " (未找到 Excel 源文件,跳过历史数据导入:$XLSX)"
fi
if [ -f "$STAFF_XLSX" ]; then
echo "==> 导入人员名单:$STAFF_XLSX"
"$PY" scripts/import_staff.py "$STAFF_XLSX"
else
echo " (未找到人员名单,跳过人力库导入:$STAFF_XLSX)"
fi
fi
# ---------- 4. 启动 ----------
# uvicorn 默认只监听 *.py,这里补上前端文件与 .env
RELOAD_ARGS=()
if [ "$DEV" = "1" ]; then
RELOAD_ARGS=(--reload
--reload-include "*.js" --reload-include "*.css" --reload-include "*.html"
--reload-include ".env")
fi
URL="http://127.0.0.1:${PORT}"
echo "==> 启动服务: $URL"
echo " API 文档: $URL/api/docs"
echo " 停止服务: Ctrl+C"
echo
if [ "${NO_OPEN:-0}" != "1" ]; then
( sleep 3; open "$URL" >/dev/null 2>&1 || true ) &
fi
exec "$PY" -m uvicorn app.main:app --host 127.0.0.1 --port "$PORT" ${RELOAD_ARGS[@]+"${RELOAD_ARGS[@]}"}