Рефакторинг обработчиков. Часть 1 #21
This commit is contained in:
@@ -1,18 +1,36 @@
|
||||
-module(admin_handler_health).
|
||||
-behaviour(cowboy_handler).
|
||||
-export([init/2]).
|
||||
|
||||
init(Req, State) ->
|
||||
-export([init/2]).
|
||||
-export([trails/0]).
|
||||
|
||||
%%% cowboy_handler callback
|
||||
init(Req, _State) ->
|
||||
case cowboy_req:method(Req) of
|
||||
<<"GET">> ->
|
||||
Body = jsx:encode(#{status => <<"ok">>}),
|
||||
Req2 = cowboy_req:reply(200, #{<<"content-type">> => <<"application/json">>}, Body, Req),
|
||||
{ok, Req2, State};
|
||||
handler_utils:send_json(Req, 200, #{status => <<"ok">>});
|
||||
_ ->
|
||||
send_error(Req, 405, <<"Method not allowed">>)
|
||||
handler_utils:send_error(Req, 405, <<"Method not allowed">>)
|
||||
end.
|
||||
|
||||
send_error(Req, Status, Message) ->
|
||||
Body = jsx:encode(#{error => Message}),
|
||||
Req2 = cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
|
||||
{ok, Req2, []}.
|
||||
%%% Swagger metadata
|
||||
trails() ->
|
||||
[
|
||||
#{
|
||||
path => <<"/v1/admin/health">>,
|
||||
method => <<"GET">>,
|
||||
description => <<"Admin API health check">>,
|
||||
tags => [<<"Health">>],
|
||||
responses => #{
|
||||
200 => #{
|
||||
description => <<"API is healthy">>,
|
||||
content => #{<<"application/json">> => #{schema => #{
|
||||
type => object,
|
||||
properties => #{
|
||||
status => #{type => string}
|
||||
}
|
||||
}}}
|
||||
}
|
||||
}
|
||||
}
|
||||
].
|
||||
Reference in New Issue
Block a user