Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,13 @@ login(post, _Params) ->
{200, #{<<"set-cookie">> => Cookie}, #{}}.
```

## Add log meta

Call `minirest_handler:update_log_meta/1` from a handler or an
`authorization` callback to add meta for the `log` hook. The call does
nothing in a process that does not handle a minirest request, for example
when a test calls the callback directly.

## Start your HTTP server

```erlang
Expand Down
12 changes: 10 additions & 2 deletions src/minirest_handler.erl
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,17 @@ init(Request0, State) ->
%% or it may be an interactive endpoint whose metadata cannot be determined in one step.
%% Here, the metadata is moved from the function return to the process dictionary,
%% so that the metadata in the endpoint can be updated.
%%
%% The call does nothing when the calling process does not handle a minirest
%% request, for example when a test calls an authorize callback directly.
update_log_meta(New) ->
Meta = get_log_meta(),
erlang:put(?META_KEY, maps:merge(Meta, New)).
case get_log_meta() of
undefined ->
ok;
Meta ->
erlang:put(?META_KEY, maps:merge(Meta, New)),
ok
end.

%%%==============================================================================================
%% internal
Expand Down
9 changes: 8 additions & 1 deletion test/minirest_handler_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ all() ->
t_route_path_in_auth,
t_post_large_body,
t_set_cookie,
t_set_cookies
t_set_cookies,
t_update_log_meta_outside_request
].

init_per_suite(Config) ->
Expand Down Expand Up @@ -157,6 +158,12 @@ t_set_cookies(_Config) ->
lists:sort([cookie_pair(C) || C <- set_cookie_headers(Headers)])
).

%% `update_log_meta/1' does nothing in a process that does not handle a
%% minirest request.
t_update_log_meta_outside_request(_Config) ->
?assertEqual(ok, minirest_handler:update_log_meta(#{source => <<"nobody">>})),
?assertEqual(undefined, erlang:get({minirest_handler, meta})).

%%--------------------------------------------------------------------
%% Helpers
%%--------------------------------------------------------------------
Expand Down
Loading