/
mainstream
/
hw11
Обзор
Документация
Войти
/
mainstream
/
hw11
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/sipcall_http_handler.erl
41 строка
1 KB
iv323s29
Add
08 фев 2026, 18:59
08 фев 2026, 18:59
0be3761
Код
Авторство
О чём код?
-module(sipcall_http_handler). -export([init/2]). init(Req0, State) -> Method = cowboy_req:method(Req0), Path = cowboy_req:path(Req0), case {Method, Path} of {<<"GET">>, <<"/api/call/", UserId/binary>>} -> handle_call(UserId, Req0); {<<"GET">>, <<"/api/callers">>} -> handle_list(Req0); _ -> {ok, Req, _} = cowboy_req:reply(404, #{}, <<"Not found">>, Req0), {ok, Req, State} end. handle_call(UserId, Req) -> case ets:lookup(caller_uris, binary_to_list(UserId)) of [{_, UriBin}] -> FullUri = case binary:match(UriBin, <<"sip:">>) of nomatch -> <<"sip:", UriBin/binary>>; _ -> UriBin end, io:format("📞 Calling back: ~s~n", [FullUri]), case nksip_uac:invite(sipcall_sip, FullUri, #{}) of {ok, _} -> reply(200, #{status => <<"ok">>, to => UriBin}, Req); {error, R} -> reply(500, #{error => atom_to_binary(R, utf8)}, Req) end; [] -> reply(404, #{error => <<"user_not_found">>}, Req) end. handle_list(Req) -> Callers = ets:tab2list(caller_uris), List = [{list_to_binary(K), V} || {K, V} <- Callers], reply(200, #{callers => List}, Req). reply(Code, Data, Req) -> Body = jsone:encode(Data), Headers = #{<<"content-type">> => <<"application/json">>}, {ok, Req2, _} = cowboy_req:reply(Code, Headers, Body, Req), {ok, Req2, []}.