[Groonga-commit] groonga/express-kotoumi [master] Add skelton for REST API

Back to archive index

YUKI Hiroshi null+****@clear*****
Fri Jan 11 18:43:08 JST 2013


YUKI Hiroshi	2013-01-11 18:43:08 +0900 (Fri, 11 Jan 2013)

  New Revision: c73ba21a1973ec7d65e89bf8a20d685f94a4ec67
  https://github.com/groonga/express-kotoumi/commit/c73ba21a1973ec7d65e89bf8a20d685f94a4ec67

  Log:
    Add skelton for REST API

  Added files:
    lib/builders/index.js
    lib/builders/response.js
    lib/builders/rest-request.js
    lib/index.js

  Added: lib/builders/index.js (+2 -0) 100644
===================================================================
--- /dev/null
+++ lib/builders/index.js    2013-01-11 18:43:08 +0900 (b4550e9)
@@ -0,0 +1,2 @@
+exports.requestInREST = require('rest-request');
+exports.response = require('response');

  Added: lib/builders/response.js (+3 -0) 100644
===================================================================
--- /dev/null
+++ lib/builders/response.js    2013-01-11 18:43:08 +0900 (e9ba82c)
@@ -0,0 +1,3 @@
+function searchResponseBuilder(request) {
+}
+exports.search = searchResponseBuilder;

  Added: lib/builders/rest-request.js (+3 -0) 100644
===================================================================
--- /dev/null
+++ lib/builders/rest-request.js    2013-01-11 18:43:08 +0900 (71979c7)
@@ -0,0 +1,3 @@
+function searchRequestBuilder(request) {
+}
+exports.search = searchRequestBuilder;

  Added: lib/index.js (+46 -0) 100644
===================================================================
--- /dev/null
+++ lib/index.js    2013-01-11 18:43:08 +0900 (9191a0e)
@@ -0,0 +1,46 @@
+var express = require('express');
+var Connection = require('../backend-adaptor').Connection;
+var builders = request('./builders');
+
+express.application.kotoumi = function(params) {
+  params = params || {};
+  var connection = new Connection(params);
+
+  var prefix = params.prefix || '';
+  prefix = prefix.replace(/\/$/, '');
+
+  var handlersFactory = params.handlers;
+
+  function createDefaultHandler(type, connection) {
+    return (function(request, response) {
+      var builder = builders.requestInREST[type];
+      if (!builder) throw new Error('no request builder for ' + type);
+
+      var message = builder(request);
+      connection.sendMessage(type, message, function(responseMessage) {
+        var builder = builders.response[type];
+        if (!builder) throw new Error('no response builder for ' + type);
+
+        response.write(builder(responseMessage));
+      });
+    });
+  }
+
+  function createHandler(type) {,
+    return handlersFactory && handlersFactory[type](connection) ||
+             createDefaultHandler(type, connection);
+  }
+
+  this.get(prefix + '/status/:target', createHandler('status'));
+
+  this.get(prefix + '/tables/:tableName', createHandler('search'));
+
+  this.put(prefix + '/tables/:tableName', createHandler('createTable'));
+  this.del(prefix + '/tables/:tableName', createHandler('removeTable'));
+
+  this.put(prefix + '/tables/:tableName/columns/:columnName', createHandler('createColumn'));
+  this.del(prefix + '/tables/:tableName/columns/:columnName', createHandler('removeColumn'));
+
+  this.put(prefix + '/tables/:tableName/records/:key', createHandler('loadRecord'));
+  this.post(prefix + '/tables/:tableName/records', createHandler('loadRecords'));
+}
-------------- next part --------------
HTML����������������������������...
Descargar 



More information about the Groonga-commit mailing list
Back to archive index