-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
222 lines (171 loc) · 6.59 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
var ws = require('ws');
var https = require('https');
var http = require('http');
var express = require('express');
var fs = require('fs');
var path = require('path');
var _ = require('lodash');
var Q = require('q');
var haringGocdMapper = require('./server/haring/gocdMapper');
var miroGocdMapper = require('./server/miro/gocdMapper');
var miroGocdMapperConstellation = require('./server/miro/gocdMapperConstellation');
var configReader = require('./server/ymlHerokuConfig');
var gocdCreator = require('./server/gocdReader');
var environmentReader = require('./server/environmentReader');
function monartServer() {
var WebSocketServer = ws.Server
, app = express();
var UPDATE_INTERVAL = 10000;
var USES_SSL = false;
var port = process.env.PORT || 5000;
var config = configReader.create('gocd').get();
function createServer() {
var rootDir = path.resolve(path.dirname(module.uri || "."));
app.use(express.static(rootDir + '/app/'));
try {
var credentials = {
key: fs.readFileSync('monart-key.pem'),
cert: fs.readFileSync('monart-cert.pem')
};
USES_SSL = true;
return https.createServer(credentials, app);
} catch(couldNotReadKeyAndCert) {
console.log("WARNING - could not use SSL, provide monart-key.pem and monart-cert.pem");
return http.createServer(app);
}
}
var server = createServer();
var CACHE_INITIALISED = false;
var gocd;
gocdCreator(config).then(function(instance) {
console.log("GO CD DATA CACHE INITIALISED");
console.log((USES_SSL ? 'https' : 'http') + ' server listening on %d', port);
CACHE_INITIALISED = true;
gocd = instance;
function createListener(identifier, dataTransformer) {
return function() {
var wss = new WebSocketServer({server: server, path: '/' + identifier });
console.log(identifier +' websocket server created');
wss.on('connection', function(ws) {
console.log('connecting to /' + ws.upgradeReq.url);
function newClient() {
function getPipelineParameter() {
var requestedUrl = ws.upgradeReq.url;
var match = requestedUrl.match(/pipeline=([^&]+)/);
return match ? match[1] : undefined;
}
function getColumnsParameter() {
var requestedUrl = ws.upgradeReq.url;
var match = requestedUrl.match(/columns=([^&]+)/);
return match ? match[1] : undefined;
}
var pipelineParameter = getPipelineParameter();
var pipelines = pipelineParameter ? [pipelineParameter] : gocd.pipelineNames;
function getActivityAndUpdateClients() {
var result = {};
if (CACHE_INITIALISED !== true) {
result[identifier] = {warmingUp: true};
ws.send(JSON.stringify(result));
} else {
var all = _.map(pipelines, function(pipeline) {
return gocd.readData(pipeline);
});
Q.all(all).then(function (gocdData) {
if(pipelineParameter) {
var visualisationData = dataTransformer(gocdData[0], getColumnsParameter());
result[identifier] = visualisationData;
ws.send(JSON.stringify(result), function () {});
} else {
result[identifier] = _.map(gocdData, function(data) {
var transformedPipelineData = dataTransformer(data, getColumnsParameter());
transformedPipelineData.pipeline = data.pipeline;
return transformedPipelineData;
});
ws.send(JSON.stringify(result), function () {});
}
}).fail(function(error) {
console.log("COULD NOT READ DATA!", error);
ws.send(JSON.stringify({error: error}));
});
}
}
getActivityAndUpdateClients();
var clientId = setInterval(getActivityAndUpdateClients, UPDATE_INTERVAL);
return clientId;
}
var clientId = newClient();
console.log('websocket connection open on /' + identifier);
ws.on('message', function(msg) {
if(msg === 'ping') {
console.log('PING');
ws.send(JSON.stringify({ping: 'success'}));
}
});
ws.on('close', function() {
console.log('websocket connection close on /' + identifier);
clearInterval(clientId);
});
});
}
}
/** HARING ************************/
var listenToHaring = createListener('haring', haringGocdMapper.readHistoryAndActivity);
listenToHaring();
/** MIRO BLUE ************************/
var listenToMiro = createListener('miro', miroGocdMapperConstellation.readHistoryAndActivity);
listenToMiro();
var listenToMiroBlue = createListener('miroBlue', miroGocdMapper.readHistoryAndActivity);
listenToMiroBlue();
}).done();
/** ENDPOINTS ************************/
function readAndRespondWithPromisedData(promise, res) {
if(CACHE_INITIALISED) {
promise.then(function (data) {
respondWithJson(res, data);
}).done();
} else {
res.send('warming up');
}
}
function respondWithJson(response, data) {
response.set({
'Content-Type': 'application/json'
});
response.send(JSON.stringify(data));
}
function readDataBasedOnPipeline(req, res, processor) {
if(!req.query.pipeline) {
res.send('ERROR - Please provide pipeline');
} else {
return readAndRespondWithPromisedData(gocd.readData(req.query.pipeline).then(function(data) {
return processor ? processor(data) : data;
}), res);
}
}
app.get('/alive',
function(req, res) {
console.log('life sign');
res.send('OK');
});
app.get('/data/gocd', function(req, res) {
readDataBasedOnPipeline(req, res);
});
app.get('/data/gocd/haring', function(req, res) {
readDataBasedOnPipeline(req, res, function(data) {
return haringGocdMapper.readHistoryAndActivity(data);
});
});
app.get('/data/gocd/miro', function(req, res) {
readDataBasedOnPipeline(req, res, function(data) {
return miroGocdMapperConstellation.readHistoryAndActivity(data);
});
});
app.get('/data/gocd/miroBlue', function(req, res) {
readDataBasedOnPipeline(req, res, function(data) {
return miroGocdMapper.readHistoryAndActivity(data);
});
});
environmentReader.extendServer(app);
server.listen(port);
}
monartServer();