| #!/bin/sh |
|
|
| |
| echo "=== LANGFUSE V2 - CLICKHOUSE NOT REQUIRED ===" |
| unset CLICKHOUSE_URL |
| unset CLICKHOUSE_MIGRATION_URL |
| unset CLICKHOUSE_USER |
| unset CLICKHOUSE_PASSWORD |
|
|
| |
| if [ -n "$EXTERNAL_DATABASE_URL" ]; then |
| echo "=== USING EXTERNAL DATABASE (PERSISTENT) ===" |
| echo "Database host: ${EXTERNAL_DATABASE_URL%%/*}" |
| export DATABASE_URL="$EXTERNAL_DATABASE_URL" |
| echo "Data will persist across restarts!" |
| else |
| echo "=== USING LOCAL DATABASE (EPHEMERAL) ===" |
| echo "WARNING: HF Spaces has NO persistent storage!" |
| echo "All data will be LOST when the Space restarts." |
| echo "To fix: Use an external database like Neon.tech" |
| echo "Set EXTERNAL_DATABASE_URL secret in HF Space settings" |
| |
| |
| echo "Setting up postgres user..." |
| if ! id postgres >/dev/null 2>&1; then |
| addgroup -g 70 -S postgres 2>/dev/null || true |
| adduser -u 70 -S -D -G postgres -H -h /data/postgresql -s /bin/sh postgres 2>/dev/null || true |
| fi |
|
|
| |
| echo "Creating necessary directories in the persistent /data volume..." |
| mkdir -p /data/postgresql/data /data/postgresql/run |
| chown -R postgres:postgres /data/postgresql |
| chmod 0700 /data/postgresql/data |
| chmod 0755 /data/postgresql/run |
|
|
| |
| echo "Initializing PostgreSQL if not already initialized..." |
| if [ ! -f "/data/postgresql/data/PG_VERSION" ]; then |
| |
| echo "Initializing database..." |
| su postgres -c "initdb -D /data/postgresql/data" |
| |
| |
| echo "local all all trust" > /data/postgresql/data/pg_hba.conf |
| echo "host all all 127.0.0.1/32 trust" >> /data/postgresql/data/pg_hba.conf |
| echo "host all all ::1/128 trust" >> /data/postgresql/data/pg_hba.conf |
| echo "host all all 0.0.0.0/0 trust" >> /data/postgresql/data/pg_hba.conf |
| echo "host all all ::/0 trust" >> /data/postgresql/data/pg_hba.conf |
| chown postgres:postgres /data/postgresql/data/pg_hba.conf |
| fi |
|
|
| |
| echo "Starting PostgreSQL..." |
| su postgres -c "pg_ctl -D /data/postgresql/data -o '-c listen_addresses=\"*\" -c unix_socket_directories=\"/data/postgresql/run\"' start" |
|
|
| |
| echo "Waiting for PostgreSQL to be ready..." |
| max_attempts=30 |
| attempt=0 |
| until pg_isready -h localhost -p 5432 2>/dev/null || [ $attempt -ge $max_attempts ]; do |
| echo "Waiting for PostgreSQL to be ready... ($attempt/$max_attempts)" |
| sleep 2 |
| attempt=$((attempt + 1)) |
| done |
|
|
| if [ $attempt -ge $max_attempts ]; then |
| echo "ERROR: PostgreSQL failed to start after $max_attempts attempts" |
| exit 1 |
| fi |
| echo "PostgreSQL is ready!" |
|
|
| |
| echo "Creating database and roles..." |
| su postgres -c "createuser -h /data/postgresql/run -s postgres 2>/dev/null || true" |
| su postgres -c "createdb -h /data/postgresql/run node 2>/dev/null || true" |
| |
| |
| export DATABASE_URL="postgresql://postgres:postgres@localhost:5432/node" |
| fi |
|
|
| |
| if [ -n "$SPACE_ID" ]; then |
| echo "Setting NEXTAUTH_URL to https://huggingface.co/spaces/${SPACE_ID}" |
| export NEXTAUTH_URL="https://${SPACE_HOST}" |
| else |
| echo "WARNING: SPACE_ID not found" |
| fi |
|
|
| |
| if [ -z "$NEXTAUTH_SECRET" ]; then |
| echo "WARNING: NEXTAUTH_SECRET not set, generating random secret..." |
| echo "For production, please set this as a persistent secret in HF Space settings!" |
| export NEXTAUTH_SECRET=$(openssl rand -hex 32) |
| fi |
|
|
| |
| if [ -z "$SALT" ]; then |
| echo "WARNING: SALT not set, generating random salt..." |
| echo "For production, please set this as a persistent secret in HF Space settings!" |
| export SALT=$(openssl rand -hex 32) |
| fi |
|
|
| |
| export HOSTNAME="0.0.0.0" |
| export HOST="0.0.0.0" |
| export PORT=3000 |
|
|
| |
| export LANGFUSE_CSP_DISABLE="true" |
|
|
| |
| if [ -n "$OAUTH_CLIENT_ID" ] && [ -n "$OAUTH_CLIENT_SECRET" ]; then |
| echo "HF OAuth variables detected, enabling HF authentication..." |
| export AUTH_CUSTOM_CLIENT_ID=$OAUTH_CLIENT_ID |
| export AUTH_CUSTOM_CLIENT_SECRET=$OAUTH_CLIENT_SECRET |
| export AUTH_CUSTOM_ISSUER=$OPENID_PROVIDER_URL |
| export AUTH_CUSTOM_SCOPE=$OAUTH_SCOPES |
| export AUTH_CUSTOM_NAME="Hugging Face" |
| export AUTH_DISABLE_USERNAME_PASSWORD="true" |
| else |
| echo "No HF OAuth configured, using standard email/password authentication..." |
| export AUTH_DISABLE_USERNAME_PASSWORD="false" |
| fi |
|
|
| |
| if [ -n "$AUTH_DISABLE_SIGNUP" ]; then |
| export AUTH_DISABLE_SIGNUP="$AUTH_DISABLE_SIGNUP" |
| else |
| export AUTH_DISABLE_SIGNUP="false" |
| fi |
|
|
| |
| if [ -n "$LANGFUSE_INIT_USER_EMAIL" ] && [ -n "$LANGFUSE_INIT_USER_PASSWORD" ]; then |
| echo "Headless initialization enabled - admin account will be auto-created" |
| echo "Email: $LANGFUSE_INIT_USER_EMAIL" |
| export LANGFUSE_INIT_ORG_ID="default-org" |
| export LANGFUSE_INIT_ORG_NAME="My Organization" |
| export LANGFUSE_INIT_PROJECT_ID="default-project" |
| export LANGFUSE_INIT_PROJECT_NAME="My Project" |
| |
| export AUTH_DISABLE_SIGNUP="true" |
| echo "Signup disabled - only the pre-created admin account can log in" |
| fi |
|
|
| |
| echo "Starting Next.js..." |
| ./web/entrypoint.sh node ./web/server.js \ |
| --keepAliveTimeout 110000 |