Last active
August 29, 2015 14:16
-
-
Save vicary/9946a689cf2ba7e72355 to your computer and use it in GitHub Desktop.
Simple kittydar server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var kittydar = require('kittydar') | |
, kittyUtil = reqiure('kittydar/utils') // I just guess it worked that way | |
, restify = require('restify') | |
, http = require('http') | |
; | |
var server = restify.createServer() | |
; | |
// Parse GET parameters "image=..." | |
server.use(restify.queryParser()); | |
server.get('/kittydar', function(req, res) { | |
if ( !req.query.image ) { | |
res.status(400).end(); | |
} | |
else { | |
getImageCanvas(req.query.image, function(error, canvas) { | |
if ( error ) { | |
console.error('Cannot download image.', error); | |
} | |
else { | |
var cats = kittydar.detectCats(canvas) | |
res.send(cats); | |
} | |
}); | |
} | |
}); | |
server.listen(8080, '127.0.0.1'); | |
function getImageCanvas(url, cb) { | |
var req = http.get(url, function(res) { | |
var fileContents = ''; | |
// HTTP status error | |
if ( res.statusCode != 200 ) { | |
cb({ code: res.statusCode, message: res.statusMessage }); | |
return; | |
} | |
res.on('data', function(data) { | |
fileContents+= data; | |
}); | |
res.on('end', function() { | |
var canvas = kittyUtil.dataToCanvas(fileContents); | |
// Success | |
cb(null, canvas); | |
}); | |
// HTTP response stream error | |
res.on('error', cb); | |
}); | |
// HTTP request error | |
req.on('error', cb); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment