From 2915ac2193f61f5dfd5ac76303610b322303116f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Micha=C3=ABl=20Minelli?= <michael@minelli.me>
Date: Tue, 30 May 2023 16:59:33 +0200
Subject: [PATCH] Config loader singleton

---
 ExpressAPI/src/config/Config.ts | 49 +++++++++++++++++++++++++++++++++
 1 file changed, 49 insertions(+)
 create mode 100644 ExpressAPI/src/config/Config.ts

diff --git a/ExpressAPI/src/config/Config.ts b/ExpressAPI/src/config/Config.ts
new file mode 100644
index 0000000..980b12f
--- /dev/null
+++ b/ExpressAPI/src/config/Config.ts
@@ -0,0 +1,49 @@
+class Config {
+    private static _instance: Config;
+
+    public readonly api: {
+        port: number
+    };
+
+    public readonly database: {
+        type: string, host: string, port: number, user: string, password: string, database: string
+    };
+
+    public readonly jwtSecretKey: string;
+    public readonly sessionTimeout: number;
+
+    public readonly userPasswordLength: number;
+    public readonly userPasswordSaltRounds: number;
+
+    private constructor() {
+        this.api = {
+            port: Number(process.env.API_PORT)
+        };
+
+        this.database = {
+            type    : process.env.DATABASE_TYPE,
+            host    : process.env.DATABASE_HOST,
+            port    : Number(process.env.DATABASE_PORT),
+            user    : process.env.DATABASE_USER,
+            password: process.env.DATABASE_PASSWORD,
+            database: process.env.DATABASE_NAME
+        };
+
+        this.jwtSecretKey = process.env.JWT_SECRET_KEY;
+        this.sessionTimeout = Number(process.env.SESSION_TIMEOUT);
+
+        this.userPasswordLength = Number(process.env.USER_PASSWORD_LENGTH);
+        this.userPasswordSaltRounds = Number(process.env.USER_PASSWORD_SALT_ROUNDS);
+    }
+
+    public static get instance(): Config {
+        if ( !Config._instance ) {
+            Config._instance = new Config();
+        }
+
+        return Config._instance;
+    }
+}
+
+
+export default Config.instance;
-- 
GitLab