-
Notifications
You must be signed in to change notification settings - Fork 0
/
startup.sh
63 lines (47 loc) · 1.41 KB
/
startup.sh
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
#!/bin/bash
set -euo pipefail
# Check for .env file
test -f .env || (echo "$(date +'%Y-%m-%d %H:%M:%S') ERROR: .env file not found" >&2; exit 1)
# Source .env file
if ! source .env; then
echo "$(date +'%Y-%m-%d %H:%M:%S') ERROR: Failed to source .env file" >&2
exit 1
fi
# Check required environment variables
DATABASE_URL="${DATABASE_URL:?DATABASE_URL not set}"
JWT_SECRET="${JWT_SECRET:?JWT_SECRET not set}"
PORT="${PORT:-3001}"
# Start backend server
echo "$(date +'%Y-%m-%d %H:%M:%S') Starting backend server..."
npm run start &
backend_pid=$!
# Wait for backend server to start and check health
wait_for_service http://localhost:${PORT}/api
# Check backend server status
if [[ $? -ne 0 ]]; then
echo "$(date +'%Y-%m-%d %H:%M:%S') ERROR: Backend server failed to start" >&2
exit 1
fi
echo "$(date +'%Y-%m-%d %H:%M:%S') Backend server started successfully at http://localhost:${PORT}/api"
# Trap signals for cleanup
trap cleanup EXIT ERR
#Function definitions
wait_for_service() {
local url="$1"
local timeout=30
local interval=1
until curl --fail --silent "$url"; do
sleep "$interval"
timeout=$((timeout - interval))
if [[ $timeout -le 0 ]]; then
echo "$(date +'%Y-%m-%d %H:%M:%S') ERROR: Timeout waiting for service: $url" >&2
return 1
fi
done
return 0
}
cleanup() {
echo "$(date +'%Y-%m-%d %H:%M:%S') Cleaning up..."
kill -TERM "$backend_pid" 2>/dev/null || true
}
```