#!/bin/bash
echo "Forbinder til database..."
for i in $(seq 1 30); do
    python -c "
import pymysql, os, re
url = os.environ.get('DATABASE_URL', '')
m = re.match(r'mysql\+pymysql://([^:]+):([^@]+)@([^:/]+):?(\d+)?/(\w+)', url)
if not m:
    exit(1)
user, password, host, port, db = m.groups()
port = int(port or 3306)
try:
    conn = pymysql.connect(host=host, port=port, user=user, password=password, database=db)
    conn.close()
    print('Database OK')
    exit(0)
except Exception as e:
    print(f'Venter på database... ({e})')
    exit(1)
" && break
    sleep 2
done

exec uvicorn app.main:app --host 0.0.0.0 --port 8000 
