<?php
$config = require __DIR__ . '/config.php';

$dsn = sprintf(
  'mysql:host=%s;dbname=%s;charset=%s',
  $config['db']['host'],
  $config['db']['name'],
  $config['db']['charset']
);

try {
  $pdo = new PDO($dsn, $config['db']['user'], $config['db']['pass'], [
    PDO::ATTR_ERRMODE => ($config['env'] === 'dev' ? PDO::ERRMODE_EXCEPTION : PDO::ERRMODE_SILENT),
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
    PDO::ATTR_EMULATE_PREPARES => false,
  ]);
} catch (PDOException $e) {
  http_response_code(500);
  echo "Database connection failed.";
  if ($config['env'] === 'dev') {
    echo "<pre>" . htmlspecialchars($e->getMessage()) . "</pre>";
  }
  exit;
}
