Skip to content

Commit 0e4f8ee

Browse files
Load anv validate env vars using dotenv, and update database connection
1 parent afbc42a commit 0e4f8ee

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

public/index.php

+26-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
use DI\Container;
66
use Doctrine\DBAL\Connection;
77
use Doctrine\DBAL\DriverManager;
8+
use Doctrine\DBAL\Tools\DsnParser;
9+
use Dotenv\Dotenv;
10+
use Dotenv\Exception\ValidationException;
811
use Exception;
912
use Naisdevice\Jita\Controllers\ApiController;
1013
use Naisdevice\Jita\Controllers\IndexController;
@@ -26,10 +29,32 @@
2629

2730
require __DIR__ . '/../vendor/autoload.php';
2831

32+
$dotenv = Dotenv::createImmutable(dirname(__DIR__));
33+
$dotenv->safeLoad();
34+
35+
try {
36+
$requiredEnvVars = [
37+
'ISSUER_ENTITY_ID',
38+
'LOGIN_URL',
39+
'LOGOUT_URL',
40+
'SAML_CERT',
41+
'API_PASSWORD',
42+
'DB_URL',
43+
];
44+
45+
$dotenv->required($requiredEnvVars)->notEmpty();
46+
} catch (ValidationException $e) {
47+
http_response_code(503);
48+
echo sprintf('Missing one or more required environment variable(s): %s', join(', ', $requiredEnvVars));
49+
exit;
50+
}
51+
2952
define('DEBUG', '1' === env('DEBUG'));
3053

3154
try {
32-
$connection = DriverManager::getConnection(['url' => env('DB_URL')]);
55+
$connection = DriverManager::getConnection(
56+
(new DsnParser(['postgres' => 'pdo_pgsql']))->parse(env('DB_URL'))
57+
);
3358
} catch (Exception $e) {
3459
http_response_code(503);
3560
echo 'Unable to connect to the database';
@@ -135,7 +160,6 @@
135160
]));
136161
$app->add(TwigMiddleware::createFromContainer($app, Twig::class));
137162
$app->add(new Middleware\RemoveDuplicateAuthHeader());
138-
$app->add(new Middleware\EnvironmentValidation(getenv()));
139163
$app
140164
->addErrorMiddleware(DEBUG, true, true)
141165
->setDefaultErrorHandler(function (Request $request, Throwable $exception, bool $displayErrorDetails) use ($app) {

0 commit comments

Comments
 (0)