Рефакторинг обработчиков. Часть 2 #21

This commit is contained in:
2026-05-11 21:51:45 +03:00
parent 6403f061df
commit 61bb44ab4a
31 changed files with 8391 additions and 1480 deletions
+40 -6
View File
@@ -1,18 +1,52 @@
%%%-------------------------------------------------------------------
%%% @doc Обработчик проверки здоровья клиентского API.
%%% GET – возвращает статус сервера.
%%% @end
%%%-------------------------------------------------------------------
-module(handler_health).
-behaviour(cowboy_handler).
-export([init/2]).
-export([trails/0]).
%%% cowboy_handler callback
-spec init(cowboy_req:req(), any()) -> {ok, cowboy_req:req(), any()}.
init(Req, Opts) ->
handle(Req, Opts).
%%% Swagger metadata
-spec trails() -> [map()].
trails() ->
[
#{
path => <<"/v1/health">>,
method => <<"GET">>,
description => <<"API health check">>,
tags => [<<"Health">>],
responses => #{
200 => #{
description => <<"API is healthy">>,
content => #{<<"application/json">> => #{schema => #{
type => object,
properties => #{
status => #{type => string}
}
}}}
}
}
}
].
%%%===================================================================
%%% Внутренние функции
%%%===================================================================
%% @private
-spec handle(cowboy_req:req(), any()) -> {ok, cowboy_req:req(), any()}.
handle(Req, _Opts) ->
case cowboy_req:method(Req) of
<<"GET">> ->
Body = jsx:encode(#{status => <<"ok">>}),
Req1 = cowboy_req:reply(200, #{<<"content-type">> => <<"application/json">>}, Body, Req),
{ok, Req1, []};
handler_utils:send_json(Req, 200, #{status => <<"ok">>});
_ ->
Body = jsx:encode(#{error => <<"Method not allowed">>}),
Req1 = cowboy_req:reply(405, #{<<"content-type">> => <<"application/json">>}, Body, Req),
{ok, Req1, []}
handler_utils:send_error(Req, 405, <<"Method not allowed">>)
end.