readConfig.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. The MIT License (MIT)
  3. Copyright (c) 2014-2018 Nikolai Suslov and the Krestianstvo.org project contributors. (https://github.com/NikolaySuslov/lcs-reflector/blob/master/LICENSE.md)
  4. ADL VW Sandbox Apache 2.0 license(https://github.com/NikolaySuslov/lcs-reflector/blob/master/licenses/LICENSE_ADL_Sandbox.md)
  5. */
  6. var fs = require('fs'),
  7. argv = require('optimist').argv
  8. //logger = require('./logger')
  9. function parseConfigOptions() {
  10. let conf = {
  11. sslOptions: {}
  12. }
  13. var ssl = (argv.s || argv.ssl);
  14. var pass = ((argv.w) ? (argv.w) : undefined);
  15. var sslOptions = {
  16. key: ((argv.k || argv.key) ? fs.readFileSync(argv.k || argv.key) : undefined),
  17. cert: ((argv.c || argv.cert) ? fs.readFileSync(argv.c || argv.cert) : undefined),
  18. ca: ((argv.t || argv.ca) ? fs.readFileSync(argv.t || argv.ca) : undefined),
  19. passphrase: JSON.stringify(pass)
  20. };
  21. conf.sslOptions = sslOptions;
  22. var port = ((argv.p || argv.port) ? (argv.p || argv.port) : undefined);
  23. if (!ssl) {
  24. if (global.configuration.ssl === undefined)
  25. ssl = false;
  26. if (global.configuration.ssl) {
  27. ssl = true;
  28. if (global.configuration.sslKey !== "")
  29. conf.sslOptions.key = fs.readFileSync(global.configuration.sslKey)
  30. if (global.configuration.sslCert !== "")
  31. conf.sslOptions.cert = fs.readFileSync(global.configuration.sslCert)
  32. if (global.configuration.sslCA !== "")
  33. conf.sslOptions.ca = fs.readFileSync(global.configuration.sslCA)
  34. if (global.configuration.certPwd !== "")
  35. conf.sslOptions.passphrase = JSON.stringify(global.configuration.certPwd)
  36. }
  37. }
  38. if (!port) {
  39. if (global.configuration.port === undefined)
  40. global.configuration.port = 3001 //by default
  41. if (global.configuration.port)
  42. port = global.configuration.port
  43. }
  44. conf.ssl = ssl;
  45. conf.port = port
  46. return conf
  47. }
  48. function readConfigFile() {
  49. var configSettings;
  50. var p = process.argv.indexOf('-config');
  51. //This is a bit ugly, but it does beat putting a ton of if/else statements everywhere
  52. var config = p >= 0 ? (process.argv[p + 1]) : './config.json';
  53. //logger.warn('loading config from ' + config);
  54. console.log('loading config from ' + config);
  55. //start the DAL, load configuration file
  56. try {
  57. configSettings = JSON.parse(fs.readFileSync(config).toString());
  58. //logger.info('Configuration read.');
  59. console.log('Configuration read.');
  60. } catch (e) {
  61. configSettings = {};
  62. //logger.error(e.message);
  63. //logger.error('Could not read config file. Loading defaults.');
  64. console.log('Could not read config file. Loading defaults.');
  65. }
  66. //save configuration into global scope so other modules can use.
  67. global.configuration = configSettings;
  68. }
  69. exports.parseConfigOptions = parseConfigOptions;
  70. exports.readConfigFile = readConfigFile;