|
| 1 | +#!/bin/sh |
| 2 | + |
| 3 | +# Let user skip all checks and initialization, if DL_SKIP_INIT env var is set |
| 4 | +if [ -n "$DL_SKIP_INIT" ]; then |
| 5 | + echo "Skipping initialization, and starting app directly" |
| 6 | + exec node ./dist/analog/server/index.mjs |
| 7 | +fi |
| 8 | + |
| 9 | +# Skip checks if SUPABASE_URL and SUPABASE_ANON_KEY are set, or if DL_ENV_TYPE is 'managed' |
| 10 | +if { [ -n "$SUPABASE_URL" ] && [ -n "$SUPABASE_ANON_KEY" ]; } || [ "$DL_ENV_TYPE" = "managed" ]; then |
| 11 | + echo "Skipping initialization, as not self-hosted instance, or using Postgres" |
| 12 | + exec node ./dist/analog/server/index.mjs |
| 13 | +fi |
| 14 | + |
| 15 | +# ANSI color codes |
| 16 | +ERR=$'\033[0;31m❌' |
| 17 | +WARN=$'\033[1;33m⚠️' |
| 18 | +SUCCESS=$'\033[0;32m✅' |
| 19 | +INFO=$'\nℹ️ \033[90m' |
| 20 | +RESET=$'\033[0m' |
| 21 | + |
| 22 | +MAX_WAIT=600 |
| 23 | +WARNINGS_FOUND=0 |
| 24 | + |
| 25 | +echo "${INFO} Checking environment...${RESET}" |
| 26 | + |
| 27 | +# Check psql and node are installed, and that the app is built |
| 28 | +[ -x "$(command -v psql)" ] || { |
| 29 | + echo "${WARN} psql (Postgres client) not found in PATH${RESET}"; |
| 30 | + WARNINGS_FOUND=1; |
| 31 | +} |
| 32 | +[ -x "$(command -v node)" ] || { |
| 33 | + echo "${WARN} node not found in PATH. Is it installed?${RESET}"; |
| 34 | + WARNINGS_FOUND=1; |
| 35 | +} |
| 36 | +[ -f "./dist/analog/server/index.mjs" ] || { |
| 37 | + echo "${WARN} App entrypoint not found. Did you run the build?${RESET}"; |
| 38 | + WARNINGS_FOUND=1; |
| 39 | +} |
| 40 | + |
| 41 | +# Check if in Docker |
| 42 | +if [ ! -f /.dockerenv ]; then |
| 43 | + echo "${WARN} This doesn't appear to be a Docker container${RESET}" |
| 44 | +fi |
| 45 | + |
| 46 | +# Check required environment variables |
| 47 | +REQUIRED_VARS="DL_PG_HOST DL_PG_PORT DL_PG_USER DL_PG_PASSWORD DL_PG_NAME" |
| 48 | +for VAR in $REQUIRED_VARS; do |
| 49 | + if [ -z "$(eval echo \$$VAR)" ]; then |
| 50 | + echo "${WARN} Environment variable $VAR is not set${RESET}" |
| 51 | + WARNINGS_FOUND=1 |
| 52 | + fi |
| 53 | +done |
| 54 | + |
| 55 | +# Success message if everything looks good |
| 56 | +if [ "$WARNINGS_FOUND" -eq 0 ]; then |
| 57 | + echo "${SUCCESS} All environment checks have passed${RESET}" |
| 58 | +else |
| 59 | + echo "${ERR} Unable to start app, as issues were found${RESET}" |
| 60 | + exit 1 |
| 61 | +fi |
| 62 | + |
| 63 | +# Wait for Postgres to be ready |
| 64 | +echo "${INFO} Waiting for Postgres at ${DL_PG_HOST}:${DL_PG_PORT}...${RESET}" |
| 65 | +elapsed=0 |
| 66 | +while ! pg_isready -h "$DL_PG_HOST" -p "$DL_PG_PORT" -U "$DL_PG_USER" > /dev/null 2>&1; do |
| 67 | + sleep 1 |
| 68 | + elapsed=$((elapsed + 1)) |
| 69 | + if [ $((elapsed % 30)) -eq 0 ]; then |
| 70 | + echo "${WARN} Postgres doesn't appear to be ready yet, after ${elapsed}s. Still waiting...${RESET}" |
| 71 | + fi |
| 72 | + if [ "$elapsed" -ge "$MAX_WAIT" ]; then |
| 73 | + echo "${ERR} Postgres not ready after ${MAX_WAIT}s. Exiting.${RESET}" |
| 74 | + exit 1 |
| 75 | + fi |
| 76 | +done |
| 77 | +echo "${SUCCESS} Postgres is ready (took ${elapsed}s)${RESET}" |
| 78 | + |
| 79 | +# Check if schema is applied / and apply it |
| 80 | +echo "${INFO} Applying schema from schema.sql...${RESET}" |
| 81 | +PGPASSWORD="$DL_PG_PASSWORD" psql -h "$DL_PG_HOST" -p "$DL_PG_PORT" -U "$DL_PG_USER" \ |
| 82 | + -d "$DL_PG_NAME" -f ./schema.sql > /dev/null 2>&1 \ |
| 83 | + && echo "${SUCCESS} Schema applied successfully${RESET}" \ |
| 84 | + || echo "${ERR} Failed to apply schema${RESET}" |
| 85 | + |
| 86 | +# Testing the database connection |
| 87 | +echo "${INFO} Testing database connection...${RESET}" |
| 88 | +PGPASSWORD="$DL_PG_PASSWORD" psql -h "$DL_PG_HOST" -p "$DL_PG_PORT" -U "$DL_PG_USER" -d "$DL_PG_NAME" \ |
| 89 | + -c "SELECT 1;" > /dev/null 2>&1 \ |
| 90 | + && echo "${SUCCESS} Database connection test succeeded${RESET}" \ |
| 91 | + || echo "${ERR} Database connection test failed${RESET}" |
| 92 | + |
| 93 | +# Start the app! |
| 94 | +echo "${INFO} Starting Domain Locker${RESET}" |
| 95 | +exec node ./dist/analog/server/index.mjs |
0 commit comments