forked from fzaninotto/uptime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
104 lines (86 loc) · 2.71 KB
/
app.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
/*
* Monitor remote server uptime.
*/
var http = require('http');
var express = require('express');
var config = require('config');
var socketIo = require('socket.io');
var fs = require('fs');
var monitor = require('./lib/monitor');
var analyzer = require('./lib/analyzer');
var CheckEvent = require('./models/checkEvent');
var Ping = require('./models/ping');
// database
var mongoose = require('./bootstrap');
// monitor
var m;
if (config.autoStartMonitor) {
m = monitor.createMonitor(config.monitor);
m.start();
}
var a = analyzer.createAnalyzer(config.analyzer);
a.start();
// web front
var app = module.exports = express();
var server = http.createServer(app);
app.configure(function(){
app.use(app.router);
// the following middlewares are only necessary for the mounted 'dashboard' app,
// but express needs it on the parent app (?) and it therefore pollutes the api
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser());
app.use(express.session({ secret: 'qdfegsgkjhflkquhfskqdjfhskjdfh' }));
});
app.configure('development', function() {
if (config.verbose) mongoose.set('debug', true);
app.use(express.static(__dirname + '/public'));
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function() {
var oneYear = 31557600000;
app.use(express.static(__dirname + '/public', { maxAge: oneYear }));
app.use(express.errorHandler());
});
// Routes
app.use('/api', require('./app/api/app'));
app.use('/dashboard', require('./app/dashboard/app'));
app.get('/', function(req, res) {
res.redirect('/dashboard/events');
});
app.get('/favicon.ico', function(req, res) {
res.redirect(301, '/dashboard/favicon.ico');
});
// Sockets
var io = socketIo.listen(server);
io.configure('production', function() {
io.enable('browser client etag');
io.set('log level', 1);
});
io.configure('development', function() {
if (!config.verbose) io.set('log level', 1);
});
CheckEvent.on('afterInsert', function(event) {
io.sockets.emit('CheckEvent', event.toJSON());
});
io.sockets.on('connection', function(socket) {
socket.on('set check', function(check) {
socket.set('check', check);
});
Ping.on('afterInsert', function(ping) {
socket.get('check', function(err, check) {
if (ping.check == check) {
socket.emit('ping', ping);
}
});
});
});
// load plugins
fs.exists('./plugins/index.js', function(exists) {
if (exists) {
require('./plugins').init(app, io, config, mongoose);
}
});
var port = process.env.PORT || config.server.port;
server.listen(port);
console.log("Express server listening on port %d in %s mode", port, app.settings.env);