From 4816d1986ab8903f5a12728018c3fa84987cc82c Mon Sep 17 00:00:00 2001
From: Pierre Viara <pierre.viara@outlook.com>
Date: Tue, 29 Aug 2023 16:33:54 +0200
Subject: [PATCH] feat(express-app): add  environment configuration

---
 .env.template                             |  1 +
 src/infrastructure/express-application.ts | 25 ++++++++++++++++++++++-
 2 files changed, 25 insertions(+), 1 deletion(-)
 create mode 100644 .env.template

diff --git a/.env.template b/.env.template
new file mode 100644
index 0000000..8f55cbd
--- /dev/null
+++ b/.env.template
@@ -0,0 +1 @@
+PORT=
\ No newline at end of file
diff --git a/src/infrastructure/express-application.ts b/src/infrastructure/express-application.ts
index d7996f0..625bd68 100644
--- a/src/infrastructure/express-application.ts
+++ b/src/infrastructure/express-application.ts
@@ -1,9 +1,32 @@
 import { ExpressServer } from './express-server';
+import * as dotenv from 'dotenv';
 
 export class ExpressApplication {
-    private server = new ExpressServer('3000');
+    private server: ExpressServer;
+
+    constructor() {
+        this.configureEnvironment();
+
+        const port = this.getPort();
+        this.server = new ExpressServer(port);
+    }
 
     bootstrap(): void {
         this.server.bootstrap();
     }
+
+    private configureEnvironment(): void {
+        dotenv.config({
+            path: '.env',
+        });
+    }
+
+    private getPort(): string {
+        const port = process.env.PORT;
+        if (!port) {
+            throw new Error('No port was found in env file.');
+        }
+
+        return port;
+    }
 }
-- 
GitLab