-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
139 lines (113 loc) · 3.03 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
var express = require('express');
var redis = require('redis');
var path = require('path');
var url = require('url');
var COLORS = ['red', 'yellow', 'green'];
var app = express();
var db;
app.set('secret', process.env.CI_SECRET);
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.static(path.join(__dirname, 'public')));
if (app.get('env') === 'production') {
app.use(express.errorHandler());
var redisUrl = url.parse(process.env.REDISTOGO_URL);
db = redis.createClient(redisUrl.port, redisUrl.hostname);
db.auth(redisUrl.auth.split(":")[1]);
} else {
db = redis.createClient();
}
function authorizeWebhook(req, res) {
if (app.get('secret') !== req.params.secret) {
res.send(401);
return false;
}
return true;
}
function authorizeUser(req, res) {
if (getLightMode() !== 'public') {
res.send(401);
return false;
}
return true;
}
function setColor(color, mode) {
db.set('trafficlight:' + color, mode);
}
function getColors(callback) {
var mode = getLightMode();
var data = { mode: getLightMode() };
if (mode === 'public') {
getPublicColors(callback, data);
} else if (mode === 'ci') {
getCiColors(callback, data);
}
}
function getPublicColors(callback, data) {
var arr = COLORS.map(function (color) { return 'trafficlight:' + color; });
db.mget(arr, function (err, states) {
COLORS.forEach(function (color, index) {
data[color] = states[index] === 'true';
});
callback(null, data);
});
}
// parse build status based on
// https://documentation.codeship.com/basic/getting-started/webhooks/
function getCiColors(callback, data) {
db.get('trafficlight:ci', function (err, buildStatus) {
COLORS.forEach(function (color) { data[color] = false; });
switch (buildStatus) {
case 'error':
case 'stopped':
case 'ignored':
case 'blocked':
case 'infrastructure_failure':
data.red = true;
break;
case 'testing':
case 'waiting':
data.yellow = true;
break;
case 'success':
data.green = true;
break;
}
callback(null, data);
});
}
function getLightMode() {
var mode = process.env.LIGHT_MODE;
if (mode !== 'public' && mode !== 'ci') throw('Unknown light mode!');
return mode;
}
app.get('/', function (req, res) {
res.render('index');
});
app.get('/lights', function (req, res) {
getColors(function (err, colors) {
res.send(colors);
});
});
app.post('/lights', function (req, res) {
if (!authorizeUser(req, res)) return;
COLORS.forEach(function(color) {
if (req.body.hasOwnProperty(color)) {
setColor(color, req.body[color]);
}
});
getColors(function (err, colors) {
res.send(colors);
});
});
app.post('/ci/:secret', function (req, res) {
if (!authorizeWebhook(req, res)) return;
var status = req.body.build.status;
db.set('trafficlight:ci', status);
res.send(201);
});
app.listen(app.get('port'));