Automated server.

This commit is contained in:
Thaddee Tyl
2014-01-03 17:08:19 +01:00
parent 9bdbe595b9
commit 08172c504b
2 changed files with 34 additions and 1 deletions
+2 -1
View File
@@ -12,6 +12,7 @@
"dot": ">=1.0.2",
"svgo": "~0.4.2",
"canvas": "~1.1.2",
"es6-promise": "~0.1.1"
"es6-promise": "~0.1.1",
"camp": "~13.11.9"
}
}
+32
View File
@@ -0,0 +1,32 @@
var camp = require('camp').start({ port: +process.argv[2]||80 });
var badge = require('./badge.js');
// Escapes `t` using the format specified in
// <https://github.com/espadrine/gh-badges/issues/12#issuecomment-31518129>
function escapeFormat(t) {
return t.replace('_', ' ').replace('__', '_').replace('--', '-');
}
camp.route(/^\/(([^-]|--)+)-(([^-]|--)+)-(([^-]|--)+).svg$/,
function(data, match, end, ask) {
var subject = escapeFormat(match[1]);
var status = escapeFormat(match[3]);
var color = escapeFormat(match[5]);
ask.res.setHeader('Content-Type', 'image/svg+xml');
try {
badge({text: [subject, status], colorscheme: color}, function(res) {
end(null, {template: streamFromString(res)});
});
} catch(e) {
badge({text: ["error", "bad badge"], colorscheme: "red"}, function(res) {
end(null, {template: streamFromString(res)});
});
}
});
var stream = require('stream');
function streamFromString(str) {
var newStream = new stream.Readable();
newStream._read = function() { newStream.push(str); newStream.push(null); };
return newStream;
}