File size: 2,101 Bytes
d988ae4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env bash
set -euo pipefail

export PORT=${PORT:-7860}
export FRONTEND_PORT=${FRONTEND_PORT:-3000}
export BACKEND_PORT=${BACKEND_PORT:-3001}
export REDIS_PORT=${REDIS_PORT:-6379}
export REDIS_HOST=${REDIS_HOST:-127.0.0.1}
export REDIS_PASSWORD=${REDIS_PASSWORD:-}
export ADMIN_TOKEN=${ADMIN_TOKEN:-admin-token}

# Ensure uploads directory exists
mkdir -p /app/uploads

# Start Redis locally only when no external host/URL is provided
redis_pid=""
if [[ -z "${REDIS_URL:-}" && ("$REDIS_HOST" == "127.0.0.1" || "$REDIS_HOST" == "localhost") ]]; then
  redis-server --port "$REDIS_PORT" --protected-mode no --save "" --appendonly no &
  redis_pid=$!
fi

echo "Waiting for Redis to be ready on $REDIS_HOST:$REDIS_PORT..."
if [[ -n "${REDIS_URL:-}" ]]; then
  redis_cli_args=("-u" "$REDIS_URL")
else
  redis_cli_args=("-h" "$REDIS_HOST" "-p" "$REDIS_PORT")
fi
if [[ -n "$REDIS_PASSWORD" ]]; then
  redis_cli_args+=("--pass" "$REDIS_PASSWORD")
fi
for i in {1..20}; do
  if redis-cli "${redis_cli_args[@]}" ping >/dev/null 2>&1; then
    echo "Redis is ready."
    break
  fi
  sleep 0.5
  if [[ $i -eq 20 ]]; then
    echo "Redis did not start in time" >&2
    exit 1
  fi
done

# Start backend
(
  cd /app/apps/backend
  PORT="$BACKEND_PORT" REDIS_HOST="$REDIS_HOST" REDIS_PORT="$REDIS_PORT" REDIS_PASSWORD="$REDIS_PASSWORD" REDIS_URL="$REDIS_URL" ADMIN_TOKEN="$ADMIN_TOKEN" pnpm start:prod
) &
backend_pid=$!

# Start frontend
(
  cd /app/apps/frontend
  PORT="$FRONTEND_PORT" pnpm start --hostname 0.0.0.0 --port "$FRONTEND_PORT"
) &
frontend_pid=$!

# Render nginx config and start proxy
envsubst '${PORT} ${FRONTEND_PORT} ${BACKEND_PORT}' < /app/deploy/hf/nginx.conf.template > /etc/nginx/nginx.conf
nginx -g 'daemon off;' &
nginx_pid=$!

pids=()
if [[ -n "$redis_pid" ]]; then
  pids+=($redis_pid)
fi
pids+=($backend_pid $frontend_pid $nginx_pid)

cleanup() {
  echo "Shutting down services..."
  for pid in "${pids[@]}"; do
    if kill -0 "$pid" 2>/dev/null; then
      kill "$pid" 2>/dev/null || true
    fi
  done
}

trap cleanup EXIT

# Wait for any service to exit
wait -n "${pids[@]}"