mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-04-25 08:46:22 +02:00
- Modified Dockerfile to use placeholder values for frontend environment variables, allowing for runtime substitution. - Enhanced entrypoint script to apply runtime environment variable configuration, replacing placeholders in JavaScript files with actual values. - Updated documentation paths in MDX files for Google OAuth images and added detailed setup guides for Discord, Linear, Notion, and Slack OAuth integrations.
176 lines
6.7 KiB
Bash
176 lines
6.7 KiB
Bash
#!/bin/bash
|
|
set -e
|
|
|
|
echo "==========================================="
|
|
echo " 🏄 SurfSense All-in-One Container"
|
|
echo "==========================================="
|
|
|
|
# Create log directory
|
|
mkdir -p /var/log/supervisor
|
|
|
|
# ================================================
|
|
# Ensure data directory exists
|
|
# ================================================
|
|
mkdir -p /data
|
|
|
|
# ================================================
|
|
# Generate SECRET_KEY if not provided
|
|
# ================================================
|
|
if [ -z "$SECRET_KEY" ]; then
|
|
# Generate a random secret key and persist it
|
|
if [ -f /data/.secret_key ]; then
|
|
export SECRET_KEY=$(cat /data/.secret_key)
|
|
echo "✅ Using existing SECRET_KEY from persistent storage"
|
|
else
|
|
export SECRET_KEY=$(python3 -c "import secrets; print(secrets.token_urlsafe(32))")
|
|
echo "$SECRET_KEY" > /data/.secret_key
|
|
chmod 600 /data/.secret_key
|
|
echo "✅ Generated new SECRET_KEY (saved for persistence)"
|
|
fi
|
|
fi
|
|
|
|
# ================================================
|
|
# Set default TTS/STT services if not provided
|
|
# ================================================
|
|
if [ -z "$TTS_SERVICE" ]; then
|
|
export TTS_SERVICE="local/kokoro"
|
|
echo "✅ Using default TTS_SERVICE: local/kokoro"
|
|
fi
|
|
|
|
if [ -z "$STT_SERVICE" ]; then
|
|
export STT_SERVICE="local/base"
|
|
echo "✅ Using default STT_SERVICE: local/base"
|
|
fi
|
|
|
|
# ================================================
|
|
# Initialize PostgreSQL if needed
|
|
# ================================================
|
|
if [ ! -f /data/postgres/PG_VERSION ]; then
|
|
echo "📦 Initializing PostgreSQL database..."
|
|
|
|
# Initialize PostgreSQL data directory
|
|
chown -R postgres:postgres /data/postgres
|
|
chmod 700 /data/postgres
|
|
|
|
# Initialize with UTF8 encoding (required for proper text handling)
|
|
su - postgres -c "/usr/lib/postgresql/14/bin/initdb -D /data/postgres --encoding=UTF8 --locale=C.UTF-8"
|
|
|
|
# Configure PostgreSQL for connections
|
|
echo "host all all 0.0.0.0/0 md5" >> /data/postgres/pg_hba.conf
|
|
echo "local all all trust" >> /data/postgres/pg_hba.conf
|
|
echo "listen_addresses='*'" >> /data/postgres/postgresql.conf
|
|
|
|
# Start PostgreSQL temporarily to create database and user
|
|
su - postgres -c "/usr/lib/postgresql/14/bin/pg_ctl -D /data/postgres -l /tmp/postgres_init.log start"
|
|
|
|
# Wait for PostgreSQL to be ready
|
|
sleep 5
|
|
|
|
# Create user and database
|
|
su - postgres -c "psql -c \"CREATE USER ${POSTGRES_USER:-surfsense} WITH PASSWORD '${POSTGRES_PASSWORD:-surfsense}' SUPERUSER;\""
|
|
su - postgres -c "psql -c \"CREATE DATABASE ${POSTGRES_DB:-surfsense} OWNER ${POSTGRES_USER:-surfsense};\""
|
|
|
|
# Enable pgvector extension
|
|
su - postgres -c "psql -d ${POSTGRES_DB:-surfsense} -c 'CREATE EXTENSION IF NOT EXISTS vector;'"
|
|
|
|
# Stop temporary PostgreSQL
|
|
su - postgres -c "/usr/lib/postgresql/14/bin/pg_ctl -D /data/postgres stop"
|
|
|
|
echo "✅ PostgreSQL initialized successfully"
|
|
else
|
|
echo "✅ PostgreSQL data directory already exists"
|
|
fi
|
|
|
|
# ================================================
|
|
# Initialize Redis data directory
|
|
# ================================================
|
|
mkdir -p /data/redis
|
|
chmod 755 /data/redis
|
|
echo "✅ Redis data directory ready"
|
|
|
|
# ================================================
|
|
# Copy frontend build to runtime location
|
|
# ================================================
|
|
if [ -d /app/frontend/.next/standalone ]; then
|
|
cp -r /app/frontend/.next/standalone/* /app/frontend/ 2>/dev/null || true
|
|
cp -r /app/frontend/.next/static /app/frontend/.next/static 2>/dev/null || true
|
|
fi
|
|
|
|
# ================================================
|
|
# Runtime Environment Variable Replacement
|
|
# ================================================
|
|
# Next.js NEXT_PUBLIC_* vars are baked in at build time.
|
|
# This replaces placeholder values with actual runtime env vars.
|
|
echo "🔧 Applying runtime environment configuration..."
|
|
|
|
# Set defaults if not provided
|
|
NEXT_PUBLIC_FASTAPI_BACKEND_URL="${NEXT_PUBLIC_FASTAPI_BACKEND_URL:-http://localhost:8000}"
|
|
NEXT_PUBLIC_FASTAPI_BACKEND_AUTH_TYPE="${NEXT_PUBLIC_FASTAPI_BACKEND_AUTH_TYPE:-LOCAL}"
|
|
NEXT_PUBLIC_ETL_SERVICE="${NEXT_PUBLIC_ETL_SERVICE:-DOCLING}"
|
|
|
|
# Replace placeholders in all JS files
|
|
find /app/frontend -type f \( -name "*.js" -o -name "*.json" \) -exec sed -i \
|
|
-e "s|__NEXT_PUBLIC_FASTAPI_BACKEND_URL__|${NEXT_PUBLIC_FASTAPI_BACKEND_URL}|g" \
|
|
-e "s|__NEXT_PUBLIC_FASTAPI_BACKEND_AUTH_TYPE__|${NEXT_PUBLIC_FASTAPI_BACKEND_AUTH_TYPE}|g" \
|
|
-e "s|__NEXT_PUBLIC_ETL_SERVICE__|${NEXT_PUBLIC_ETL_SERVICE}|g" \
|
|
{} +
|
|
|
|
echo "✅ Environment configuration applied"
|
|
echo " Backend URL: ${NEXT_PUBLIC_FASTAPI_BACKEND_URL}"
|
|
echo " Auth Type: ${NEXT_PUBLIC_FASTAPI_BACKEND_AUTH_TYPE}"
|
|
echo " ETL Service: ${NEXT_PUBLIC_ETL_SERVICE}"
|
|
|
|
# ================================================
|
|
# Run database migrations
|
|
# ================================================
|
|
run_migrations() {
|
|
echo "🔄 Running database migrations..."
|
|
|
|
# Start PostgreSQL temporarily for migrations
|
|
su - postgres -c "/usr/lib/postgresql/14/bin/pg_ctl -D /data/postgres -l /tmp/postgres_migrate.log start"
|
|
sleep 5
|
|
|
|
# Start Redis temporarily for migrations (some might need it)
|
|
redis-server --dir /data/redis --daemonize yes
|
|
sleep 2
|
|
|
|
# Run alembic migrations
|
|
cd /app/backend
|
|
alembic upgrade head || echo "⚠️ Migrations may have already been applied"
|
|
|
|
# Stop temporary services
|
|
redis-cli shutdown || true
|
|
su - postgres -c "/usr/lib/postgresql/14/bin/pg_ctl -D /data/postgres stop"
|
|
|
|
echo "✅ Database migrations complete"
|
|
}
|
|
|
|
# Run migrations on first start or when explicitly requested
|
|
if [ ! -f /data/.migrations_run ] || [ "${FORCE_MIGRATIONS:-false}" = "true" ]; then
|
|
run_migrations
|
|
touch /data/.migrations_run
|
|
fi
|
|
|
|
# ================================================
|
|
# Environment Variables Info
|
|
# ================================================
|
|
echo ""
|
|
echo "==========================================="
|
|
echo " 📋 Configuration"
|
|
echo "==========================================="
|
|
echo " Frontend URL: http://localhost:3000"
|
|
echo " Backend API: ${NEXT_PUBLIC_FASTAPI_BACKEND_URL}"
|
|
echo " API Docs: ${NEXT_PUBLIC_FASTAPI_BACKEND_URL}/docs"
|
|
echo " Auth Type: ${NEXT_PUBLIC_FASTAPI_BACKEND_AUTH_TYPE}"
|
|
echo " ETL Service: ${NEXT_PUBLIC_ETL_SERVICE}"
|
|
echo " TTS Service: ${TTS_SERVICE}"
|
|
echo " STT Service: ${STT_SERVICE}"
|
|
echo "==========================================="
|
|
echo ""
|
|
|
|
# ================================================
|
|
# Start Supervisor (manages all services)
|
|
# ================================================
|
|
echo "🚀 Starting all services..."
|
|
exec /usr/local/bin/supervisord -c /etc/supervisor/conf.d/surfsense.conf
|
|
|