Current section

Files

Jump to
glisten src glisten.erl
Raw

src/glisten.erl

-module(glisten).
-compile(no_auto_import).
-export([serve/2, serve_ssl/4]).
-export_type([start_error/0]).
-type start_error() :: listener_closed |
listener_timeout |
acceptor_timeout |
{acceptor_failed, gleam@erlang@process:exit_reason()} |
{acceptor_crashed, gleam@dynamic:dynamic()}.
-spec serve(
integer(),
fun((glisten@socket:listen_socket()) -> glisten@acceptor:pool(any()))
) -> {ok, nil} | {error, start_error()}.
serve(Port, With_pool) ->
case begin
_pipe = Port,
_pipe@1 = glisten@tcp:listen(_pipe, []),
_pipe@2 = gleam@result:map_error(_pipe@1, fun(Err) -> case Err of
closed ->
listener_closed;
timeout ->
listener_timeout
end end),
gleam@result:then(
_pipe@2,
fun(Socket) ->
_pipe@3 = Socket,
_pipe@4 = With_pool(_pipe@3),
_pipe@5 = glisten@acceptor:start_pool(_pipe@4),
gleam@result:map_error(_pipe@5, fun(Err@1) -> case Err@1 of
init_timeout ->
acceptor_timeout;
{init_failed, Reason} ->
{acceptor_failed, Reason};
{init_crashed, Reason@1} ->
{acceptor_crashed, Reason@1}
end end)
end
)
end of
{error, _try} -> {error, _try};
{ok, _@1} ->
{ok, nil}
end.
-spec serve_ssl(
integer(),
binary(),
binary(),
fun((glisten@socket:listen_socket()) -> glisten@acceptor:pool(any()))
) -> {ok, nil} | {error, start_error()}.
serve_ssl(Port, Certfile, Keyfile, With_pool) ->
{ok, _@2} = case ssl_ffi:start_ssl() of
{ok, _@1} -> {ok, _@1};
_try ->
erlang:error(#{gleam_error => assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _try,
module => <<"glisten"/utf8>>,
function => <<"serve_ssl"/utf8>>,
line => 64})
end,
case begin
_pipe = Port,
_pipe@1 = glisten@ssl:listen(
_pipe,
[{certfile, Certfile}, {keyfile, Keyfile}]
),
_pipe@2 = gleam@result:map_error(_pipe@1, fun(Err) -> case Err of
closed ->
listener_closed;
timeout ->
listener_timeout
end end),
gleam@result:then(
_pipe@2,
fun(Socket) ->
_pipe@3 = Socket,
_pipe@4 = (glisten@acceptor:over_ssl(With_pool))(_pipe@3),
_pipe@5 = glisten@acceptor:start_pool(_pipe@4),
gleam@result:map_error(_pipe@5, fun(Err@1) -> case Err@1 of
init_timeout ->
acceptor_timeout;
{init_failed, Reason} ->
{acceptor_failed, Reason};
{init_crashed, Reason@1} ->
{acceptor_crashed, Reason@1}
end end)
end
)
end of
{error, _try@1} -> {error, _try@1};
{ok, _@3} ->
{ok, nil}
end.