Files
WeReadBoard/weread
T
admin 5608562f02 feat: 添加 weread 启停命令 & 修复 macOS SSL 证书问题
- 新增 weread CLI 脚本,支持 weread start/stop [port] 一键管理服务
- app.py 端口改为从环境变量 WEREAD_PORT 读取,默认 8080
- 修复 macOS Python urllib 调用微信读书 API 时 SSL 证书验证失败问题(引入 certifi)
2026-07-17 14:34:35 +08:00

103 lines
2.3 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.
#!/bin/bash
# weread - 微信读书数据看板 服务管理命令
# 用法:
# weread start [port] 启动服务(默认端口 8080)
# weread stop [port] 停止服务(默认端口 8080)
PROJECT_DIR="/Users/hwj/Codes/MyProjects/WeReadBoard"
PID_FILE="$PROJECT_DIR/.server.pid"
LOG_FILE="/tmp/weread_server.log"
DEFAULT_PORT=8080
get_port() {
echo "${1:-$DEFAULT_PORT}"
}
get_pid() {
local port="$1"
if [ -f "$PID_FILE" ]; then
local saved_port
saved_port=$(sed -n '2p' "$PID_FILE" 2>/dev/null)
local pid
pid=$(head -1 "$PID_FILE" 2>/dev/null)
if [ -n "$pid" ] && kill -0 "$pid" 2>/dev/null; then
echo "$pid"
return
fi
fi
lsof -i :"$port" -t 2>/dev/null | head -1
}
is_running() {
local port="$1"
if [ -n "$(get_pid "$port")" ]; then
return 0
fi
return 1
}
cmd_start() {
local port
port=$(get_port "$1")
if is_running "$port"; then
echo "❌ 服务已在运行 (PID: $(get_pid "$port"), 端口: $port)"
exit 1
fi
echo "启动服务(端口: $port)..."
WEREAD_PORT="$port" nohup python3 "$PROJECT_DIR/app.py" > "$LOG_FILE" 2>&1 &
local pid=$!
printf '%s\n%s\n' "$pid" "$port" > "$PID_FILE"
sleep 2
if kill -0 "$pid" 2>/dev/null; then
echo "✅ 服务已启动"
echo " PID: $pid"
echo " 端口: $port"
echo " 访问: http://127.0.0.1:$port"
echo " 日志: $LOG_FILE"
else
echo "❌ 启动失败,请查看日志: $LOG_FILE"
cat "$LOG_FILE" 2>/dev/null
rm -f "$PID_FILE"
exit 1
fi
}
cmd_stop() {
local port
port=$(get_port "$1")
if ! is_running "$port"; then
echo "服务未运行(端口: $port)"
exit 0
fi
local pid
pid=$(get_pid "$port")
echo "停止服务 (PID: $pid, 端口: $port)..."
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 "✅ 已停止"
}
usage() {
echo "用法:"
echo " weread start [port] 启动服务(默认端口 8080)"
echo " weread stop [port] 停止服务(默认端口 8080)"
exit 1
}
case "${1}" in
start) cmd_start "$2" ;;
stop) cmd_stop "$2" ;;
*) usage ;;
esac