forked from active911/asterisk-analyze
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
138 lines (101 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
126
127
128
129
130
131
var server=require("http").createServer();
var express=require('express');
var morgan=require('morgan');
//var url=require("url");
var log = require("bunyan").createLogger({"name":"asterisk-analyze"});
var fs = require('fs');
var stream=require('stream');
var mysql = require("promise-mysql");
var WebsocketServer=require("ws").Server;
var redis = new (require("ioredis"))();
// Read the config
var config = JSON.parse(fs.readFileSync('config.json'));
// A stream to pipe morgan (http logging) to bunyan
(info_log_stream=new stream.Writable())
._write=(chunk, encoding, done) => {
log.info(chunk.toString().replace(/^[\r\n]+|[\r\n]+$/g,""));
done();
};
// Websockets server
var wss=new WebsocketServer({server: server});
// Alert clients when new call happens
redis.subscribe("calls",(err)=>console.log(err?err:"Subscribed to 'calls' on Redis"));
redis.on("message", (channel, msg)=>{
// Verify channel
if(channel!="calls") return;
// Get the call
let call=JSON.parse(msg);
// Only send finished calls (so we don't complicate things and maybe cause the webapp to ingest mutating data)
if(!call.end) return;
// Broadcast call to all clients
wss.clients.forEach((client)=>client.send(msg));
log.info("Call ended, sending to all clients");
});
// wss.on("connection",(ws)=>{
// let location=url.parse(ws.upgradeReq.url, true);
// log.info("websocket client connected to path '"+location.pathname+"'");
// // ws.send("Hello...",()=>{});
// // ws.on("message",(msg)=>{
// // log.info("Websockets message: "+msg);
// // })
// .on("close",()=>{
// clearInterval(iv);
// log.info("websocket client closed");
// });
// });
// Web app
var app = express();
server.on("request", app);
app
.use(morgan(':remote-addr ":method :url HTTP/:http-version" :status :res[content-length] - :response-time ms', { "stream" : info_log_stream })) // Morgan HTTP logging
.use(express.static("public"))
.get("/api/calls/:year/:month",(req, res)=>{
// Calculate date ranges
let [year, month]=[req.params.year, req.params.month];
let from=`${year}-${month}-01`;
month++;if(month>12) {month=1; year++;}
let to=`${year}-${month}-01`;
// Fetch from database
db.query("SELECT id, data FROM "+(nconf.get('mysql').table||"calls")+" WHERE stamp BETWEEN ? AND ? ORDER BY stamp",[from, to])// WHERE stamp = ?", [moment(call.start).format('YYYY-MM-DD HH:mm:ss')])
.then((rows) =>{
var data=[];
for(let r of rows) {
data.push({
"type" : "call",
"id" : r.id,
"attributes" : JSON.parse(r.data)
});
}
res
.set('Content-Type', 'text/json')
.status(200)
.send(JSON.stringify({
"data" : data
}));
})
.catch((e)=>{
log.error("Error: " + e);
res
.set('Content-Type', 'text/json')
.status(500)
.send(JSON.stringify({
"errors" : [
{
"title" : "internal error",
"detail": "See server logs for details"
}
]
}));
});
});
// Connect to the database and start the server
var db;
mysql
.createConnection(config.mysql)
.then((c)=>{
log.info("Connected to database");
db=c;
server.listen(3000, function() {
log.info("webservice listening on %s", 3000);
});
});