File size: 2,475 Bytes
e933369
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/bin/sh
#
# Docker entrypoint for JSON API Server
#
# Runs database initialisation (migrate + seed) before handing off
# to the main application process.  All configuration is sourced
# from environment variables β€” no .env files are loaded in Docker
# because NODE_ENV=production skips dotenv entirely.
#
# Behaviour can be controlled via:
#   SKIP_DB_SETUP=true   β€” skip migrate / seed on container start
#   DEBUG_ENTRYPOINT     β€” print diagnostic info for troubleshooting
#

set -eu

# ── Logging helpers ──────────────────────────────────────────────
log()  { printf '[Entrypoint] %s\n' "$*"; }
warn() { printf '[Entrypoint] [WARN] %s\n' "$*" >&2; }
die()  { printf '[Entrypoint] [FATAL] %s\n' "$*" >&2; exit 1; }

# ── Diagnostics ──────────────────────────────────────────────────
log "Node.js $(node --version)"

if [ "${DEBUG_ENTRYPOINT:-}" = "true" ]; then
  log "NODE_ENV  = ${NODE_ENV:-<unset>}"
  log "DB_PATH   = ${DB_PATH:-<unset>}"
  log "ADMIN_KEY = $( [ -n "${ADMIN_KEY:-}" ] && echo 'set' || echo 'not set' )"
  log "REDIS_URL = $( [ -n "${REDIS_URL:-}" ] && echo 'set' || echo 'not set' )"
fi

# ── Environment validation ───────────────────────────────────────
if [ -z "${DB_PATH:-}" ]; then
  die "DB_PATH is required but was not set. Define it in the Dockerfile or pass it at runtime."
fi

if [ -z "${ADMIN_KEY:-}" ]; then
  warn "ADMIN_KEY is not set β€” admin routes will be disabled at runtime."
fi

# ── Database initialisation ──────────────────────────────────────
if [ "${SKIP_DB_SETUP:-}" = "true" ]; then
  log "SKIP_DB_SETUP is set β€” skipping database setup."
else
  log "Running database setup (migrate + seed)..."

  if ! npm run db:setup; then
    die "Database setup failed. Check the logs above for details."
  fi

  log "Database setup completed."
fi

# ── Runtime β€” hand off to CMD ────────────────────────────────────
# exec replaces the shell so that signals (SIGTERM from docker stop)
# go directly to the Node process, enabling graceful shutdown.
log "Starting server..."
exec "$@"