Bladeren bron

Fichier de config et script de lecture et de verification du fichier

Loquicom 5 jaren geleden
bovenliggende
commit
ab2ae8cad7
2 gewijzigde bestanden met toevoegingen van 33 en 0 verwijderingen
  1. 4 0
      node/config.json
  2. 29 0
      node/src/config.js

+ 4 - 0
node/config.json

@@ -0,0 +1,4 @@
+{
+  "storage": "database",
+  "auth": true
+}

+ 29 - 0
node/src/config.js

@@ -0,0 +1,29 @@
+const fs = require('fs');
+
+// Chemin vers le fichier
+const path = './config.json';
+
+// Verif que le fichier de config existe
+if (!fs.existsSync(path)) {
+    throw "Config file not found";
+}
+
+// Lecture du fichier
+const data = fs.readFileSync(path).toString();
+const config = JSON.parse(data);
+
+// Verification de la presences des différents elements
+if (config.storage === undefined) {
+    throw 'Storage undefined in the config file';
+} else if (config.auth === undefined) {
+    throw 'Auth undefined in the config file';
+}
+
+//Verification valeur
+if (config.storage !== 'database' && config.storage !== 'file') {
+    throw 'Bad value for storage: database or file expected';
+} else if (typeof config.auth !== 'boolean') {
+    throw 'Bad value for auth: boolean expected';
+}
+
+module.exports = config;