From fad9ffa087203633ab9895908bcdebd19f8f244a Mon Sep 17 00:00:00 2001 From: Marc Worrell Date: Mon, 11 Jan 2021 14:49:44 +0100 Subject: [PATCH] Add on_handled callback. Triggered after processing, but before sending the response. --- README.md | 6 ++++++ src/cowmachine.erl | 6 +++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 4cfe3b0..1de70b6 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,12 @@ execute(Req, Env) -> fun(Ctx) -> % Perform anything after well-formedness check of your request % Examples are parsing the query args, or authentication + Ctx + end, + on_handled => + fun(Ctx) -> + % Perform anything after processing, before sending the result. + Ctx end }, % Handle the request, returns updated Req and Env for next Cowboy middleware diff --git a/src/cowmachine.erl b/src/cowmachine.erl index 9e44fc4..062d2c8 100644 --- a/src/cowmachine.erl +++ b/src/cowmachine.erl @@ -73,7 +73,11 @@ request_1(Controller, Req, Env, Options, Context) -> Context1 = cowmachine_req:set_env(EnvInit, Context), case cowmachine_decision_core:handle_request(State, Context1) of {_Finish, _StateResult, ContextResult} -> - cowmachine_response:send_response(ContextResult); + ContextResult1 = case maps:get(on_handled, Options, undefined) of + undefined -> ContextResult; + Fun when is_function(Fun) -> Fun(ContextResult) + end, + cowmachine_response:send_response(ContextResult1); {upgrade, UpgradeFun, _StateResult, ContextResult} -> {upgrade, UpgradeFun, _StateResult, ContextResult} end