Skip to content

Commit

Permalink
Add on_handled callback. Triggered after processing, but before sendi…
Browse files Browse the repository at this point in the history
…ng the response.
  • Loading branch information
mworrell committed Jan 11, 2021
1 parent 1bf84df commit fad9ffa
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion src/cowmachine.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit fad9ffa

Please sign in to comment.