forked from fzaninotto/uptime
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
112 lines (95 loc) · 3.04 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
105
106
107
108
109
110
111
112
/*
* Monitor remote server uptime.
*/
var mongoose = require('mongoose');
var express = require('express');
var config = require('config');
var socketIo = require('socket.io');
var path = require('path');
var monitor = require('./lib/monitor');
var analyzer = require('./lib/analyzer');
var CheckEvent = require('./models/checkEvent');
var Ping = require('./models/ping');
// configure mongodb
mongoose.connect('mongodb://' + config.mongodb.user + ':' + config.mongodb.password + '@' + config.mongodb.server +'/' + config.mongodb.database);
mongoose.connection.on('error', function (err) {
console.error('MongoDB error: ' + err.message);
console.error('Make sure a mongoDB server is running and accessible by this application')
});
if (config.autoStartMonitor) {
m = monitor.createMonitor(config.monitor);
m.start();
}
a = analyzer.createAnalyzer(config.analyzer);
a.start();
var app = module.exports = express.createServer();
// Site
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(reaq, res) {
res.redirect('/dashboard/events');
});
// Sockets
var io = socketIo.listen(app);
io.configure('production', function() {
io.enable('browser client etag');
io.set('log level', 1);
io.set('transports', [
'flashsocket'
, 'htmlfile'
, 'xhr-polling'
, 'jsonp-polling'
]);
});
io.configure('development', function() {
if (!config.verbose) io.set('log level', 1);
io.set('transports', [
'flashsocket'
, 'htmlfile'
, 'xhr-polling'
, 'jsonp-polling'
]);
});
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
path.exists('./plugins/index.js', function(exists) {
if (exists) {
require('./plugins').init(app, io, config, mongoose);
};
});
app.listen(config.server.port);
console.log("Express server listening on port %d in %s mode", config.server.port, app.settings.env);