Skip to content

Commit

Permalink
Implement environment variable support to prevent hard-coded values.
Browse files Browse the repository at this point in the history
Issue MIDU-192
  • Loading branch information
Gavric-Sava committed Apr 4, 2022
1 parent b96b71d commit 43033a4
Show file tree
Hide file tree
Showing 8 changed files with 256 additions and 20 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
assets/images

/vendor/
volumes/database/scripts/initialize_db.sql
config/initialize_db.sql
/volumes/
database.env
4 changes: 1 addition & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,4 @@ COPY index.php /var/www/html/

RUN chown -R www-data:www-data /var/www/html

RUN a2enmod rewrite

EXPOSE 80
RUN a2enmod rewrite
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"require": {
"php": "7.4.*",
"ext-pdo": "*",
"ext-json": "*"
"ext-json": "*",
"vlucas/phpdotenv": "4.*"
},
"config": {
"platform": {
Expand Down
236 changes: 234 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions database.example.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
SQL_DRIVER=mysql
MYSQL_ROOT_PASSWORD=root_password
MYSQL_DATABASE=database_name
MYSQL_USER=username
MYSQL_PASSWORD=password
MYSQL_HOST=database_host
11 changes: 3 additions & 8 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ services:
image: bookstore
ports:
- "80:80"
volumes:
- ~/Documents/Projects/BookStore/volumes/logs:/var/log/apache2
depends_on:
- database
database:
Expand All @@ -15,10 +13,7 @@ services:
- "3306:3306"
hostname: database
volumes:
- ~/Documents/Projects/BookStore/volumes/database/scripts:/docker-entrypoint-initdb.d
- ./config/initialize_db.sql:/docker-entrypoint-initdb.d/initialize_db.sql
- ~/Documents/data/mysql/bookstore:/var/lib/mysql
environment:
- MYSQL_ALLOW_EMPTY_PASSWORD=yes
- MYSQL_DATABASE=bookstore_db
- MYSQL_USER=bookstore_user
- MYSQL_PASSWORD=password
env_file:
- database.env
3 changes: 3 additions & 0 deletions src/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@

use Logeecom\Bookstore\presentation\routers\Router;

$dotenv = Dotenv\Dotenv::createImmutable($_SERVER['DOCUMENT_ROOT'], 'database.env');
$dotenv->load();

(new Router())->route();
10 changes: 5 additions & 5 deletions src/data_access/DatabaseConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ class DatabaseConnection
*/
private function __construct()
{
$config = include $_SERVER['DOCUMENT_ROOT'] . '/config/database.php';

while (true) {
try {
$dsn = $_ENV['SQL_DRIVER'] . ':host=' . $_ENV['MYSQL_HOST'] . ';dbname=' . $_ENV['MYSQL_DATABASE'];
$this->PDOConnection = new PDO(
$config['driver'] . ':host=' . $config['host'] . ';dbname=' . $config['db_name'],
$config['username'],
$config['password']
$dsn,
$_ENV['MYSQL_USER'],
$_ENV['MYSQL_PASSWORD']
);

error_log("Connection success!");
return;
} catch (PDOException $e) {
Expand Down

0 comments on commit 43033a4

Please sign in to comment.