This repository has been archived by the owner on Nov 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
55 lines (46 loc) · 1.63 KB
/
index.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
var http = require('http');
var connect = require('connect');
var fs = require('fs');
var shielded = require('shielded');
var urlgreyConnect = require('urlgrey-connect');
var getUniqueId = function(vendorText, statusText, color, scale){
return new Buffer(JSON.stringify({vendor:vendorText,
status:statusText,
color:color,
scale:scale}))
.toString('base64');
};
var tempdir = __dirname + '/tmp';
fs.mkdir(tempdir, function(err){
if (err && !err.message.match(/^EEXIST/)){
// don't care if the dir already exists.
throw err;
}
});
var app = connect();
app.use(urlgreyConnect());
app.use(function(req, res) {
var options = {
vendorText : req.uri.query().label,
statusText : req.uri.query().value,
color : (req.uri.query().color || 'lightgray'),
scale : (req.uri.query().scale || 1)
};
options.filename = tempdir + '/' + getUniqueId(options.vendorText,
options.statusText,
options.color,
options.scale) + '.png';
shielded(options, function(err){
if (!err){
res.writeHead(200, {'Content-Type': 'image/png'});
var png = fs.createReadStream(options.filename);
png.pipe(res);
} else {
res.writeHead(400, {'Content-Type': 'text/plain'});
res.end(err.toString());
}
});
});
var port = process.env.PORT || 3000;
http.createServer(app).listen(port);
console.log('Server running at http://127.0.0.1:' + port + '/');