-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlink2pay
More file actions
111 lines (86 loc) · 2.43 KB
/
link2pay
File metadata and controls
111 lines (86 loc) · 2.43 KB
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
106
107
108
109
110
111
#!/usr/bin/env bash
set -euo pipefail
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
BACKEND_DIR="${ROOT_DIR}/backend"
FRONTEND_DIR="${ROOT_DIR}/frontend"
COMPOSE_FILE="${ROOT_DIR}/docker-compose.yml"
BACKEND_PID=""
FRONTEND_PID=""
log() {
printf '[link2pay] %s\n' "$1"
}
die() {
printf '[link2pay] ERROR: %s\n' "$1" >&2
exit 1
}
assert_docker_ready() {
if ! docker compose version >/dev/null 2>&1; then
die "Docker Compose plugin is required."
fi
if ! docker info >/dev/null 2>&1; then
die "Docker daemon is not running."
fi
}
cleanup() {
if [[ -n "${BACKEND_PID}" ]] && kill -0 "${BACKEND_PID}" >/dev/null 2>&1; then
kill "${BACKEND_PID}" >/dev/null 2>&1 || true
fi
if [[ -n "${FRONTEND_PID}" ]] && kill -0 "${FRONTEND_PID}" >/dev/null 2>&1; then
kill "${FRONTEND_PID}" >/dev/null 2>&1 || true
fi
}
require_artifact() {
local path="$1"
local help_message="$2"
if [[ ! -e "$path" ]]; then
die "${help_message}"
fi
}
start_backend() {
log "Starting backend API on http://localhost:3001"
(cd "$BACKEND_DIR" && npm start) &
BACKEND_PID=$!
}
start_frontend() {
log "Starting frontend preview on https://localhost:4173"
(cd "$FRONTEND_DIR" && npm run preview -- --host 0.0.0.0 --port 4173) &
FRONTEND_PID=$!
}
wait_for_postgres() {
local attempts=30
local i
log "Waiting for PostgreSQL container readiness"
for ((i = 1; i <= attempts; i++)); do
if docker compose -f "$COMPOSE_FILE" exec -T postgres pg_isready -U link2pay -d link2pay >/dev/null 2>&1; then
return 0
fi
sleep 1
done
die "PostgreSQL did not become ready in time."
}
if [[ "$(uname -s)" != "Linux" ]]; then
die "This launcher is intended for Linux. Use ./setup.sh first."
fi
if ! command -v npm >/dev/null 2>&1; then
die "npm is required."
fi
if ! command -v docker >/dev/null 2>&1; then
die "Docker is required."
fi
assert_docker_ready
if [[ -f "$COMPOSE_FILE" ]]; then
docker compose -f "$COMPOSE_FILE" up -d postgres >/dev/null
wait_for_postgres
fi
require_artifact "${BACKEND_DIR}/dist/index.js" "Missing backend build. Run ./setup.sh first."
require_artifact "${FRONTEND_DIR}/dist/index.html" "Missing frontend build. Run ./setup.sh first."
trap cleanup EXIT INT TERM
start_backend
sleep 1
start_frontend
log "Press Ctrl+C to stop both services."
wait -n "${BACKEND_PID}" "${FRONTEND_PID}"
exit_code=$?
if (( exit_code != 0 )); then
die "One of the processes exited unexpectedly."
fi