Spaces:
Paused
Paused
Update start.sh
Browse files1234567894567891011121314151417151617181920212425262728293022233233242526272829303132333435363 unmodified lines404142434445464748495051525354434456575859606162636465666768697045464772737475767778798081828384858687888990919293944849505152535455565758969798991001011021031041051061071085960616263646566676869701107172112737411575#!/bin/bashset -e
# ======================= Supabase Config =======================# Strip any whitespace/newlines from environment variablesexport SUPABASE_URL="${SUPABASE_URL//[$'\t\r\n ']}"export SUPABASE_ANON_KEY="${SUPABASE_ANON_KEY//[$'\t\r\n ']}"export CONFIG_ID="mcp_settings"# ================================================================# Database configuration (using built-in DAO layer)export USE_DAO_LAYER=trueexport DB_URL="${DB_URL//[$'\t\r\n ']}"
# Migration flag - set to true to run migration, then set back to falseMIGRATE_DATA="${MIGRATE_DATA:-false}"
# --- Define paths ---APP_SRC_DIR="/app"RUN_DIR="/tmp/mcphub"CONFIG_FILE_NAME="mcp_settings.json"CONFIG_FILE_PATH="${RUN_DIR}/${CONFIG_FILE_NAME}"
echo "--- MCPHub with Supabase Storage ---"echo "--- MCPHub with Supabase Database ---"
# 1. Create runtime directory and copy filesecho "Setting up runtime environment..."mkdir -p "$RUN_DIR"cp -a "${APP_SRC_DIR}/." "$RUN_DIR/"
# 2. Download config from Supabaseecho "Loading configuration..."RESPONSE=$(curl --http1.1 -s -S -X GET \ "${SUPABASE_URL}/rest/v1/configs?id=eq.${CONFIG_ID}&select=content" \ -H "apikey: ${SUPABASE_ANON_KEY}" \ -H "Authorization: Bearer ${SUPABASE_ANON_KEY}" \ -H "Accept: application/json" 2>&1)cd "$RUN_DIR"
if [ -n "$RESPONSE" ] && [ "$RESPONSE" != "[]" ] && [[ ! "$RESPONSE" =~ "ERROR" ]]; then echo "$RESPONSE" | python3 -c "# 2. One-time migration from your current setupif [ "$MIGRATE_DATA" = "true" ]; then echo "🔄 Migrating data from configs table..." # Download current config from your configs table CONFIG_JSON=$(curl --http1.1 -s -X GET \ "${SUPABASE_URL}/rest/v1/configs?id=eq.mcp_settings&select=content" \ -H "apikey: ${SUPABASE_ANON_KEY}" \ -H "Authorization: Bearer ${SUPABASE_ANON_KEY}" \ -H "Accept: application/json" | python3 -c "import sys, jsontry: data = json.load(sys.stdin)3 unmodified lines print('{}')except: print('{}')" > "$CONFIG_FILE_PATH" echo "✅ Configuration loaded"else echo "{}" > "$CONFIG_FILE_PATH" echo "⚠️ Using default configuration"fi
# 3. Function to sync file to Supabasesync_to_supabase() { if [ ! -f "$CONFIG_FILE_PATH" ]; then return 1 fi") # Create JSON payload using Python (just for JSON formatting) PAYLOAD=$(python3 -c "import jsonfrom datetime import datetime
with open('$CONFIG_FILE_PATH', 'r') as f: config = json.load(f)
payload = { 'content': config, 'updated_at': datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S')}
print(json.dumps(payload))" 2>/dev/null) # Save the config temporarily echo "$CONFIG_JSON" > /tmp/current_config.json if [ -n "$PAYLOAD" ]; then # Upload using curl curl --http1.1 -s -X PATCH \ "${SUPABASE_URL}/rest/v1/configs?id=eq.${CONFIG_ID}" \ -H "apikey: ${SUPABASE_ANON_KEY}" \ -H "Authorization: Bearer ${SUPABASE_ANON_KEY}" \ -H "Content-Type: application/json" \ -H "Prefer: return=minimal" \ -d "$PAYLOAD" > /dev/null 2>&1 if [ $? -eq 0 ]; then echo "✅ Config synced to Supabase at $(date)" fi fi}
# 4. Start background sync processecho "Starting background sync..."( LAST_HASH="" if [ -f "$CONFIG_FILE_PATH" ]; then LAST_HASH=$(md5sum "$CONFIG_FILE_PATH" | awk '{ print $1 }') fi # Set environment to use legacy mode for loading export USE_DAO_LAYER=false # Load the config into the app and save via DAO layer node -e " const fs = require('fs'); const config = JSON.parse(fs.readFileSync('/tmp/current_config.json', 'utf8')); // Use the built-in migration function const { migrateToDao } = require('./dist/config/configManager.js'); while true; do sleep 30 if [ -f "$CONFIG_FILE_PATH" ]; then CURRENT_HASH=$(md5sum "$CONFIG_FILE_PATH" | awk '{ print $1 }') if [ "$CURRENT_HASH" != "$LAST_HASH" ]; then sync_to_supabase LAST_HASH=$CURRENT_HASH fi fi done) & migrateToDao().then(() => { console.log('✅ Migration completed successfully'); process.exit(0); }).catch((error) => { console.error('❌ Migration failed:', error); process.exit(1); }); " echo "✅ Migration completed!"fi
# 5. Start application# 3. Start applicationecho "🚀 Starting MCPHub server..."cd "$RUN_DIR"npm start
echo "MCPHub server stopped."echo "MCPHub server stopped."start.sh+44-84