Skip to content
This repository has been archived by the owner on Sep 29, 2019. It is now read-only.

Commit

Permalink
Image search microservice
Browse files Browse the repository at this point in the history
  • Loading branch information
linuxenko committed Mar 29, 2016
0 parents commit 623d582
Show file tree
Hide file tree
Showing 7 changed files with 169 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
.env
1 change: 1 addition & 0 deletions Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
web: node index.js
26 changes: 26 additions & 0 deletions database.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
var mongo = require('mongodb').MongoClient;

module.exports = function Database(callback) {
var self = this;

mongo.connect(process.env.MONGO_URI, function(err, db) {
if (err) return console.log(err);
self.collection = db.collection(process.env.MONGO_COLLECTION);
callback();
});

this.latestQuery = function(cb) {
self.collection.find().sort({ $natural : -1 }).limit(10).toArray(function(err, data) {
if (err) return cb(err);
cb(null, data);
});
}

this.insertQuery = function(query, cb) {
self.collection.insert({query : query, date: new Date()}, function(err) {
if (err) return cb(err);
cb();
});
};
return this;
}
53 changes: 53 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
var env = require('node-env-file');
try {
env(__dirname + '/.env');
} catch (e) { }

var imgur = require('./lib/imgur');
var express = require('express');
var async = require('async');
var app = express();
var db = null;

app.set('view engine', 'jade');

app.get('/', function(req,res) {
res.render('index', {host : req.protocol + '://' + req.get('host')});
});

app.get('/api/search/:search', function(req, res) {
var query = req.params.search;
var page = Number(req.query.offset) || 0;

async.parallel([
function(callback) {
imgur.query(query, page, function(err, data) {
if (err) return callback(err);
callback(data);
});
},
function(callback) {
db.insertQuery(query, callback);
}
],function() {
if (Array.isArray(arguments[0]) !== true) {
res.send({}).end();
} else {
res.send(arguments[0]).end();
}
});
});

app.get('/api/latest', function(req, res) {
db.latestQuery(function(err, data) {
if (err) return res.send({}).end();
res.send(data).end();
});
});


db = new require('./database')(function() {
app.listen(process.env.PORT, function() {
console.log('Application started on :' + process.env.PORT);
});
});
48 changes: 48 additions & 0 deletions lib/imgur.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
var imgurg = exports;
var request = require('request');

imgurg.cid = process.env.IMGUR_CLIENT_ID;

imgurg.query = function(query, page, callback){
var query = { c: String(query) }
var subcat = (query.c).match(/(?:^r\/)(.*)/);
query.e = subcat && encodeURIComponent((query.c).substring(2,(query.c).length)) || encodeURIComponent(query.c);

var options = {
encoding: 'utf8',
json: true,
uri : 'https://api.imgur.com/3/gallery/search/top/'+page+'?q='+query.e,
headers: {
'Authorization': 'Client-ID '+ imgurg.cid
},
method: 'GET'
}

function tError(stat, err, body){
callback('error');
return false
}

function isObjectEmpty(obj) {
for (var key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
return false;
}
}
return true;
}

request(options, function (err, res, body) {
if ( (err != "" && body !== undefined) || res.statusCode !== 200 ){
if(body.data === undefined || isObjectEmpty(body.data) ){ return tError(res.statusCode, err, body); }
callback(null, body.data.map(function(d) {
return {
title : d.title,
link : 'http://imgur.com/' + d.id,
image : d.link,
time : d.datetime
};
}));
}else{ return tError(res.statusCode, err, body); }
});
}
27 changes: 27 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "imagesearch-freecodecamp",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/b37t1td/imagesearch-freecodecamp.git"
},
"author": "Svetlana Linuxenko <[email protected]> (http://www.linuxenko.pro)",
"license": "MIT",
"bugs": {
"url": "https://github.com/b37t1td/imagesearch-freecodecamp/issues"
},
"homepage": "https://github.com/b37t1td/imagesearch-freecodecamp#readme",
"dependencies": {
"async": "^2.0.0-rc.2",
"express": "^4.13.4",
"jade": "^1.11.0",
"mongodb": "^2.1.13",
"node-env-file": "^0.1.8",
"request": "^2.69.0"
}
}
12 changes: 12 additions & 0 deletions views/index.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
html
head
title Image search microservice
body
h2 Image Search Abstraction Layer (freeCodeCamp)
hr
h3 Search for images
p #{host}/api/search/:query[?offset=0]
strong Example of request second page of "cats" query
p #{host}/api/search/cats?offset=1
h3 Latest queries stats (last 10 queries)
p #{host}/api/latest

0 comments on commit 623d582

Please sign in to comment.