-
Notifications
You must be signed in to change notification settings - Fork 95
/
server.js
55 lines (51 loc) · 1.8 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
var connect = require('connect');
var requestCl = require("request");
var serveStatic = require('serve-static');
var fs = require('fs');
var url = require('url');
var port = Number(process.env.PORT || 9000);
var server = connect()
server.use(serveStatic(__dirname + '/public'));
server.use(function(req, res, next) {
var app = url.parse(req.url, true).query.app;
var root = url.parse(req.url, true).query.root || '';
var style = url.parse(req.url, true).query.style || '';
var svg = url.parse(req.url, true).query.svg || 0;
var fileExtension = 'png';
if (svg == 1) {
fileExtension = 'svg';
}
if (style.length > 0) {
if (style == 'flat') style = '-' +style;
else style = '';
}
requestCl.get({
url: "https://" + app + ".herokuapp.com/" + root
}, function(error, response, body) {
if (error || [200,401].indexOf(response.statusCode) < 0) {
var errorMessage = error || response.statusCode;
console.log('server error image for ' + app + ' error ' + errorMessage);
var fileStream = fs.createReadStream('public/images/heroku-failed' + style + '.' + fileExtension);
if (fileExtension == 'svg') {
fileExtension = 'svg+xml';
}
res.writeHead(200, {
'Content-Type': 'image/'+fileExtension,
"Cache-Control": "no-cache, no-store, must-revalidate"
});
fileStream.pipe(res);
} else {
console.log('server success image for ' + app);
var fileStream = fs.createReadStream('public/images/heroku-badge' + style + '.' + fileExtension);
if (fileExtension == 'svg') {
fileExtension = 'svg+xml';
}
res.writeHead(200, {
'Content-Type': 'image/'+fileExtension,
"Cache-Control": "no-cache, no-store, must-revalidate"
});
fileStream.pipe(res);
}
});
});
server.listen(port);