[Groonga-commit] droonga/express-droonga at 07f09fe [master] groonga: Accept both POST and GET requests for any command

Back to archive index

YUKI Hiroshi null+****@clear*****
Mon Apr 28 12:54:15 JST 2014


YUKI Hiroshi	2014-04-28 12:54:15 +0900 (Mon, 28 Apr 2014)

  New Revision: 07f09fe5679b2785130466d6bfd9d9f900a25304
  https://github.com/droonga/express-droonga/commit/07f09fe5679b2785130466d6bfd9d9f900a25304

  Message:
    groonga: Accept both POST and GET requests for any command

  Modified files:
    lib/adapter/api/groonga/index.js
    test/adapter/http.test.js

  Modified: lib/adapter/api/groonga/index.js (+26 -18)
===================================================================
--- lib/adapter/api/groonga/index.js    2014-04-28 12:48:27 +0900 (9b8353c)
+++ lib/adapter/api/groonga/index.js    2014-04-28 12:54:15 +0900 (e1a1244)
@@ -1,32 +1,40 @@
 var command = require('../../command');
 var Loader = require('./loader');
 
-function handleHTTPRequest(request, connection) {
-  connection.emit(request.params.commandName, request.query);
+var SUFFIX_PATTERN = /\.([^\.]+)$/;
+
+function normalizeCommandName(commandName) {
+  if (SUFFIX_PATTERN.test(commandName)) {
+    commandName = commandName.replace(SUFFIX_PATTERN, '');
+    if (RegExp.$1 != 'json')
+      throw new Error('unsupported output type');
+  }
+  return commandName;
+}
+
+function handleGetRequest(request, connection) {
+  var commandName = normalizeCommandName(request.params.commandName);
+  connection.emit(commandName, request.query);
 }
 
-function handleLoadHTTPRequest(request, connection, response) {
-  var loader = new Loader(request, response, connection, this.logger);
-  loader.run();
+function handlePostRequest(request, connection, response) {
+  var commandName = normalizeCommandName(request.params.commandName);
+  if (commandName == 'load') {
+    var loader = new Loader(request, response, connection, this.logger);
+    loader.run();
+  } else {
+    connection.emit(commandName, request.query);
+  }
 }
 
 module.exports = {
   'groonga': new command.HTTPRequestResponse({
     path: '/d/:commandName',
-    onRequest: handleHTTPRequest
-  }),
-  'groonga-json': new command.HTTPRequestResponse({
-    path: '/d/:commandName.json',
-    onRequest: handleHTTPRequest
+    onRequest: handleGetRequest
   }),
-  'groonga-load': new command.HTTPRequestResponse({
-    method: 'POST',
-    path:   '/d/load',
-    onRequest: handleLoadHTTPRequest
-  }),
-  'groonga-load-json': new command.HTTPRequestResponse({
+  'groonga-post': new command.HTTPRequestResponse({
+    path: '/d/:commandName',
     method: 'POST',
-    path:   '/d/load.json',
-    onRequest: handleLoadHTTPRequest
+    onRequest: handlePostRequest
   })
 };

  Modified: test/adapter/http.test.js (+2 -6)
===================================================================
--- test/adapter/http.test.js    2014-04-28 12:48:27 +0900 (5322adf)
+++ test/adapter/http.test.js    2014-04-28 12:54:15 +0900 (b71bd8f)
@@ -37,12 +37,8 @@ suite('HTTP Adapter', function() {
                         definition: restAPI.search },
                       { name:       'groonga',
                         definition: groongaAPI.groonga },
-                      { name:       'groonga-json',
-                        definition: groongaAPI['groonga-json'] },
-                      { name:       'groonga-load',
-                        definition: groongaAPI['groonga-load'] },
-                      { name:       'groonga-load-json',
-                        definition: groongaAPI['groonga-load-json'] },
+                      { name:       'groonga-post',
+                        definition: groongaAPI['groonga-post'] },
                       { name:       'droonga',
                         definition: droongaAPI.droonga },
                       { name:       'droonga-streaming:watch',
-------------- next part --------------
HTML����������������������������...
Download 



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