-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
180 lines (151 loc) · 4.41 KB
/
server.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
'use strict';
var argv = require('yargs').argv;
var E = require('linq');
var moment = require('moment');
var express = require('express');
var bodyParser = require('body-parser')
var DailyReport = require('./daily-report');
var assert = require('chai').assert;
var cron = require('cron');
var path = require('path');
var conf = require('confucious');
var fs = require('fs');
process.on('uncaughtException', function (err) {
console.error('Uncaught Exception: ' + err.message + '\r\n' + err.stack);
});
//
// Start the log server.
//
var startServer = function (conf, outputPlugin) {
if (!outputPlugin) {
throw new Error("'outputPlugin' argument not specified.");
}
assert.isObject(outputPlugin);
assert.isFunction(outputPlugin.emit);
assert.isFunction(outputPlugin.retrieveLogs);
var app = express();
app.use(bodyParser.json());
//
// Check that the server is alive (used by the server monitor)
//
app.get("/alive", function (req, res) {
res.json({ ok: 1 });
});
//
// Preprocess log to our expected structure.
//
var transformLog = function (log) {
return {
Timestamp: moment(log.Timestamp).toDate(),
Level: log.Level,
MessageTemplate: log.MessageTemplate,
RenderedMessage: log.RenderedMessage,
Properties: E.from(Object.keys(log.Properties))
.toObject(
function (propertyName) {
return propertyName;
},
function (propertyName) {
return log.Properties[propertyName].Value;
}
),
};
}
app.post('/log', function (req, res) {
if (!req.body) {
throw new Error("Expected 'body'");
}
if (!req.body.Logs) {
throw new Error("Expected 'Logs' property on body");
}
var logs = E.from(req.body.Logs)
.select(transformLog)
.toArray();
outputPlugin.emit(logs);
res.status(200).end();
});
return new Promise(function (resolve, reject) {
var server = app.listen(conf.get("port"), "0.0.0.0", function (err) {
if (err) {
reject(err);
return;
}
var host = server.address().address;
var port = server.address().port;
console.log("Receiving logs at " + host + ":" + port + "/log");
resolve(server);
});
});
};
//
// http://stackoverflow.com/a/6398335/25868
//
if (require.main === module) {
console.log('Starting from command line.');
//
// Run from command line.
//
var configFilePath = path.join(__dirname, 'config.json');
if (fs.existsSync(configFilePath)) {
conf.pushJsonFile(configFilePath);
}
else {
console.log("!! " + configFilePath + " not found.");
}
conf.pushArgv();
if (!conf.get('db')) {
throw new Error("'db' not specified in config.json or as command line option.");
}
if (!conf.get('logsCollection')) {
throw new Error("'logsCollection' not specified in config.json or as command line option.");
}
if (!conf.get('errorsCollection')) {
throw new Error("'errorsCollection' not specified in config.json or as command line option.");
}
if (!conf.get('port')) {
throw new Error("'port' not specified in config.json or as command line option.");
}
require('./mongodb-output')(conf)
.then(logStoragePlugin => {
return startServer(conf, logStoragePlugin)
.then(() => {
var emailDailyReport = function () {
console.log("Generating daily logging report email...");
var dailyReport = new DailyReport(logStoragePlugin, conf);
dailyReport.emailDailyReport(conf.get('mail:dailyReportSpec'))
.then(() => {
console.log("...generated daily logging report email.");
})
.catch(err => {
console.error("Failed to generate daily report\r\n" + err.stack);
})
;
};
if (argv.dailyReport) {
emailDailyReport();
return;
}
console.log("Starting daily report cron...");
var dailyReportSchedule = conf.get('dailyReportSchedule');
assert.isString(dailyReportSchedule);
console.log('Daily report schduled: ' + dailyReportSchedule);
var CronJob = cron.CronJob;
var cronJob = new CronJob({
cronTime: dailyReportSchedule,
onTick: emailDailyReport,
start: false,
});
cronJob.start();
});
})
.catch(err => {
console.error("Failed to start server.\r\n" + err.stack);
})
;
}
else {
//
// Required from another module.
//
module.exports = startServer;
}