-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
129 lines (112 loc) · 4.26 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
"use strict";
const _ = require('lodash');
const express = require('express');
const bodyParser = require('body-parser');
const serveStatic = require('serve-static');
// Configuration file
const configuration = require('./config');
// AirVantage API wrapper
const AirVantage = require('airvantage');
// Simplified HTTP requests
const got = require('got');
// Create the web app server + socket.io setup
const app = express();
const server = require('http').Server(app);
const socketio = require('socket.io');
const io = socketio(server);
// In memory clients storage
let clients = {};
// Express webapp setup
app.use(bodyParser.json());
app.engine('.html', require('ejs').__express);
app.set('views', './views');
app.set('view engine', 'html');
// "/" route that displays what is received thru the webook
app.get('/', (req, res) => res.render('index'));
// Webhook route
app.post('/alert', (req, res) => {
// Sample JSON sent to the webhook
// {
// "name": "event.alert.rule.triggered",
// "date": 1473091118798,
// "content": {
// "alert.uid": "e3cf3fed7d9c41beb33facbffaca6504",
// "rule.uid": "35b00600117d4cb58437eccd0935a305",
// "target.uid": "88e085b6012a408f9e2582b89a3b7161"
// }
// }
const alertState = req.body.content,
alertRuleUid = alertState["rule.uid"],
alertUid = alertState["alert.uid"],
systemUid = alertState["target.uid"];
// Loop thru the list of connected clients
_.each(clients, (token, socketId) => {
let alertRule, alertDetails, system;
// Clients may have not provided an AirVantage token in the url
// Just display send the raw JSON
if (!token) {
io.sockets.connected[socketId].emit('alerts', {
content: req.body
});
} else {
// Otherwise make additional AirVantage API calls to get more details
const airvantage = new AirVantage({
serverUrl: configuration.airvantageHost
});
airvantage.authenticate({
token: token
})
.then(() => airvantage.getDetailsAlertRule(alertRuleUid))
.then(res => alertRule = res)
.then(() => airvantage.getDetailsAlert(alertUid))
.then(res => alertDetails = res)
.then(() => airvantage.getDetailsSystem(systemUid))
// Finally send to the client all compiled data
.then(system => io.sockets.connected[socketId].emit('alerts', {
alertRule: alertRule,
alertDetails: alertDetails,
system: system,
content: req.body
}))
.catch(res => {
// In case of an error, simply send the raw JSON
console.error(res);
io.sockets.connected[socketId].emit('alerts', {
content: req.body
});
});
}
});
res.status(200).send('OK');
});
/**
* How many connected clients
*/
function getNbClients() {
return Object.keys(clients).length;
}
// Socket.io setup
io.on('connection', socket => {
// Get the possible AirVantage token that will be used to get more details on the received alerts
const token = socket.request._query["token"];
// Store it
const clientId = socket.id;
clients[clientId] = token;
const nbClients = getNbClients();
console.log("New client connected with id:", clientId, "and token:", token);
console.log(nbClients, "client(s) connected");
// Handshake with the new client
socket.emit('welcome', nbClients);
// Inform every clients that someone just connected
io.emit('updateNbClients', nbClients);
// Handle client disconnection
socket.on('disconnect', () => {
delete clients[clientId];
console.log("Someone just left");
console.log(getNbClients(), "client(s) connected");
// Inform everyone that someone just left
io.emit('updateNbClients', getNbClients())
});
});
app.set('port', process.env.PORT || 3000);
server.listen(app.get("port"), () => console.log('AirVantage alert webhook app listening on port', app.get("port"), '\\o/'));