├── Procfile ├── .gitignore ├── README.md ├── package.json └── index.js /Procfile: -------------------------------------------------------------------------------- 1 | web: node index.js 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | tmp/ 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Shield Server 2 | This is a node.js web application that creates shields ( https://github.com/olivierlacan/shields ) to solve this issue: https://github.com/olivierlacan/shields/issues/15?source=c . 3 | 4 | It uses the shielded library ( https://github.com/cainus/shielded ). 5 | 6 | It runs on heroku out-of-the-box, but can easily run elsewhere. 7 | 8 | See also the golang version by @jbowes : https://github.com/jbowes/buckler . It probably vastly outperforms this library. 9 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "shield-server", 3 | "version": "0.0.0", 4 | "description": "http service for creating github shield PNGs ala https://github.com/olivierlacan/shields/", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git://github.com/cainus/shield-server.git" 12 | }, 13 | "keywords": [ 14 | "shields", 15 | "github", 16 | "badges" 17 | ], 18 | "author": "Gregg Caines", 19 | "license": "BSD", 20 | "dependencies": { 21 | "shielded": "0.2.0", 22 | "connect": "2.7.11", 23 | "urlgrey-connect": "~0.1.0", 24 | "log-driver": "~1.2.1" 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var http = require('http'); 2 | var connect = require('connect'); 3 | var fs = require('fs'); 4 | var shielded = require('shielded'); 5 | var urlgreyConnect = require('urlgrey-connect'); 6 | 7 | var getUniqueId = function(vendorText, statusText, color, scale){ 8 | return new Buffer(JSON.stringify({vendor:vendorText, 9 | status:statusText, 10 | color:color, 11 | scale:scale})) 12 | .toString('base64'); 13 | }; 14 | 15 | var tempdir = __dirname + '/tmp'; 16 | fs.mkdir(tempdir, function(err){ 17 | if (err && !err.message.match(/^EEXIST/)){ 18 | // don't care if the dir already exists. 19 | throw err; 20 | } 21 | }); 22 | 23 | 24 | var app = connect(); 25 | app.use(urlgreyConnect()); 26 | app.use(function(req, res) { 27 | 28 | var options = { 29 | vendorText : req.uri.query().label, 30 | statusText : req.uri.query().value, 31 | color : (req.uri.query().color || 'lightgray'), 32 | scale : (req.uri.query().scale || 1) 33 | }; 34 | 35 | options.filename = tempdir + '/' + getUniqueId(options.vendorText, 36 | options.statusText, 37 | options.color, 38 | options.scale) + '.png'; 39 | 40 | shielded(options, function(err){ 41 | if (!err){ 42 | res.writeHead(200, {'Content-Type': 'image/png'}); 43 | var png = fs.createReadStream(options.filename); 44 | png.pipe(res); 45 | } else { 46 | res.writeHead(400, {'Content-Type': 'text/plain'}); 47 | res.end(err.toString()); 48 | } 49 | }); 50 | }); 51 | var port = process.env.PORT || 3000; 52 | http.createServer(app).listen(port); 53 | 54 | console.log('Server running at http://127.0.0.1:' + port + '/'); 55 | 56 | --------------------------------------------------------------------------------