Permalink
Browse files

Initial commit

  • Loading branch information...
0 parents commit 0bda19a7b6aea0bfec470f146f6572c78878ac37 @tj tj committed Sep 16, 2012
Showing with 166 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +18 −0 Readme.md
  3. +91 −0 app.js
  4. +46 −0 fetch.js
  5. +10 −0 package.json
@@ -0,0 +1 @@
+node_modules
@@ -0,0 +1,18 @@
+
+# Component Search
+
+ The component search application, powered by Express and Redis.
+
+## API
+
+### GET /search/:query
+
+ Search for `query`:
+
+ - `500` shit exploded
+ - `404` no packages, responds with json array `[]`
+ - `200` packages found, responds json array of component.json contents
+
+# License
+
+ MIT
91 app.js
@@ -0,0 +1,91 @@
+
+/**
+ * Module dependencies.
+ */
+
+var express = require('express')
+ , redis = require('redis')
+ , db = redis.createClient()
+ , app = express();
+
+// middleware
+
+app.use(express.logger());
+app.use(express.responseTime());
+
+/**
+ * Parse words.
+ */
+
+function words(str) {
+ return str.match(/\w+/);
+}
+
+/**
+ * Word keys from `words`.
+ */
+
+function wordKeys(words) {
+ return words.map(function(str){
+ return 'word:' + str;
+ });
+}
+
+/**
+ * Component keys from `names`.
+ */
+
+function componentKeys(names) {
+ return names.map(function(str){
+ return 'component:' + str;
+ });
+}
+
+/**
+ * Parse pkg json strings.
+ */
+
+function parse(pkgs) {
+ return pkgs.map(JSON.parse);
+}
+
+/**
+ * Respond with packages.
+ */
+
+function reply(res) {
+ return function reply(err, keys) {
+ if (err) return res.send(500);
+ if (!keys.length) return res.send(404, []);
+
+ db.mget(componentKeys(keys), function(err, pkgs){
+ if (err) return res.send(500);
+ res.send(parse(pkgs));
+ });
+ }
+}
+
+/**
+ * GET search :query.
+ */
+
+app.get('/search/:query', function(req, res){
+ var query = words(req.params.query);
+
+ // query stats
+ db.incr('stats:queries');
+
+ // word stats
+ query.forEach(function(word){
+ db.incr('stats:word:' + word);
+ })
+
+ // perform search
+ query = wordKeys(query);
+ db.sunion(query, reply(res));
+});
+
+// boot
+
+app.listen(3000);
+console.log('app listening on port 3000');
@@ -0,0 +1,46 @@
+
+/**
+ * Module dependencies.
+ */
+
+var wiki = require('component-wiki')
+ , redis = require('redis')
+ , db = redis.createClient();
+
+var pending = 0;
+
+db.flushdb();
+
+wiki(function(err, pkgs){
+ if (err) throw err;
+ pkgs.forEach(function(pkg){
+ if (!pkg) return;
+ console.log();
+ console.log('%s:', pkg.name);
+ var words = [pkg.name];
+ words = words.concat(parse(pkg.description));
+ words = words.concat(pkg.keywords || []);
+
+ ++pending;
+ db.set('component:' + pkg.repo, JSON.stringify(pkg), done);
+
+ ++pending;
+ db.sadd('components', pkg.repo, done);
+
+ words.forEach(function(word){
+ console.log(' %s', word);
+ ++pending;
+ db.sadd('word:' + word, pkg.repo, done);
+ });
+ });
+})
+
+function parse(str) {
+ return str.match(/\w+/).map(function(word){
+ return word.toLowerCase();
+ });
+}
+
+function done() {
+ --pending || process.exit();
+}
@@ -0,0 +1,10 @@
+{
+ "name": "component-search",
+ "description": "Component search server",
+ "version": "0.0.1",
+ "dependencies": {
+ "express": "3.x",
+ "redis": "0.8.1",
+ "component-wiki": "0.0.1"
+ }
+}

0 comments on commit 0bda19a

Please sign in to comment.