readConfig.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. The MIT License (MIT)
  3. Copyright (c) 2014-2018 Nikolai Suslov and the Krestianstvo.org project contributors. (https://github.com/NikolaySuslov/livecodingspace/blob/master/LICENSE.md)
  4. ADL VW Sandbox Apache 2.0 license(https://github.com/NikolaySuslov/livecodingspace/blob/master/licenses/LICENSE_ADL_Sandbox.md)
  5. */
  6. var fs = require('fs'),
  7. argv = require('yargs').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 = 3007
  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(e.message);
  65. console.log('Could not read config file. Loading defaults.');
  66. }
  67. //save configuration into global scope so other modules can use.
  68. global.configuration = configSettings;
  69. }
  70. exports.parseConfigOptions = parseConfigOptions;
  71. exports.readConfigFile = readConfigFile;