Navigation Menu

Skip to content

Commit

Permalink
groonga: Accept both POST and GET requests for any command
Browse files Browse the repository at this point in the history
  • Loading branch information
piroor committed Apr 28, 2014
1 parent 4fa6e6a commit 07f09fe
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 24 deletions.
44 changes: 26 additions & 18 deletions lib/adapter/api/groonga/index.js
@@ -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
})
};
8 changes: 2 additions & 6 deletions test/adapter/http.test.js
Expand Up @@ -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',
Expand Down

0 comments on commit 07f09fe

Please sign in to comment.