From 57e90bf9cc71169bda08df6aee15b4b6c159f542 Mon Sep 17 00:00:00 2001 From: Tobias Lindahl Date: Mon, 4 May 2020 15:04:21 +0200 Subject: [PATCH 1/3] Add redbug to release --- rebar.config | 5 +++-- rebar.lock | 4 ++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/rebar.config b/rebar.config index ee02d575c..3c68f2103 100644 --- a/rebar.config +++ b/rebar.config @@ -35,7 +35,8 @@ {jose, {git, "https://github.com/NYNJA-MC/jose-erlang.git", {ref, "7094018"}}}, {uuid, {git, "https://github.com/avtobiff/erlang-uuid.git",{branch,"master"}}}, {enenra, {git, "https://github.com/nlfiedler/enenra", {tag, "0.3.0"}}}, - {'qdate', {git, "https://github.com/enterprizing/qdate.git", {ref,"fba988fc54214bb37a3ce11d5c5a3cc68752c3ce"}}} + {'qdate', {git, "https://github.com/enterprizing/qdate.git", {ref,"fba988fc54214bb37a3ce11d5c5a3cc68752c3ce"}}}, + {redbug, {git, "https://github.com/massemanet/redbug.git", {tag, "2.0.1"}}} ]}. @@ -87,7 +88,7 @@ ssl_verify_fun,locus,emqttd,hackney,roster,service,active, cowboy,emq_dashboard,emqttc,enenra,envy,uuid,erlydtl,forms, gen_smtp, jwt, migresia, mini_s3, nitro, opencensus, - qdate,rest,rfc3339,sh,stacktrace_compat]}, + qdate,rest,rfc3339,sh,stacktrace_compat, redbug]}, {sys_config, "./sys.config"}, {vm_args, "./vm.args"}, {dev_mode, true}, diff --git a/rebar.lock b/rebar.lock index dedcc49b0..c972e7c5d 100644 --- a/rebar.lock +++ b/rebar.lock @@ -171,6 +171,10 @@ {git,"https://github.com/ninenines/ranch", {ref,"3190aef88aea04d6dce8545fe9b4574288903f44"}}, 1}, + {<<"redbug">>, + {git,"https://github.com/massemanet/redbug.git", + {ref,"4cdfd2106432579502da535e390466fe9ca0d41f"}}, + 0}, {<<"rest">>, {git,"git://github.com/synrc/rest", {ref,"9a4e21d50a96b06649944c9a88ad595114db6c31"}}, -- GitLab From b2a44ce10917a059a3664013d5b6acb3877f75a0 Mon Sep 17 00:00:00 2001 From: Tobias Lindahl Date: Mon, 4 May 2020 15:05:20 +0200 Subject: [PATCH 2/3] Add content type to REST API logging --- apps/roster/src/rest/rest_cowboy_chat_handler.erl | 4 +++- apps/roster/src/rest/rest_cowboy_cri_handler.erl | 4 +++- apps/roster/src/rest/rest_cowboy_csv_handler.erl | 4 +++- apps/roster/src/rest/rest_cowboy_fake_numbers_handler.erl | 4 +++- apps/roster/src/rest/rest_cowboy_link_handler.erl | 4 +++- apps/roster/src/rest/rest_cowboy_metric_handler.erl | 4 +++- apps/roster/src/rest/rest_cowboy_publish_handler.erl | 4 +++- apps/roster/src/rest/rest_cowboy_push_handler.erl | 4 +++- apps/roster/src/rest/rest_cowboy_room_handler.erl | 4 +++- apps/roster/src/rest/rest_cowboy_session_handler.erl | 4 +++- apps/roster/src/rest/rest_cowboy_test_handler.erl | 4 +++- apps/roster/src/rest/rest_cowboy_transcribe_handler.erl | 4 +++- apps/roster/src/rest/rest_cowboy_users_handler.erl | 4 +++- apps/roster/src/rest/rest_cowboy_whitelist_handler.erl | 4 +++- 14 files changed, 42 insertions(+), 14 deletions(-) diff --git a/apps/roster/src/rest/rest_cowboy_chat_handler.erl b/apps/roster/src/rest/rest_cowboy_chat_handler.erl index 7f7e1d45a..c91e67e36 100644 --- a/apps/roster/src/rest/rest_cowboy_chat_handler.erl +++ b/apps/roster/src/rest/rest_cowboy_chat_handler.erl @@ -32,7 +32,9 @@ init(#{path := Path, method := Method, qs := QS} = Req, #{endpoint := Endpoint} = State) when Endpoint =:= chat; Endpoint =:= room_chat -> - ?LOG_INFO("~s:~s ~p", [Method, Path, QS]), + Headers = cowboy_req:headers(Req), + CT = maps:get(<<"content-type">>, Headers, <<"no-content-type">>), + ?LOG_INFO("~s:~s:~s ~p", [Method, Path, CT, QS]), {cowboy_rest, Req, State}. is_authorized(Req, State) -> diff --git a/apps/roster/src/rest/rest_cowboy_cri_handler.erl b/apps/roster/src/rest/rest_cowboy_cri_handler.erl index ef02dfaeb..3c06dc791 100644 --- a/apps/roster/src/rest/rest_cowboy_cri_handler.erl +++ b/apps/roster/src/rest/rest_cowboy_cri_handler.erl @@ -49,7 +49,9 @@ init(#{path := Path, method := Method, qs := QS} = Req, Endpoint =:= ?cri_room_members; Endpoint =:= ?cri_room_type; Endpoint =:= ?cri_bubble -> - ?LOG_INFO("~s:~s ~p", [Method, Path, QS]), + Headers = cowboy_req:headers(Req), + CT = maps:get(<<"content-type">>, Headers, <<"no-content-type">>), + ?LOG_INFO("~s:~s:~s ~p", [Method, Path, CT, QS]), {cowboy_rest, Req, State}. is_authorized(Req, State) -> diff --git a/apps/roster/src/rest/rest_cowboy_csv_handler.erl b/apps/roster/src/rest/rest_cowboy_csv_handler.erl index a21da1b59..6f261d005 100644 --- a/apps/roster/src/rest/rest_cowboy_csv_handler.erl +++ b/apps/roster/src/rest/rest_cowboy_csv_handler.erl @@ -31,7 +31,9 @@ init(#{path := Path, method := Method, qs := QS} = Req, #{endpoint := Endpoint} = State) when Endpoint =:= ?groups_csv; Endpoint =:= ?chats_csv -> - ?LOG_INFO("~s:~s ~p", [Method, Path, QS]), + Headers = cowboy_req:headers(Req), + CT = maps:get(<<"content-type">>, Headers, <<"no-content-type">>), + ?LOG_INFO("~s:~s:~s ~p", [Method, Path, CT, QS]), {cowboy_rest, Req, State}. is_authorized(Req, State) -> diff --git a/apps/roster/src/rest/rest_cowboy_fake_numbers_handler.erl b/apps/roster/src/rest/rest_cowboy_fake_numbers_handler.erl index 7e3e8591f..e35fb36b8 100644 --- a/apps/roster/src/rest/rest_cowboy_fake_numbers_handler.erl +++ b/apps/roster/src/rest/rest_cowboy_fake_numbers_handler.erl @@ -32,7 +32,9 @@ init(#{path := Path, method := Method, qs := QS} = Req, #{endpoint := Endpoint} = State) when Endpoint =:= fn; Endpoint =:= fake_numbers -> - ?LOG_INFO("~s:~s ~p", [Method, Path, QS]), + Headers = cowboy_req:headers(Req), + CT = maps:get(<<"content-type">>, Headers, <<"no-content-type">>), + ?LOG_INFO("~s:~s:~s ~p", [Method, Path, CT, QS]), {cowboy_rest, Req, State}. resource_exists(Req, State) -> diff --git a/apps/roster/src/rest/rest_cowboy_link_handler.erl b/apps/roster/src/rest/rest_cowboy_link_handler.erl index 82216edc8..53b89df5a 100644 --- a/apps/roster/src/rest/rest_cowboy_link_handler.erl +++ b/apps/roster/src/rest/rest_cowboy_link_handler.erl @@ -26,7 +26,9 @@ init(#{path := Path, method := Method, qs := QS} = Req, #{endpoint := link_room} = State) -> - ?LOG_INFO("~s:~s ~p", [Method, Path, QS]), + Headers = cowboy_req:headers(Req), + CT = maps:get(<<"content-type">>, Headers, <<"no-content-type">>), + ?LOG_INFO("~s:~s:~s ~p", [Method, Path, CT, QS]), {cowboy_rest, Req, State}. is_authorized(Req, State) -> diff --git a/apps/roster/src/rest/rest_cowboy_metric_handler.erl b/apps/roster/src/rest/rest_cowboy_metric_handler.erl index 2b88370df..6c0212ede 100644 --- a/apps/roster/src/rest/rest_cowboy_metric_handler.erl +++ b/apps/roster/src/rest/rest_cowboy_metric_handler.erl @@ -25,7 +25,9 @@ %%%=================================================================== init(#{path := Path, method := Method} = Req, #{endpoint := metrics} = State) -> - ?LOG_INFO("~s:~s", [Method, Path]), + Headers = cowboy_req:headers(Req), + CT = maps:get(<<"content-type">>, Headers, <<"no-content-type">>), + ?LOG_INFO("~s:~s:~s", [Method, Path, CT]), {cowboy_rest, Req, State}. is_authorized(Req, State) -> diff --git a/apps/roster/src/rest/rest_cowboy_publish_handler.erl b/apps/roster/src/rest/rest_cowboy_publish_handler.erl index a030da52b..2ddc1a6fa 100644 --- a/apps/roster/src/rest/rest_cowboy_publish_handler.erl +++ b/apps/roster/src/rest/rest_cowboy_publish_handler.erl @@ -26,7 +26,9 @@ init(#{path := Path, method := Method, qs := QS} = Req, #{endpoint := publish} = State) -> - ?LOG_INFO("~s:~s ~p", [Method, Path, QS]), + Headers = cowboy_req:headers(Req), + CT = maps:get(<<"content-type">>, Headers, <<"no-content-type">>), + ?LOG_INFO("~s:~s:~p ~p", [Method, Path, CT, QS]), {cowboy_rest, Req, State}. is_authorized(Req, State) -> diff --git a/apps/roster/src/rest/rest_cowboy_push_handler.erl b/apps/roster/src/rest/rest_cowboy_push_handler.erl index 5b603bd61..96303a561 100644 --- a/apps/roster/src/rest/rest_cowboy_push_handler.erl +++ b/apps/roster/src/rest/rest_cowboy_push_handler.erl @@ -25,7 +25,9 @@ init(#{path := Path, method := Method, qs := QS} = Req, #{endpoint := push_message} = State) -> - ?LOG_INFO("~s:~s ~p", [Method, Path, QS]), + Headers = cowboy_req:headers(Req), + CT = maps:get(<<"content-type">>, Headers, <<"no-content-type">>), + ?LOG_INFO("~s:~s:~s ~p", [Method, Path, CT, QS]), {cowboy_rest, Req, State}. is_authorized(Req, State) -> diff --git a/apps/roster/src/rest/rest_cowboy_room_handler.erl b/apps/roster/src/rest/rest_cowboy_room_handler.erl index f8fba266c..a613cfe9b 100644 --- a/apps/roster/src/rest/rest_cowboy_room_handler.erl +++ b/apps/roster/src/rest/rest_cowboy_room_handler.erl @@ -27,7 +27,9 @@ init(#{path := Path, method := Method, qs := QS} = Req, #{endpoint := room} = State) -> - ?LOG_INFO("~s:~s ~p", [Method, Path, QS]), + Headers = cowboy_req:headers(Req), + CT = maps:get(<<"content-type">>, Headers, <<"no-content-type">>), + ?LOG_INFO("~s:~s:~s ~p", [Method, Path, CT, QS]), {cowboy_rest, Req, State}. is_authorized(Req, State) -> diff --git a/apps/roster/src/rest/rest_cowboy_session_handler.erl b/apps/roster/src/rest/rest_cowboy_session_handler.erl index 1d4b27941..08e054e7b 100644 --- a/apps/roster/src/rest/rest_cowboy_session_handler.erl +++ b/apps/roster/src/rest/rest_cowboy_session_handler.erl @@ -28,7 +28,9 @@ init(#{path := Path, method := Method, qs := QS} = Req, #{endpoint := sessions} = State) -> - ?LOG_INFO("~s:~s ~p", [Method, Path, QS]), + Headers = cowboy_req:headers(Req), + CT = maps:get(<<"content-type">>, Headers, <<"no-content-type">>), + ?LOG_INFO("~s:~s:~s ~p", [Method, Path, CT, QS]), {cowboy_rest, Req, State}. resource_exists(Req, State) -> diff --git a/apps/roster/src/rest/rest_cowboy_test_handler.erl b/apps/roster/src/rest/rest_cowboy_test_handler.erl index 3ffeb3808..16803f9e8 100644 --- a/apps/roster/src/rest/rest_cowboy_test_handler.erl +++ b/apps/roster/src/rest/rest_cowboy_test_handler.erl @@ -23,7 +23,9 @@ init(#{path := Path, method := Method, qs := QS} = Req, #{endpoint := test_api} = State) -> - ?LOG_INFO("~s:~s ~p", [Method, Path, QS]), + Headers = cowboy_req:headers(Req), + CT = maps:get(<<"content-type">>, Headers, <<"no-content-type">>), + ?LOG_INFO("~s:~s:~s ~p", [Method, Path, CT, QS]), {cowboy_rest, Req, State}. is_authorized(Req, State) -> diff --git a/apps/roster/src/rest/rest_cowboy_transcribe_handler.erl b/apps/roster/src/rest/rest_cowboy_transcribe_handler.erl index fc633efee..92898eafd 100644 --- a/apps/roster/src/rest/rest_cowboy_transcribe_handler.erl +++ b/apps/roster/src/rest/rest_cowboy_transcribe_handler.erl @@ -34,7 +34,9 @@ init(#{path := Path, method := Method, qs := QS} = Req, #{endpoint := Endpoint} = State) when Endpoint =:= ?groups_transcribe; Endpoint =:= ?chats_transcribe -> - ?LOG_INFO("~s:~s ~p", [Method, Path, QS]), + Headers = cowboy_req:headers(Req), + CT = maps:get(<<"content-type">>, Headers, <<"no-content-type">>), + ?LOG_INFO("~s:~s:~s ~p", [Method, Path, CT, QS]), {cowboy_rest, Req, State}. is_authorized(Req, State) -> diff --git a/apps/roster/src/rest/rest_cowboy_users_handler.erl b/apps/roster/src/rest/rest_cowboy_users_handler.erl index 138620201..a23ed0fe7 100644 --- a/apps/roster/src/rest/rest_cowboy_users_handler.erl +++ b/apps/roster/src/rest/rest_cowboy_users_handler.erl @@ -27,7 +27,9 @@ init(#{path := Path, method := Method, qs := QS} = Req, #{endpoint := users} = State) -> - ?LOG_INFO("~s:~s ~p", [Method, Path, QS]), + Headers = cowboy_req:headers(Req), + CT = maps:get(<<"content-type">>, Headers, <<"no-content-type">>), + ?LOG_INFO("~s:~s:~s ~p", [Method, Path, CT, QS]), {cowboy_rest, Req, State}. is_authorized(Req, State) -> diff --git a/apps/roster/src/rest/rest_cowboy_whitelist_handler.erl b/apps/roster/src/rest/rest_cowboy_whitelist_handler.erl index 0597cc95d..e4bb1a16d 100644 --- a/apps/roster/src/rest/rest_cowboy_whitelist_handler.erl +++ b/apps/roster/src/rest/rest_cowboy_whitelist_handler.erl @@ -32,7 +32,9 @@ init(#{path := Path, method := Method, qs := QS} = Req, #{endpoint := Endpoint} = State) when Endpoint =:= whitelist; Endpoint =:= admin_whitelist -> - ?LOG_INFO("~s:~s ~p", [Method, Path, QS]), + Headers = cowboy_req:headers(Req), + CT = maps:get(<<"content-type">>, Headers, <<"no-content-type">>), + ?LOG_INFO("~s:~s:~s ~p", [Method, Path, CT, QS]), {cowboy_rest, Req, State}. resource_exists(Req, State) -> -- GitLab From a55c5ba77eea4726683771a387941dc7133e66ae Mon Sep 17 00:00:00 2001 From: Tobias Lindahl Date: Mon, 4 May 2020 15:58:01 +0200 Subject: [PATCH 3/3] Make a more descriptive return message for group chat rest API --- apps/roster/src/rest/rest_cowboy_chat_handler.erl | 3 ++- spec/nynja_rest.yaml | 5 ++++- test/api_nynja.erl | 7 ++++++- test/server_SUITE.erl | 6 +++--- 4 files changed, 15 insertions(+), 6 deletions(-) diff --git a/apps/roster/src/rest/rest_cowboy_chat_handler.erl b/apps/roster/src/rest/rest_cowboy_chat_handler.erl index c91e67e36..6783e4887 100644 --- a/apps/roster/src/rest/rest_cowboy_chat_handler.erl +++ b/apps/roster/src/rest/rest_cowboy_chat_handler.erl @@ -218,7 +218,8 @@ room_chat_from_json(Req, State) -> State2 -> case send_message(State2) of {ok, MsgId} -> - Req2 = set_ok_resp(integer_to_binary(MsgId), Req1), + Resp = [{<<"server_message_id">>, MsgId}], + Req2 = set_ok_resp(Resp, Req1), {true, Req2, State2}; error -> Req2 = set_error_resp(<<"Failed sending message">>, Req1), diff --git a/spec/nynja_rest.yaml b/spec/nynja_rest.yaml index 350ff4144..61661cd53 100644 --- a/spec/nynja_rest.yaml +++ b/spec/nynja_rest.yaml @@ -375,7 +375,10 @@ paths: status: type: string data: - type: string + type: object + properties: + server_message_id: + type: string '400': description: "Malformed request" content: diff --git a/test/api_nynja.erl b/test/api_nynja.erl index fee6315e9..b68545c8c 100644 --- a/test/api_nynja.erl +++ b/test/api_nynja.erl @@ -463,7 +463,12 @@ operations() -> [{"application/json", #{schema => #{<<"properties">> => - #{<<"data">> => #{<<"type">> => <<"string">>}, + #{<<"data">> => + #{<<"properties">> => + #{<<"server_message_id">> => + #{<<"type">> => <<"integer">>} + }, + <<"type">> => <<"object">>}, <<"status">> => #{<<"type">> => <<"string">>}}, <<"type">> => <<"object">>}}}], diff --git a/test/server_SUITE.erl b/test/server_SUITE.erl index 2d6bde147..a0a9c72e9 100644 --- a/test/server_SUITE.erl +++ b/test/server_SUITE.erl @@ -566,7 +566,7 @@ room_chat_message(Cfg) -> Content = <<"I am the rest message">>, Mime = <<"text">>, - {ok, 200, #{ status := <<"ok">>, data := MsgId1 }} + {ok, 200, #{ status := <<"ok">>, data := #{ server_message_id := MsgId1 }}} = request('PostRoomChatMessage', #{ sender_uuid => User1UUID , room_uuid => RoomUUID @@ -577,12 +577,12 @@ room_chat_message(Cfg) -> %% Make sure the message arrived ct:pal("Emptying: ~p", [nynja:ws_receive(User1, any, 100)]), History1 = nynja:get_messages(User1, RoomFeed, MsgId1, 1), - ensure_msg_in_history(History1, binary_to_integer(MsgId1), Mime, Content), + ensure_msg_in_history(History1, MsgId1, Mime, Content), %% Ensure the other member got it as well. ct:pal("Emptying: ~p", [nynja:ws_receive(User2, any, 100)]), History2 = nynja:get_messages(User2, RoomFeed, MsgId1, 1), - ensure_msg_in_history(History2, binary_to_integer(MsgId1), Mime, Content), + ensure_msg_in_history(History2, MsgId1, Mime, Content), ok. -- GitLab