summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorILYA Khlopotov <iilyak@apache.org>2020-04-07 09:46:30 -0700
committerILYA Khlopotov <iilyak@apache.org>2020-04-09 02:52:20 -0700
commita14f62d3f0bbb16f57d692d63028579f96affc5e (patch)
tree0aa51a827d8965c834dfb91d7488c9875c38d66d
parentd6ec9935453c4f0fe26174a472cdf3e4cb9c5e60 (diff)
downloadcouchdb-a14f62d3f0bbb16f57d692d63028579f96affc5e.tar.gz
Add mango_plugin
Implement the following extention points: - `before_find(Req) -> {ok, Req}` - `after_find(Req, HttpResp, KVs) -> {ok, KVs}`
-rw-r--r--src/mango/src/mango_epi.erl4
-rw-r--r--src/mango/src/mango_httpd.erl20
-rw-r--r--src/mango/src/mango_plugin.erl43
3 files changed, 57 insertions, 10 deletions
diff --git a/src/mango/src/mango_epi.erl b/src/mango/src/mango_epi.erl
index 1fcd05b7f..d593d6371 100644
--- a/src/mango/src/mango_epi.erl
+++ b/src/mango/src/mango_epi.erl
@@ -33,7 +33,9 @@ providers() ->
].
services() ->
- [].
+ [
+ {mango, mango_plugin}
+ ].
data_subscriptions() ->
[].
diff --git a/src/mango/src/mango_httpd.erl b/src/mango/src/mango_httpd.erl
index 94aa866d2..8d5a2123d 100644
--- a/src/mango/src/mango_httpd.erl
+++ b/src/mango/src/mango_httpd.erl
@@ -187,17 +187,18 @@ handle_explain_req(Req, _Db) ->
chttpd:send_method_not_allowed(Req, "POST").
-handle_find_req(#httpd{method='POST'}=Req, Db) ->
- chttpd:validate_ctype(Req, "application/json"),
- Body = chttpd:json_body_obj(Req),
+handle_find_req(#httpd{method='POST'}=Req0, Db) ->
+ {ok, Req1} = mango_plugin:before_find(Req0),
+ chttpd:validate_ctype(Req1, "application/json"),
+ Body = chttpd:json_body_obj(Req1),
{ok, Opts0} = mango_opts:validate_find(Body),
{value, {selector, Sel}, Opts} = lists:keytake(selector, 1, Opts0),
- {ok, Resp0} = start_find_resp(Req),
+ {ok, Resp0} = start_find_resp(Req1),
case run_find(Resp0, Db, Sel, Opts) of
{ok, AccOut} ->
- end_find_resp(AccOut);
+ end_find_resp(Req1, AccOut);
{error, Error} ->
- chttpd:send_error(Req, Error)
+ chttpd:send_error(Req1, Error)
end;
@@ -225,14 +226,15 @@ start_find_resp(Req) ->
chttpd:start_delayed_json_response(Req, 200, [], "{\"docs\":[").
-end_find_resp(Acc0) ->
- #vacc{resp=Resp00, buffer=Buf, kvs=KVs, threshold=Max} = Acc0,
+end_find_resp(Req, Acc0) ->
+ #vacc{resp=Resp00, buffer=Buf, kvs=KVs0, threshold=Max} = Acc0,
{ok, Resp0} = chttpd:close_delayed_json_object(Resp00, Buf, "\r\n]", Max),
+ {ok, KVs1} = mango_plugin:after_find(Req, Resp0, KVs0),
FinalAcc = lists:foldl(fun({K, V}, Acc) ->
JK = ?JSON_ENCODE(K),
JV = ?JSON_ENCODE(V),
[JV, ": ", JK, ",\r\n" | Acc]
- end, [], KVs),
+ end, [], KVs1),
Chunk = lists:reverse(FinalAcc, ["}\r\n"]),
{ok, Resp1} = chttpd:send_delayed_chunk(Resp0, Chunk),
chttpd:end_delayed_json_response(Resp1).
diff --git a/src/mango/src/mango_plugin.erl b/src/mango/src/mango_plugin.erl
new file mode 100644
index 000000000..296a35419
--- /dev/null
+++ b/src/mango/src/mango_plugin.erl
@@ -0,0 +1,43 @@
+% Licensed under the Apache License, Version 2.0 (the "License"); you may not
+% use this file except in compliance with the License. You may obtain a copy of
+% the License at
+%
+% http://www.apache.org/licenses/LICENSE-2.0
+%
+% Unless required by applicable law or agreed to in writing, software
+% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+% License for the specific language governing permissions and limitations under
+% the License.
+
+-module(mango_plugin).
+
+-export([
+ before_find/1,
+ after_find/3
+]).
+
+-define(SERVICE_ID, mango).
+
+%% ------------------------------------------------------------------
+%% API Function Definitions
+%% ------------------------------------------------------------------
+
+before_find(HttpReq0) ->
+ with_pipe(before_find, [HttpReq0]).
+
+
+after_find(HttpReq, HttpResp, Arg0) ->
+ with_pipe(after_find, [HttpReq, HttpResp, Arg0]).
+
+%% ------------------------------------------------------------------
+%% Internal Function Definitions
+%% ------------------------------------------------------------------
+
+with_pipe(Func, Args) ->
+ do_apply(Func, Args, [pipe]).
+
+
+do_apply(Func, Args, Opts) ->
+ Handle = couch_epi:get_handle(?SERVICE_ID),
+ couch_epi:apply(Handle, ?SERVICE_ID, Func, Args, Opts).