Files
Trackeep/docker-entrypoint.sh
T
Tomas DvorakandDevin <158243242+devin-ai-integration[bot]@users.noreply.github.com> 4dfdd500b4
CI/CD Pipeline / Test (push) Successful in 20m59s
CI/CD Pipeline / Security Scan (push) Successful in 10m38s
CI/CD Pipeline / Build and Push Images (push) Failing after 13s
fix(frontend): resolve production API URL fallback to localhost
Problem:
The unified Docker image builds the frontend at build time without
VITE_API_URL. Vite inlined import.meta.env.VITE_API_URL as undefined,
so every API call fell back to the hardcoded 'http://localhost:8080'.
This broke Casa deployments where the frontend loaded from the public
 domain but tried to reach the backend at localhost.

Solution:
1. Centralize API URL resolution in lib/api-url.ts via getApiOrigin().
   It checks runtime window.ENV first (injected by docker-entrypoint.sh
   at container startup), then build-time import.meta.env, then dev
   fallback. In production unified deployments it returns '' so API
   calls use same-origin relative URLs (/api/v1/...) that nginx proxies
   to the backend.
2. Replace all 50+ inline import.meta.env.VITE_API_URL || 'localhost'
   usages across 14 source files with getApiOrigin() / getApiV1BaseUrl().
3. Add build args and runtime sed substitution to Dockerfile and
   docker-entrypoint.sh so the same image works for any deployment.
4. Pass VITE_API_URL through docker-compose.yml and CI/CD build-args.

Verified:
- Production bundle contains 0 occurrences of localhost:8080.
- Container health check and /api/v1/auth/check-users both return 200.
- Runtime injection correctly sets VITE_API_URL='' for same-origin
  and VITE_API_URL='https://domain' for external backend.

Generated with [Devin](https://cli.devin.ai/docs)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-05-22 12:34:39 +02:00

115 lines
3.8 KiB
Bash

#!/bin/sh
# All-in-one entrypoint for Trackeep
# Initializes and starts PostgreSQL, then backend + nginx
set -e
PGDATA=${PGDATA:-/var/lib/postgresql/data}
# Auto-generate DB_PASSWORD if not provided
if [ -z "$DB_PASSWORD" ]; then
DB_PASSWORD=$(tr -dc 'a-zA-Z0-9' < /dev/urandom | head -c 32)
echo "========================================"
echo "WARNING: DB_PASSWORD was not set."
echo "Auto-generated password: $DB_PASSWORD"
echo "Set DB_PASSWORD explicitly to keep it stable across restarts."
echo "========================================"
fi
DB_USER=${DB_USER:-trackeep}
DB_NAME=${DB_NAME:-trackeep}
# Ensure PostgreSQL directories are owned by postgres (fixes volume permission issues)
mkdir -p "$PGDATA" /run/postgresql /var/log/postgresql
chown -R postgres:postgres "$PGDATA" /run/postgresql /var/log/postgresql
# Initialize PostgreSQL if data directory is empty
if [ ! -f "$PGDATA/PG_VERSION" ]; then
echo "Initializing PostgreSQL database cluster..."
su -s /bin/sh postgres -c "initdb -D $PGDATA --auth-local=trust --auth-host=md5"
# Allow local TCP connections
echo "host all all 127.0.0.1/32 md5" >> "$PGDATA/pg_hba.conf"
echo "host all all ::1/128 md5" >> "$PGDATA/pg_hba.conf"
# Start postgres temporarily to create user and database
su -s /bin/sh postgres -c "pg_ctl -D $PGDATA -l /var/log/postgresql/server.log start"
# Wait until postgres accepts connections
echo "Waiting for PostgreSQL to accept connections..."
for i in $(seq 1 30); do
if su -s /bin/sh postgres -c "pg_isready -q"; then
break
fi
sleep 1
done
# Create role and database
su -s /bin/sh postgres -c "psql -c \"CREATE USER \\\"$DB_USER\\\" WITH PASSWORD '$DB_PASSWORD';\""
su -s /bin/sh postgres -c "psql -c \"CREATE DATABASE \\\"$DB_NAME\\\" OWNER \\\"$DB_USER\\\";\""
su -s /bin/sh postgres -c "pg_ctl -D $PGDATA stop"
echo "PostgreSQL initialized."
fi
# Start PostgreSQL
echo "Starting PostgreSQL..."
su -s /bin/sh postgres -c "pg_ctl -D $PGDATA -l /var/log/postgresql/server.log start"
# Wait for PostgreSQL to be ready
echo "Waiting for PostgreSQL to be ready..."
for i in $(seq 1 30); do
if su -s /bin/sh postgres -c "pg_isready -q"; then
echo "PostgreSQL is ready."
break
fi
echo "Waiting for PostgreSQL... ($i/30)"
sleep 1
done
# Backend connects to the bundled local PostgreSQL
export BACKEND_PORT=8081
export DB_HOST=localhost
export DB_PORT=5432
export DB_NAME="$DB_NAME"
export DB_USER="$DB_USER"
export DB_PASSWORD="$DB_PASSWORD"
export DB_SSL_MODE=disable
export JWT_SECRET=${JWT_SECRET:-}
export GIN_MODE=${GIN_MODE:-release}
export CORS_ALLOWED_ORIGINS=${CORS_ALLOWED_ORIGINS:-*}
# Start backend in background
cd /app
echo "Starting Trackeep backend on port ${BACKEND_PORT}..."
./main &
BACKEND_PID=$!
# Wait for backend to be ready
echo "Waiting for backend to be ready..."
for i in $(seq 1 30); do
if wget --no-verbose --tries=1 --spider http://localhost:8081/health 2>/dev/null; then
echo "Backend is ready!"
break
fi
echo "Waiting... ($i/30)"
sleep 2
done
# Runtime environment variable injection for frontend.
# The frontend is built with placeholders; at container startup we replace
# them so the same image works for any deployment target (Casa, local, etc.).
HTML_FILE="/usr/share/nginx/html/index.html"
if [ -f "$HTML_FILE" ]; then
VITE_API_URL=${VITE_API_URL:-}
VITE_DEMO_MODE=${VITE_DEMO_MODE:-false}
sed -i "s|VITE_API_URL_PLACEHOLDER|$VITE_API_URL|g" "$HTML_FILE"
sed -i "s|VITE_DEMO_MODE_PLACEHOLDER|$VITE_DEMO_MODE|g" "$HTML_FILE"
echo "Frontend env injected: VITE_API_URL='$VITE_API_URL', VITE_DEMO_MODE='$VITE_DEMO_MODE'"
fi
# Start nginx in foreground (keeps container alive)
echo "Starting nginx on port 8080..."
nginx -g "daemon off;"