46 lines
1.2 KiB
Bash
Executable File
46 lines
1.2 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
|
|
port="${PORT:-43210}"
|
|
data_dir="${BOSS_DATA_DIR:-.boss-data}"
|
|
data_file="${BOSS_DATA_FILE:-$data_dir/server-store.json}"
|
|
pid_file="${BOSS_PID_FILE:-$data_dir/server.pid}"
|
|
log_file="${BOSS_LOG_FILE:-$data_dir/server.log}"
|
|
base_path="${BOSS_BASE_PATH:-}"
|
|
if [[ -n "$base_path" && "$base_path" != /* ]]; then
|
|
base_path="/$base_path"
|
|
fi
|
|
base_path="${base_path%/}"
|
|
health_url="http://127.0.0.1:${port}${base_path}/api/health"
|
|
|
|
mkdir -p "$data_dir"
|
|
|
|
if [[ -f "$pid_file" ]]; then
|
|
existing_pid="$(cat "$pid_file")"
|
|
if [[ -n "$existing_pid" ]] && kill -0 "$existing_pid" 2>/dev/null; then
|
|
kill "$existing_pid"
|
|
for _ in {1..20}; do
|
|
if ! kill -0 "$existing_pid" 2>/dev/null; then
|
|
break
|
|
fi
|
|
sleep 1
|
|
done
|
|
fi
|
|
rm -f "$pid_file"
|
|
fi
|
|
|
|
nohup env PORT="$port" BOSS_DATA_FILE="$data_file" node dist/server.js >>"$log_file" 2>&1 < /dev/null &
|
|
echo $! > "$pid_file"
|
|
|
|
for _ in {1..30}; do
|
|
if curl -fsS "$health_url" >/dev/null 2>&1; then
|
|
echo "Boss server started on :${port}"
|
|
exit 0
|
|
fi
|
|
sleep 1
|
|
done
|
|
|
|
echo "Boss server failed to become healthy. Recent logs:" >&2
|
|
tail -n 80 "$log_file" >&2 || true
|
|
exit 1
|