Skip to main content

src/starfruit.erl

-module(starfruit).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src\\starfruit.gleam").
-export([start/5]).

-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\\starfruit.gleam", 13).
?DOC(" This is all you need!\n").
-spec start(integer(), binary(), binary(), boolean(), binary()) -> nil.
start(Port, Cert, Key, Loopback, Hostname) ->
    gleam_stdlib:print(
        erlang:list_to_binary(
            [<<"⋆✴︎˚。⋆ Starfruit is listening on port "/utf8>>,
                erlang:integer_to_binary(Port),
                <<"!! ⋆。˚✴︎⋆\n"/utf8>>]
        )
    ),
    case begin
        _pipe@3 = glisten:new(
            fun(_) -> {nil, none} end,
            fun(_, Msg, Conn) ->
                Source@1 = case glisten:get_connection_info(Conn) of
                    {ok, Source} -> Source;
                    _assert_fail ->
                        erlang:error(#{gleam_error => let_assert,
                                    message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
                                    file => <<?FILEPATH/utf8>>,
                                    module => <<"starfruit"/utf8>>,
                                    function => <<"start"/utf8>>,
                                    line => 29,
                                    value => _assert_fail,
                                    start => 690,
                                    'end' => 747,
                                    pattern_start => 701,
                                    pattern_end => 711})
                end,
                gleam_stdlib:print(
                    <<<<<<(gleam@time@timestamp:to_rfc3339(
                                    gleam@time@timestamp:system_time(),
                                    gleam@time@calendar:local_offset()
                                ))/binary,
                                "\nIncoming request from "/utf8>>/binary,
                            (glisten:ip_address_to_string(
                                erlang:element(3, Source@1)
                            ))/binary>>/binary,
                        " for "/utf8>>
                ),
                case erlang:element(3, Conn) of
                    tcp ->
                        glisten:stop();

                    ssl ->
                        Msg@2 = case Msg of
                            {packet, Msg@1} -> Msg@1;
                            _assert_fail@1 ->
                                erlang:error(#{gleam_error => let_assert,
                                            message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
                                            file => <<?FILEPATH/utf8>>,
                                            module => <<"starfruit"/utf8>>,
                                            function => <<"start"/utf8>>,
                                            line => 43,
                                            value => _assert_fail@1,
                                            start => 1205,
                                            'end' => 1233,
                                            pattern_start => 1216,
                                            pattern_end => 1227})
                        end,
                        _ = begin
                            _pipe = starfruit@internal:parse_url(
                                Msg@2,
                                Port,
                                Hostname,
                                erlang:element(3, Source@1)
                            ),
                            _pipe@1 = starfruit@internal:router(_pipe),
                            _pipe@2 = starfruit@internal:response(_pipe@1),
                            starfruit@internal:senddata(_pipe@2, Conn)
                        end,
                        glisten:stop()
                end
            end
        ),
        _pipe@4 = glisten:with_tls(_pipe@3, Cert, Key),
        _pipe@5 = starfruit@internal:bind(_pipe@4, Loopback),
        glisten:start(_pipe@5, Port)
    end of
        {ok, _} -> nil;
        _assert_fail@2 ->
            erlang:error(#{gleam_error => let_assert,
                        message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
                        file => <<?FILEPATH/utf8>>,
                        module => <<"starfruit"/utf8>>,
                        function => <<"start"/utf8>>,
                        line => 27,
                        value => _assert_fail@2,
                        start => 595,
                        'end' => 1607,
                        pattern_start => 606,
                        pattern_end => 611})
    end,
    gleam_erlang_ffi:sleep_forever().