Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
groonga: Accept both POST and GET requests for any command
- Loading branch information
Showing
2 changed files
with
28 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
| }) | ||
| }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters