Current section

Files

Jump to
gcourier src gcourier.erl
Raw

src/gcourier.erl

-module(gcourier).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([dev_server/0]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
-file("src/gcourier.gleam", 42).
-spec check_live() -> boolean().
check_live() ->
_assert_subject = gleam@http@request:to(<<"http://localhost:8025"/utf8>>),
{ok, Req} = case _assert_subject of
{ok, _} -> _assert_subject;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
value => _assert_fail,
module => <<"gcourier"/utf8>>,
function => <<"check_live"/utf8>>,
line => 43})
end,
Resp = begin
_pipe = Req,
gleam@hackney:send(_pipe)
end,
case Resp of
{error, _} ->
gleam_erlang_ffi:sleep(1000),
check_live();
{ok, _} ->
true
end.
-file("src/gcourier.gleam", 13).
?DOC(
" Starts an SMTP server that captures outgoing mail and\n"
" displays it in the browser. By default, the server runs on localhost:1025, \n"
" and the UI runs on localhost:8025. It accepts username and password combination.\n"
).
-spec dev_server() -> nil.
dev_server() ->
_assert_subject = gleam@http@request:to(<<"http://localhost:8025"/utf8>>),
{ok, Req} = case _assert_subject of
{ok, _} -> _assert_subject;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
value => _assert_fail,
module => <<"gcourier"/utf8>>,
function => <<"dev_server"/utf8>>,
line => 14})
end,
Resp = begin
_pipe = Req,
gleam@hackney:send(_pipe)
end,
case Resp of
{error, _} ->
Binary = priv:find_bin(<<"mailpit"/utf8>>),
gleam@erlang@process:start(
fun() ->
shellout:command(
Binary,
[<<"--smtp-auth-accept-any"/utf8>>,
<<"--smtp-auth-allow-insecure"/utf8>>],
<<"."/utf8>>,
[]
)
end,
true
),
check_live(),
nil;
{ok, _} ->
gleam_stdlib:println(
<<"Successfully started SMTP server on localhost:1025."/utf8>>
),
gleam_stdlib:println(
<<"Running web UI at http://localhost:8025."/utf8>>
),
nil
end.