Spaces:
Paused
Paused
File size: 2,555 Bytes
dbe3386 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | #!/usr/bin/env bash
set -euo pipefail
SERVICE_NAME="com.chatgpt-register.web"
PROJECT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
LOG_DIR="${PROJECT_DIR}/logs"
LAUNCH_AGENTS_DIR="${HOME}/Library/LaunchAgents"
PLIST_PATH="${LAUNCH_AGENTS_DIR}/${SERVICE_NAME}.plist"
PYTHON_BIN="${PROJECT_DIR}/.venv/bin/python"
HOST="${HOST:-127.0.0.1}"
PORT="${PORT:-8080}"
while [[ $# -gt 0 ]]; do
case "$1" in
--host)
HOST="${2:-}"
shift 2
;;
--port)
PORT="${2:-}"
shift 2
;;
*)
echo "未知参数: $1"
echo "用法: $0 [--host 127.0.0.1] [--port 8080]"
exit 1
;;
esac
done
if [[ -z "${HOST}" || -z "${PORT}" ]]; then
echo "错误: host/port 不能为空"
exit 1
fi
if ! [[ "${PORT}" =~ ^[0-9]+$ ]] || (( PORT < 1 || PORT > 65535 )); then
echo "错误: 端口必须是 1-65535 的整数"
exit 1
fi
if [[ ! -x "${PYTHON_BIN}" ]]; then
echo "错误: 未找到可执行 Python: ${PYTHON_BIN}"
echo "请先创建虚拟环境并安装依赖。"
exit 1
fi
mkdir -p "${LOG_DIR}" "${LAUNCH_AGENTS_DIR}"
cat > "${PLIST_PATH}" <<EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>${SERVICE_NAME}</string>
<key>ProgramArguments</key>
<array>
<string>${PYTHON_BIN}</string>
<string>-m</string>
<string>uvicorn</string>
<string>web_app:app</string>
<string>--host</string>
<string>${HOST}</string>
<string>--port</string>
<string>${PORT}</string>
</array>
<key>WorkingDirectory</key>
<string>${PROJECT_DIR}</string>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<true/>
<key>ThrottleInterval</key>
<integer>10</integer>
<key>StandardOutPath</key>
<string>${LOG_DIR}/web.stdout.log</string>
<key>StandardErrorPath</key>
<string>${LOG_DIR}/web.stderr.log</string>
<key>EnvironmentVariables</key>
<dict>
<key>PYTHONUNBUFFERED</key>
<string>1</string>
</dict>
</dict>
</plist>
EOF
if launchctl print "gui/$(id -u)/${SERVICE_NAME}" >/dev/null 2>&1; then
launchctl bootout "gui/$(id -u)" "${PLIST_PATH}" >/dev/null 2>&1 || true
fi
launchctl bootstrap "gui/$(id -u)" "${PLIST_PATH}"
launchctl enable "gui/$(id -u)/${SERVICE_NAME}"
launchctl kickstart -k "gui/$(id -u)/${SERVICE_NAME}"
echo "已安装并启动: ${SERVICE_NAME}"
echo "配置文件: ${PLIST_PATH}"
echo "日志目录: ${LOG_DIR}"
echo "访问地址: http://${HOST}:${PORT}"
|