-
Notifications
You must be signed in to change notification settings - Fork 94
/
sugarizer.js
82 lines (73 loc) · 2.63 KB
/
sugarizer.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
// require files
var express = require('express'),
http = require('http'),
https = require('https'),
fs = require('fs'),
process = require('process'),
settings = require('./config/settings'),
wait4db = require('./config/wait4db'),
common = require('./dashboard/helper/common'),
presence = require('./api/middleware/presence'),
ini = settings.load(),
app = express(),
server = null;
// Prevent uncaught exception
process.on("uncaughtException", function(err) {
console.error("FATAL ERROR, uncaught exception '" + err.message + "'");
console.error(err.stack);
process.exit(-1);
});
// wait for database
wait4db.waitConnection(ini, function(db) {
if (!db) {
console.log("Cannot connect with the database");
process.exit(-1);
}
// init common
common.init(ini);
//configure app setting
require('./config/main')(app, ini);
// include api routes
require('./api/route')(app, ini, db);
// include dashboard routes
require('./dashboard/route')(app, ini);
// Handle https
if (ini.security.https) {
var credentials = common.loadCredentials(ini);
if (!credentials) {
console.log("Error reading HTTPS credentials");
process.exit(-1);
}
server = https.createServer(credentials, app);
} else {
server = http.createServer(app);
}
// Check node version
var nodev = (process.versions.node).split(".");
if (parseInt(nodev[0]) < 10) {
console.log("Ooops! Sugarizer require node v10+, current version is v"+process.versions.node);
process.exit(-1);
}
// Start listening
var info = JSON.parse(fs.readFileSync("./package.json", 'utf-8'));
console.log(" _____ _ ");
console.log(" / ____| (_) ");
console.log(" | (___ _ _ __ _ __ _ _ __ _ _______ _ __ ");
console.log(" \\___ \\| | | |/ _` |/ _` | '__| |_ / _ \\ '__|");
console.log(" ____) | |_| | (_| | (_| | | | |/ / __/ | ");
console.log(" |_____/ \\__,_|\\__, |\\__,_|_| |_/___\\___|_| ");
console.log(" __/ | ");
console.log(" |___/ ");
console.log(info.description+" v"+info.version);
console.log("node v"+process.versions.node);
console.log("Settings file './env/"+(process.env.NODE_ENV ? process.env.NODE_ENV : 'sugarizer')+".ini'");
presence.init(ini, server, app);
server.listen(ini.web.port, function() {
console.log("API is listening on"+(ini.security.https ? " secure":"")+" port " + ini.web.port + "...");
}).on('error', function(err) {
console.log("Ooops! cannot launch API on port "+ ini.web.port+", error code "+err.code);
process.exit(-1);
});
});
//export app for testing
module.exports = app;