config.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. const fs = require('fs');
  2. // Chemin vers le fichier
  3. const path = './config.json';
  4. // Verif que le fichier de config existe
  5. if (!fs.existsSync(path)) {
  6. throw "Config file not found";
  7. }
  8. // Lecture du fichier
  9. const data = fs.readFileSync(path).toString();
  10. const config = JSON.parse(data);
  11. // Verification de la presences des différents elements
  12. if (config.storage === undefined) {
  13. throw 'Storage undefined in the config file';
  14. } else if (config.auth === undefined) {
  15. throw 'Auth undefined in the config file';
  16. } else if (config.findPort === undefined) {
  17. throw 'findPort undefined in the config file';
  18. } else if (config.basePort === undefined) {
  19. throw 'basePort undefined in the config file';
  20. } else if (config.highestPort === undefined) {
  21. throw 'highestPort undefined in the config file';
  22. } else if (config.https === undefined) {
  23. throw 'https undefined in the config file';
  24. }
  25. //Verification valeur
  26. if (config.storage !== 'database' && config.storage !== 'file') {
  27. throw 'Bad value for storage: database or file expected';
  28. } else if (typeof config.auth !== 'boolean') {
  29. throw 'Bad value for auth: boolean expected';
  30. } else if (typeof config.findPort !== 'boolean') {
  31. throw 'Bad value for findPort: boolean expected';
  32. } else if (config.basePort < 0 || config.basePort > 65535) {
  33. throw 'Bad value for basePort: number between 0 and 65535 expected';
  34. } else if (config.highestPort < 0 || config.highestPort > 65535 || config.highestPort < config.basePort) {
  35. throw 'Bad value for highestPort: number between 0 and 65535 and higher than or equal basePort expected';
  36. } else if (typeof config.https !== 'boolean') {
  37. throw 'Bad value for https: boolean expected';
  38. }
  39. module.exports = config;