Browse Source

use config instead argv

Nikolay Suslov 5 years ago
parent
commit
ba7a36b7b2
6 changed files with 128 additions and 30 deletions
  1. 2 3
      .gitignore
  2. 8 0
      config_example.json
  3. 14 21
      index.js
  4. 15 5
      package-lock.json
  5. 1 1
      package.json
  6. 88 0
      readConfig.js

+ 2 - 3
.gitignore

@@ -4,8 +4,7 @@ node_modules
 certs
 ossl
 radata
-radata-p
 data.json
 *radata*
-*radata-p*
-*report*
+*report*
+config.json

+ 8 - 0
config_example.json

@@ -0,0 +1,8 @@
+{
+    "ssl": true,
+    "port": 3001,
+    "sslKey":"./certs/server-key.pem",
+    "sslCert":"./certs/server-crt.pem",
+    "sslCA":"./certs/ca-crt.pem",
+    "certPwd": ""
+}

+ 14 - 21
index.js

@@ -1,33 +1,26 @@
-var fs = require('fs'),
-    argv = require('optimist').argv
+// var fs = require('fs'),
+//     argv = require('optimist').argv
+
+var config = require('./readConfig');
 
 var Gun = require('gun')
-        require('gun/sea')
-        require('gun/lib/path')
-        require('gun/lib/not')
-        require('gun/nts')
+require('gun/sea')
+require('gun/lib/path')
+require('gun/lib/not')
+require('gun/nts')
 
-var ssl = ( argv.s  || argv.ssl );
-var pass = ( ( argv.w) ? ( argv.w) : undefined );
-var sslOptions = {
-    key: ( ( argv.k || argv.key ) ? fs.readFileSync( argv.k || argv.key ) : undefined ),
-    cert: ( ( argv.c || argv.cert ) ? fs.readFileSync( argv.c || argv.cert ) : undefined ),
-    ca: ( ( argv.t || argv.ca ) ? fs.readFileSync( argv.t || argv.ca ) : undefined ),
-    passphrase: JSON.stringify(pass)
-};
-var port = ( ( argv.p || argv.port ) ? ( argv.p || argv.port ) : 8080 );
+config.readConfigFile();
+var conf = config.parseConfigOptions();
 
 
-var config = { port: port};
+var config = { port: conf.port };
 //create the server
 
-if(ssl){
-    config.key = sslOptions.key;
-    config.cert = sslOptions.cert;
-    config.server = require('https').createServer(config, Gun.serve(__dirname));
+if (conf.ssl) {
+    config.server = require('https').createServer(conf.sslOptions, Gun.serve(__dirname));
 } else {
     config.server = require('http').createServer(Gun.serve(__dirname));
 }
 
-global.gun = Gun({web: config.server.listen(config.port), axe: false }); //until: 5000, chunk: 10
+global.gun = Gun({ web: config.server.listen(config.port), axe: false }); //until: 5000, chunk: 10
 console.log('Relay peer started on port ' + config.port + ' with /gun');

+ 15 - 5
package-lock.json

@@ -53,11 +53,6 @@
         "ws": "~>7.1.0"
       }
     },
-    "minimist": {
-      "version": "0.0.8",
-      "resolved": "http://registry.npmjs.org/minimist/-/minimist-0.0.8.tgz",
-      "integrity": "sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0="
-    },
     "mkdirp": {
       "version": "0.5.1",
       "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.1.tgz",
@@ -65,6 +60,14 @@
       "optional": true,
       "requires": {
         "minimist": "0.0.8"
+      },
+      "dependencies": {
+        "minimist": {
+          "version": "0.0.8",
+          "resolved": "https://registry.npmjs.org/minimist/-/minimist-0.0.8.tgz",
+          "integrity": "sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0=",
+          "optional": true
+        }
       }
     },
     "nan": {
@@ -92,6 +95,13 @@
       "requires": {
         "minimist": "~0.0.1",
         "wordwrap": "~0.0.2"
+      },
+      "dependencies": {
+        "minimist": {
+          "version": "0.0.10",
+          "resolved": "https://registry.npmjs.org/minimist/-/minimist-0.0.10.tgz",
+          "integrity": "sha1-3j+YVD2/lggr5IrRoMfNqDYwHc8="
+        }
       }
     },
     "ramda": {

+ 1 - 1
package.json

@@ -6,7 +6,7 @@
   "scripts": {
     "test": "echo \"Error: no test specified\" && exit 1",
     "start": "node ./index.js",
-    "startSSL": "node ./index.js -p 8080 -s -k ./certs/192.168.0.10.key -c ./certs/192.168.0.10.pem"
+    "startSSL": "node ./index.js -p 8080 -s -k ./certs/192.168.0.10.key -c ./certs/192.168.0.10.pem "
   },
   "repository": {
     "type": "git",

+ 88 - 0
readConfig.js

@@ -0,0 +1,88 @@
+/*
+The MIT License (MIT)
+Copyright (c) 2014-2018 Nikolai Suslov and the Krestianstvo.org project contributors. (https://github.com/NikolaySuslov/lcs-reflector/blob/master/LICENSE.md)  
+ADL VW Sandbox Apache 2.0 license(https://github.com/NikolaySuslov/lcs-reflector/blob/master/licenses/LICENSE_ADL_Sandbox.md)
+*/
+
+var fs = require('fs'),
+    argv = require('optimist').argv
+    //logger = require('./logger')
+
+function parseConfigOptions() {
+
+  let conf = {
+      sslOptions: {}
+  }
+
+  var ssl = (argv.s || argv.ssl);
+  var pass = ((argv.w) ? (argv.w) : undefined);
+  var sslOptions = {
+      key: ((argv.k || argv.key) ? fs.readFileSync(argv.k || argv.key) : undefined),
+      cert: ((argv.c || argv.cert) ? fs.readFileSync(argv.c || argv.cert) : undefined),
+      ca: ((argv.t || argv.ca) ? fs.readFileSync(argv.t || argv.ca) : undefined),
+      passphrase: JSON.stringify(pass)
+  };
+  conf.sslOptions = sslOptions;
+
+  var port = ((argv.p || argv.port) ? (argv.p || argv.port) : undefined);
+
+  if (!ssl) {
+      if (global.configuration.ssl === undefined)
+          ssl = false;
+
+      if (global.configuration.ssl) {
+          ssl = true;
+          if (global.configuration.sslKey !== "")
+              conf.sslOptions.key = fs.readFileSync(global.configuration.sslKey)
+
+          if (global.configuration.sslCert !== "")
+              conf.sslOptions.cert = fs.readFileSync(global.configuration.sslCert)
+
+          if (global.configuration.sslCA !== "")
+              conf.sslOptions.ca = fs.readFileSync(global.configuration.sslCA)
+
+          if (global.configuration.certPwd !== "")
+              conf.sslOptions.passphrase = JSON.stringify(global.configuration.certPwd)
+      }
+  }
+
+  if (!port) {
+      if (global.configuration.port === undefined)
+          global.configuration.port = 3001 //by default
+
+      if (global.configuration.port)
+          port = global.configuration.port
+  }
+
+
+  conf.ssl = ssl;
+  conf.port = port
+
+  return conf
+}
+
+function readConfigFile() {
+  var configSettings;
+  var p = process.argv.indexOf('-config');
+
+  //This is a bit ugly, but it does beat putting a ton of if/else statements everywhere
+  var config = p >= 0 ? (process.argv[p + 1]) : './config.json';
+  //logger.warn('loading config from ' + config);
+  console.log('loading config from ' + config);
+  //start the DAL, load configuration file
+  try {
+      configSettings = JSON.parse(fs.readFileSync(config).toString());
+      //logger.info('Configuration read.');
+      console.log('Configuration read.');
+  } catch (e) {
+      configSettings = {};
+      //logger.error(e.message);
+      //logger.error('Could not read config file. Loading defaults.');
+      console.log('Could not read config file. Loading defaults.');
+  }
+  //save configuration into global scope so other modules can use.
+  global.configuration = configSettings;
+}
+
+exports.parseConfigOptions = parseConfigOptions;
+exports.readConfigFile = readConfigFile;