This repository has been archived by the owner on Apr 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
125 lines (107 loc) · 3.32 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
"use strict";
var express = require('express'),
browserify = require('browserify'),
app = express(),
server = require('http').createServer(app),
io = require('socket.io').listen(server),
nconf = require('nconf').file({'file': 'config.json'}),
aws = require('plata'),
getConfig = require('junto'),
model = require('./lib/model'),
Metric = model.Metric,
Graph = model.Graph,
moment = require('moment'),
fs = require('fs'),
plog = require('plog');
// plog.find(/^stormwatch/).level('silly');
plog.all().level('silly');
function createBundle(watch){
var rebuildLock = false,
b = browserify('./static/js/main.js', {
require : {jquery: 'jquery-browserify', backbone: 'backbone-browserify-lodash'},
'watch': watch,
'debug': true
});
if(watch){
b.on('bundle', function(){
if(!rebuildLock){
rebuildLock = true;
app.emit('rebuild', b);
setTimeout(function(){
rebuildLock = false;
}, 1000);
}
});
}
return b;
}
app.configure(function(){
app.set('browserify', createBundle(true));
app.set('views', __dirname + '/views');
app.set('view engine', 'hbs');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use("/static", express.static(__dirname + '/static'));
app.use(app.get('browserify'));
getConfig().then(function(c){
nconf.overrides(c);
aws.connect(c.aws);
});
app.use(express.logger({'format': 'tiny'}));
Metric.fromConfig(nconf.get('metrics'));
Graph.fromConfig(nconf.get("graphs"));
});
app.get('/', function(req, res){
res.render('test');
});
app.get('/metric', function(req, res){
Metric.getAll().then(function(metrics){
res.send(metrics);
});
});
app.get('/metric/:id', function(req, res){
Metric.getById(req.param('id')).then(function(metric){
var period = Number(req.param('period', 300)),
start = moment().subtract('minutes', 300)._d;
metric.loadAllSeriesData(period, start, new Date()).then(function(series){
metric.series = series;
res.send(metric);
}, function(err){
res.send(400, err.message);
});
}, function(err){
res.send(400, err.message);
});
});
app.get('/graph', function(req, res){
Graph.getAll().then(function(graphs){
res.send(graphs);
});
});
app.get('/graph/:id', function(req, res){
Graph.getById(req.param('id')).then(function(graph){
var period = Number(req.param('period', 300)),
start = moment().subtract('minutes', 300)._d;
graph.loadAllSeriesData(period, start, new Date()).then(function(data){
graph.series = data.series;
graph.metrics = data.metrics;
res.send(graph);
}, function(err){
res.send(400, err.message);
});
}, function(err){
res.send(400, err.message);
});
});
server.listen(3611);
io.sockets.on('connection', function(socket){
app.on('rebuild', function(){
socket.namespace.in(socket.flags.room).packet({
'type': 'event',
'name': 'rebuild',
'args': []
});
});
});
module.exports = app;