
The Dockerfile (and docker-compose) simplify the server's deployment. The environment variables avoid secrets in the code.
38 lines
887 B
PHP
38 lines
887 B
PHP
<?php
|
|
|
|
// Singleton to connect db.
|
|
class database
|
|
{
|
|
// Hold the class instance.
|
|
private static $instance = null;
|
|
private $conn;
|
|
|
|
// The db connection is established in the private constructor.
|
|
private function __construct()
|
|
{
|
|
try {
|
|
$this->conn = new PDO(
|
|
'mysql:host=' . getenv('MYSQL_HOST') . ';dbname=' . getenv('MYSQL_DATABASE') . ';port=' . getenv('MYSQL_PORT'),
|
|
getenv('MYSQL_USER'),
|
|
getenv('MYSQL_PASSWORD')
|
|
);
|
|
} catch (PDOException $e) {
|
|
die('Connection failed: ' . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
public static function getInstance()
|
|
{
|
|
if (!self::$instance) {
|
|
self::$instance = new database();
|
|
}
|
|
|
|
return self::$instance;
|
|
}
|
|
|
|
public function getConnection()
|
|
{
|
|
return $this-> conn;
|
|
}
|
|
}
|