absenat
Version:
dedicated messaging service core
73 lines (68 loc) • 2.13 kB
JavaScript
/**
* @author mr-exception
* @description This file is the main executable of the source. By executing this file in nodejs a node is completely out of...
*/
require('./general');
global.fs = require('fs');
global.io = require('socket.io')();
global.ioClient = require('socket.io-client');
global.chalk = require('chalk');
global.db = require('./db');
global.cli = require('./cli');
global.command_functions = require('./commands');
global.uuidv4 = require('uuid/v4');
global.handlers = require('./handlers');
/**
* loading config files
*/
console.log('loading configs...');
const yargs = require('yargs');
const argv = yargs
.option('config', {
alias: 'c',
description: 'Loads the config file',
type: 'string',
}).option('fresh', {
alias: 'f',
description: 'Starts the script with fresh and empty data',
type: 'boolean',
})
.help()
.alias('help', 'h')
.argv;
let config_file = argv.config;
if (config_file === undefined)
config_file = 'default';
if(!fs.existsSync(`./configs/${config_file}.json`)){
console.log(chalk.red(`Err: config file ${config_file} not found`));
process.exit();
}
global.config = JSON.parse(fs.readFileSync(`./configs/${config_file}.json`, `utf8`));
console.log(`config file ${chalk.blue(config_file)} loaded ${chalk.green('successfully')}.`);
/**
* loading databses
*/
db.getMyDatabase(config_file, argv.fresh == true);
db.getBridgeDatabase(config_file, argv.fresh == true);
db.getConnectionsDatabase(config_file, argv.fresh == true);
console.log(chalk.green('all databases loaded'));
/**
* load RSA encryption keys
*/
global.NodeRSA = require('node-rsa');
console.log('loading RSA keys');
global.myPublicKey = createPublicKey(config.public_key);
global.myPrivateKey = createPrivateKey(config.private_key);
/**
* start listening the target port
*/
console.log(`hello ${chalk.blue(config.nickname)}!`);
global.connections = {}
// incoming messages storage
io.listen(config.port);
console.log(chalk.yellow(`listening on ${config.port}`));
io.on('connection', handlers.connection_request);
/**
* starting the command line interface
*/
cli.run();