Files
WeReadBoard/start_server.sh
T
admin 7bc02075bd 刷新按钮支持数据同步:调用 /api/sync 拉取最新数据
- app.py 新增 /api/sync POST 接口:运行 fetch_data.py + migrate_to_mysql.py
- refreshShelf() 先调用 /api/sync 同步数据,再重新加载书架
- 按钮文案三阶段:同步中... → 加载中... → 刷新
- CSS 增加 min-width 防止按钮宽度抖动
- launchd plist 和 start_server.sh 均配置 WEREAD_API_KEY 环境变量
2026-07-17 13:22:19 +08:00

110 lines
2.8 KiB
Bash
Executable File

#!/bin/bash
# 微信读书数据看板 - 服务启动脚本
# 用法:
# ./start_server.sh 启动服务(前台运行)
# ./start_server.sh daemon 后台守护进程方式启动
# ./start_server.sh stop 停止服务
# ./start_server.sh restart 重启服务
# ./start_server.sh status 查看状态
PROJECT_DIR="/Users/hwj/WorkBuddy/2026-07-17-09-48-20"
PYTHON="/Users/hwj/.workbuddy/binaries/python/envs/default/bin/python"
PID_FILE="$PROJECT_DIR/.server.pid"
LOG_FILE="/tmp/weread_server.log"
PORT=8080
cd "$PROJECT_DIR" || exit 1
is_running() {
if [ -f "$PID_FILE" ]; then
local pid=$(cat "$PID_FILE")
if kill -0 "$pid" 2>/dev/null; then
return 0
fi
fi
# 也检查端口是否被占用
if lsof -i :$PORT -t >/dev/null 2>&1; then
return 0
fi
return 1
}
get_pid() {
if [ -f "$PID_FILE" ]; then
local pid=$(cat "$PID_FILE")
if kill -0 "$pid" 2>/dev/null; then
echo "$pid"
return
fi
fi
lsof -i :$PORT -t 2>/dev/null | head -1
}
stop_server() {
if is_running; then
local pid=$(get_pid)
echo "停止服务 (PID: $pid)..."
kill "$pid" 2>/dev/null
sleep 1
if kill -0 "$pid" 2>/dev/null; then
kill -9 "$pid" 2>/dev/null
fi
rm -f "$PID_FILE"
echo "已停止"
else
echo "服务未运行"
fi
}
start_foreground() {
if is_running; then
echo "服务已在运行 (PID: $(get_pid))"
exit 1
fi
echo "启动服务(前台模式)..."
exec "$PYTHON" app.py
}
start_daemon() {
if is_running; then
echo "服务已在运行 (PID: $(get_pid))"
exit 1
fi
echo "启动服务(后台守护模式)..."
WEREAD_API_KEY="${WEREAD_API_KEY:-wrk-ZL_oy0woT5yKFaO1jn_15AAA}" \
nohup "$PYTHON" app.py > "$LOG_FILE" 2>&1 &
local pid=$!
echo "$pid" > "$PID_FILE"
sleep 2
if kill -0 "$pid" 2>/dev/null; then
echo "服务已启动 (PID: $pid)"
echo "访问: http://127.0.0.1:$PORT"
echo "日志: $LOG_FILE"
else
echo "启动失败,请查看日志: $LOG_FILE"
cat "$LOG_FILE"
rm -f "$PID_FILE"
exit 1
fi
}
show_status() {
if is_running; then
local pid=$(get_pid)
echo "✅ 服务运行中 (PID: $pid)"
echo " 端口: $PORT"
echo " 日志: $LOG_FILE"
else
echo "❌ 服务未运行"
fi
}
case "${1:-start}" in
start) start_foreground ;;
daemon) start_daemon ;;
stop) stop_server ;;
restart) stop_server; sleep 1; start_daemon ;;
status) show_status ;;
*) echo "用法: $0 {start|daemon|stop|restart|status}"; exit 1 ;;
esac