-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
95 lines (74 loc) · 1.97 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
global.log = console.log;
global.clc = require('cli-color');
global._ = require('lodash');
global.EventEmitter = require('events').EventEmitter;
process.on('uncaughtException', function(err){
if(err.stack) console.log(clc.red(err.stack));
else console.log(err);
});
global.fs = require('fs');
global.util = require('util');
global.YAML = require('yamljs');
global.cookie = require('cookie');
global.RE = {
void: function(m){}
};
global.POST = {};
global.GET = {};
global.S = {};
global.Sessions = {};
global.ecb = function(e){
console.log(e);
};
global.api = {
check:{},
};
global.mod = {};
global.sys = new EventEmitter();
sys.setMaxListeners(900);
var stdin = process.openStdin();
stdin.setEncoding('utf8');
stdin.on('data', input => {
console.log(eval(input));
});
global.cfg = global.Cfg = YAML.load('./config.yaml');
require('./ctrl/srv.js');
var initiate = () => {
cfg.modules.forEach((name) => {
var file = Cfg.path.modules + name + '.js';
var module = mod[name] = require(file);
console.log('load module: '+name);
if(cfg.devMode)
fs.watchFile(file, function(curr, prev){
console.log(clc.yellow(curr.mtime.toString()) +' '+ clc.blue(name));
delete require.cache[require.resolve(file)];
require(file);
if(module._reload) module._reload();
});
});
process.emit('loadedModules');
sys.emit('loaded');
}
if(Cfg.email)
global.email = require("emailjs").server.connect(Cfg.email);
if(Cfg.mongodb){
const MongoClient = global.mongo = require('mongodb').MongoClient;
MongoClient.connect(
Cfg.mongodb.url,
{
useNewUrlParser: true,
useUnifiedTopology: true
}
).then(client => {
global.db = client.db(Cfg.mongodb.name);
console.log(db);
initiate();
}).catch(e => console.error('error', e));
}
process.on('unhandledRejection', (reason, promise) => {
console.log('Unhandled Rejection at:', reason.stack || reason);
});
process.on('uncaughtException', function (err) {
console.error(err);
console.log("Node NOT Exiting...");
});