-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
96 lines (78 loc) · 3.04 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
// server.js
// where your node app starts
// init project
var express = require('express');
var Broadcast = require('./broadcast')
var app = express();
const low = require('lowdb');
const FileSync = require('lowdb/adapters/FileSync');
console.log('ENV =============> ', process.env.UICD_ENV);
var dbFile = (process.env.UICD_ENV === 'staging') ? "/tmp/db.json" : ".data/db.json";
const adapter = new FileSync(dbFile);
const db = low(adapter);
// Set some defaults
// Note: "ibp" is for "Incident Broadcast Pair". Happy to change it to something else. :)
db.defaults({ ibp: [] })
.write();
// http://expressjs.com/en/starter/static-files.html
app.use(express.static('public'));
// http://expressjs.com/en/starter/basic-routing.html
app.get("/", function(request, response) {
response.sendFile(__dirname + '/views/index.html');
});
// could also use the POST body instead of query string: http://expressjs.com/en/api.html#req.body
app.post("/broadcasts", function(request, response) {
if (Object.keys(request.query).length === 0) {
throw "Request is missing query parameters!"
}
var broadcast = new Broadcast();
broadcast.create(request.query.site, request.query.message)
.then(function(broadcastId) {
db.get('ibp')
.push({ incidentId: request.query.incidentId, broadcastId: broadcastId })
.write();
});
response.sendStatus(200);
});
app.get("/incidents", function(request, response) {
console.log("In /incidents");
var StatusPageAPI = require('statuspage-api');
var statuspage = new StatusPageAPI({
pageid: process.env.STATUSPAGE_PAGE_ID,
apikey: process.env.STATUSPAGE_API_KEY,
debuglevel: "warn" // Set debug levele: debug, info, warn, error
});
function isIncidentOpen(status) {
return (status !== 'resolved');
// return ((status !== 'resolved') && (status !== 'completed') && (status !== 'postmortem'));
}
var printIncidentTitle = function(result) {
if (result.error != null) {
console.log("Error: ", result.error);
}
var incidents = [];
if (result.status == "success") {
for (var i = 0; i < result.data.length; i++) {
if (isIncidentOpen(result.data[i].status)) {
incidents.push({ id: result.data[i].id, name: result.data[i].name, created_at: result.data[i].created_at, updated_at: result.data[i].updated_at });
}
}
}
response.json(incidents);
}
statuspage.get("incidents", printIncidentTitle);
});
app.get("/add", function(request, response) {
response.sendFile(__dirname + '/views/add.html');
})
app.get("/new_broadcast", function(request, response) {
// ?broadcast_message=two&site=three.com
var q = request.query;
var message = q.message;
var site = q.site;
response.send([message, site]);
})
// listen for requests :)
var listener = app.listen(process.env.PORT, function() {
console.log('Your app is listening on port ' + listener.address().port);
});