Skip to content

Commit

Permalink
Add the Site Suggest server node.js code.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mardak committed Nov 2, 2012
1 parent 75dcae6 commit 35bacad
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions siteSuggest/app.js
@@ -0,0 +1,46 @@
const BASE_DOMAIN = "https://sitesuggest.mozillalabs.com/";
const CATEGORIES_FILE = "top_categories_domains_filtered_thumbs.json";

// Read in the top categories data
var topCategories, knownCategories;
require("fs").readFile(__dirname + "/" + CATEGORIES_FILE, "utf8", function(err, json) {
topCategories = JSON.parse(json);
knownCategories = Object.keys(topCategories);
});

// Handle requests for site suggestion for a category
require("http").createServer(function(request, response) {
function respond(statusCode, dataObject) {
response.writeHead(statusCode, {
"Content-Type": "application/json",
"Vary": "Category"
});
response.end(JSON.stringify(dataObject));
}

// Treat all other requests as a suggestion, so bail without a category
var category = request.headers.category;
if (category == null) {
respond(400, null);
return;
}

// Bail if the category is unknown
category = category.toLowerCase();
if (knownCategories.indexOf(category) == -1) {
respond(404, null);
return;
}

// Pick one site to recommend
var choices = topCategories[category];
var choice = choices[Math.floor(Math.random() * choices.length)];

// Package up a response for the site suggestion
respond(200, {
category: category,
image: BASE_DOMAIN + "images/" + choice.thumb_filename,
title: choice.title,
url: choice.url
});
}).listen(8080);

0 comments on commit 35bacad

Please sign in to comment.