Spaces:
Running
Running
File size: 1,397 Bytes
445de93 d90e9a6 445de93 d90e9a6 445de93 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | <?php
// Primero intenta conectar con MySQL (XAMPP), fallback a SQLite (Docker / HF Spaces)
$dbHost = '127.0.0.1';
$dbUsuario = 'root';
$dbClave = '';
$dbNombre = 'morphos_db';
$dbPort = 3306;
$dbPath = __DIR__ . '/../data/morphos.db';
if (file_exists(__DIR__ . '/.env')) {
foreach (file(__DIR__ . '/.env', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) as $line) {
if (str_starts_with($line, 'DB_PORT=')) $dbPort = (int) trim(substr($line, 8));
}
}
$conexion = null;
$useSqlite = getenv('DB_FORCE_SQLITE') === '1';
if (!$useSqlite) {
try {
$conexion = new PDO("mysql:host=$dbHost;port=$dbPort;dbname=$dbNombre;charset=utf8mb4", $dbUsuario, $dbClave);
$conexion->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
$conexion = null;
}
}
// Fallback de SQLite
if (!$conexion) {
try {
$conexion = new PDO("sqlite:$dbPath");
$conexion->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$conexion->exec("CREATE TABLE IF NOT EXISTS usuarios (
id INTEGER PRIMARY KEY AUTOINCREMENT,
nombre TEXT NOT NULL,
apellido TEXT NOT NULL,
email TEXT NOT NULL UNIQUE,
password TEXT NOT NULL,
creado_en DATETIME DEFAULT CURRENT_TIMESTAMP
)");
} catch (PDOException $e) {
$conexion = null;
}
}
|