Current section

Files

Jump to
lustre src lustre@try.erl
Raw

src/lustre@try.erl

-module(lustre@try).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([main/0]).
-export_type([options/0]).
-type options() :: {options, binary(), integer(), boolean()}.
-spec host_flag() -> glint@flag:flag_builder(binary()).
host_flag() ->
_pipe = glint@flag:string(),
_pipe@1 = glint@flag:default(_pipe, <<"localhost"/utf8>>),
glint@flag:description(_pipe@1, <<"The host to run the server on"/utf8>>).
-spec port_flag() -> glint@flag:flag_builder(integer()).
port_flag() ->
_pipe = glint@flag:int(),
_pipe@1 = glint@flag:default(_pipe, 1234),
glint@flag:description(_pipe@1, <<"The port to run the server on"/utf8>>).
-spec include_styles_flag() -> glint@flag:flag_builder(boolean()).
include_styles_flag() ->
_pipe = glint@flag:bool(),
_pipe@1 = glint@flag:default(_pipe, false),
glint@flag:description(
_pipe@1,
<<"Include lustre_ui's default stylesheet in your app."/utf8>>
).
-spec on_start(binary(), integer()) -> nil.
on_start(Host, Port) ->
Address = <<<<<<"http://"/utf8, Host/binary>>/binary, ":"/utf8>>/binary,
(gleam@int:to_string(Port))/binary>>,
gleam@io:println(
<<"✨ Server has been started at "/utf8,
(gleam_community@ansi:bold(Address))/binary>>
).
-spec on_port_taken(integer()) -> nil.
on_port_taken(Port) ->
gleam@io:println(
<<<<"🚨 Port "/utf8,
(gleam_community@ansi:bold(gleam@int:to_string(Port)))/binary>>/binary,
" already in use, using next available port"/utf8>>
).
-spec main() -> nil.
main() ->
Args = erlang:element(4, argv:load()),
Program = begin
_pipe = glint:new(),
_pipe@1 = glint:with_name(_pipe, <<"gleam run -m lustre/try"/utf8>>),
_pipe@2 = glint:with_pretty_help(_pipe@1, glint:default_pretty_help()),
glint:add(
_pipe@2,
[],
begin
_pipe@3 = glint:command(
fun(Input) ->
{command_input, _, Flags} = Input,
_assert_subject = glint@flag:get_int(
Flags,
<<"port"/utf8>>
),
{ok, Port} = case _assert_subject of
{ok, _} -> _assert_subject;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail,
module => <<"lustre/try"/utf8>>,
function => <<"main"/utf8>>,
line => 37})
end,
_assert_subject@1 = glint@flag:get_string(
Flags,
<<"host"/utf8>>
),
{ok, Host} = case _assert_subject@1 of
{ok, _} -> _assert_subject@1;
_assert_fail@1 ->
erlang:error(#{gleam_error => let_assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail@1,
module => <<"lustre/try"/utf8>>,
function => <<"main"/utf8>>,
line => 38})
end,
_assert_subject@2 = glint@flag:get_bool(
Flags,
<<"include-styles"/utf8>>
),
{ok, Include_styles} = case _assert_subject@2 of
{ok, _} -> _assert_subject@2;
_assert_fail@2 ->
erlang:error(#{gleam_error => let_assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail@2,
module => <<"lustre/try"/utf8>>,
function => <<"main"/utf8>>,
line => 39})
end,
Options = {options, Host, Port, Include_styles},
http_ffi:exec(<<"gleam build --target js"/utf8>>),
http_ffi:serve(
Options,
fun(_capture) -> on_start(Host, _capture) end,
fun on_port_taken/1
)
end
),
_pipe@4 = glint:flag(_pipe@3, <<"host"/utf8>>, host_flag()),
_pipe@5 = glint:flag(_pipe@4, <<"port"/utf8>>, port_flag()),
glint:flag(
_pipe@5,
<<"include-styles"/utf8>>,
include_styles_flag()
)
end
)
end,
glint:run(Program, Args).