File size: 6,477 Bytes
5c0175e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/bin/sh
# ---------------------------------------------------------------------------
# VR Media Server 2.0 - container entrypoint (Hugging Face Docker Space)
#
#   1. Seed /data/vrms.db from the build-time schema if the volume is empty
#   2. Report which secrets are present (never their values)
#   3. Start the Next.js standalone server
#   4. Self-test /api/health
#   5. Hand keep-alive over to the in-app scheduler (src/lib/keepalive.ts)
#
# NOTE ON KEEP-ALIVE: the cron now runs INSIDE the Next.js server, is timezone
# aware, and is configurable from Settings -> "Keep server awake". The shell
# loop below is only a fallback for setups that disable the in-app scheduler,
# so it defaults to OFF to avoid double pinging.
# ---------------------------------------------------------------------------
set -e

DB_PATH="/data/vrms.db"
SEED_DB="/app/seed.db"
PORT="${PORT:-7860}"

echo "================================================="
echo " VR Media Server 2.0"
echo " Offcloud -> DEOVR -> Quest 3 passthrough"
echo "================================================="

# --- 1. Database bootstrap -------------------------------------------------
mkdir -p /data
if [ ! -f "$DB_PATH" ]; then
  if [ -f "$SEED_DB" ]; then
    echo "[init] seeding fresh database at $DB_PATH"
    cp "$SEED_DB" "$DB_PATH"
    chmod 644 "$DB_PATH" 2>/dev/null || true
  else
    echo "[init] WARNING: no seed database found at $SEED_DB"
  fi
else
  echo "[init] using existing database at $DB_PATH"
fi

mkdir -p "${THUMBNAIL_DIR:-/data/thumbnails}"

# --- 1b. Additive schema migration (multi-source) --------------------------
# Upgrades an existing database with the TeraBox/Blomp columns. Idempotent.
if [ -f /app/scripts/ensure-schema.mjs ]; then
  echo "[init] ensuring database schema (multi-source migration)"
  node /app/scripts/ensure-schema.mjs || echo "[init] WARNING: schema migration failed - continuing"
fi

# --- 2. Secret presence check (values are never printed) -------------------
if [ -n "$APP_API_KEY" ]; then
  echo "[init] APP_API_KEY: set (sign in with this key)"
elif [ -n "$APP_PASSWORD" ]; then
  echo "[init] APP_PASSWORD: set (legacy variable, used as the API key)"
elif [ -n "$OFFCLOUD_API_KEY" ] && [ "$ALLOW_OFFCLOUD_KEY_LOGIN" != "false" ]; then
  echo "[init] APP_API_KEY: not set - the Offcloud API key will unlock the UI"
else
  echo "[init] APP_API_KEY: NOT SET - falling back to the default key 'vr-media-server'"
  echo "[init]   Set APP_API_KEY as a Space secret to lock this server down."
fi

if [ -n "$OFFCLOUD_API_KEY" ]; then
  echo "[init] OFFCLOUD_API_KEY: set"
else
  echo "[init] OFFCLOUD_API_KEY: not set - Offcloud skipped"
fi

# --- 2a. TeraBox / Blomp source presence -----------------------------------
if [ -n "$TERABOX_COOKIE" ]; then
  echo "[init] TERABOX_COOKIE: set (cookie mode)"
elif [ -n "$TERABOX_ACCESS_TOKEN" ] || [ -n "$TERABOX_CLIENT_ID" ]; then
  echo "[init] TeraBox: OAuth credentials set"
else
  echo "[init] TeraBox: not configured via env - can be connected in Settings"
fi

if [ -n "$BLOMP_EMAIL" ] && [ -n "$BLOMP_PASSWORD" ]; then
  echo "[init] Blomp: account credentials set (container: ${BLOMP_CONTAINER:-auto})"
else
  echo "[init] Blomp: not configured via env - can be connected in Settings"
fi

# --- 2b. Session signing secret --------------------------------------------
# Sessions are HMAC-signed. When AUTH_SECRET is unset the key is derived from
# the API key, which breaks if any runtime cannot see the secret. Generate one
# and keep it on the persistent volume so sessions survive restarts.
if [ -z "$AUTH_SECRET" ]; then
  SECRET_FILE="/data/auth-secret"
  if [ -f "$SECRET_FILE" ]; then
    AUTH_SECRET="$(cat "$SECRET_FILE")"
    echo "[init] AUTH_SECRET: loaded from $SECRET_FILE"
  else
    AUTH_SECRET="$(openssl rand -hex 32 2>/dev/null || head -c 32 /dev/urandom | od -An -tx1 | tr -d ' \n')"
    if printf '%s' "$AUTH_SECRET" > "$SECRET_FILE" 2>/dev/null; then
      chmod 600 "$SECRET_FILE" 2>/dev/null || true
      echo "[init] AUTH_SECRET: generated and saved to $SECRET_FILE"
    else
      echo "[init] AUTH_SECRET: generated (could not write $SECRET_FILE - sessions reset on restart)"
    fi
  fi
  export AUTH_SECRET
else
  echo "[init] AUTH_SECRET: provided by the environment"
fi

# --- Web UI password (layer 2) ---------------------------------------------
# Guards the browser interface only; headset feeds keep using the API key.
if [ -n "$APP_LOGIN_PASSWORD" ]; then
  echo "[init] Web UI sign-in: username + password (API key detected from the secret)"
  echo "[init] APP_USERNAME: ${APP_USERNAME:-admin}"
else
  echo "[init] Web UI sign-in: API key (set APP_LOGIN_PASSWORD for username + password)"
fi
if [ "$RESET_LOGIN_PASSWORD" = "true" ]; then
  echo "[init] RESET_LOGIN_PASSWORD=true - a password stored in the app will be cleared"
fi

# Resolve the public URL used by the in-app keep-alive scheduler.
if [ -n "$PUBLIC_URL" ]; then
  PING_BASE="$PUBLIC_URL"
elif [ -n "$SPACE_HOST" ]; then
  PING_BASE="https://${SPACE_HOST}"
else
  PING_BASE="http://127.0.0.1:${PORT}"
fi
echo "[init] keep-alive target: ${PING_BASE}/api/ping"
echo "[init] keep-alive timezone default: ${KEEP_ALIVE_TIMEZONE:-${TZ:-UTC}}"

# --- 3. Start the server ---------------------------------------------------
cd /app
echo "[boot] starting Next.js on 0.0.0.0:${PORT}"
node server.js &
SERVER_PID=$!

# --- 4. Self-test ----------------------------------------------------------
sleep 4
if curl -fsS "http://127.0.0.1:${PORT}/api/health" > /dev/null 2>&1; then
  echo "[boot] health check OK"
else
  echo "[boot] health check did not answer yet (the app may still be warming up)"
fi

# --- 5. Optional shell keep-alive fallback (OFF by default) ----------------
# The in-app scheduler handles this. Only enable when you have turned the
# in-app scheduler off (KEEP_ALIVE_ENABLED=false inside the app settings) and
# still want a dumb interval pinger.
if [ "$SHELL_KEEP_ALIVE" = "true" ]; then
  INTERVAL_MIN="${KEEP_ALIVE_INTERVAL_MINUTES:-10}"
  echo "[keep-alive] shell fallback enabled - pinging every ${INTERVAL_MIN} min"
  (
    while true; do
      sleep $((INTERVAL_MIN * 60))
      curl -fsS -o /dev/null "${PING_BASE}/api/ping" \
        && echo "[keep-alive] shell ping ok $(date -u +%FT%TZ)" \
        || echo "[keep-alive] shell ping FAILED $(date -u +%FT%TZ)"
    done
  ) &
else
  echo "[keep-alive] shell fallback disabled - in-app scheduler is in charge"
fi

wait $SERVER_PID