clip / deploy /hf /start.sh
Husr's picture
first commit
d988ae4
#!/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[@]}"