Current section
Files
Jump to
Current section
Files
src/howdy@static_resource.erl
-module(howdy@static_resource).
-compile(no_auto_import).
-export([get_file_contents/1, get_spa_file_contents/2]).
-export_type([error/0, file_info/0]).
-type error() :: file_not_found | wildcard_path_not_found | internal_error.
-type file_info() :: {file_info, binary(), bitstring()}.
-spec get_file_contents(binary()) -> fun((howdy@context:context(any())) -> gleam@http@response:response(gleam@bit_builder:bit_builder())).
get_file_contents(File_path) ->
gleam@function:compose(
fun(Context) ->
case get_wildcard_segment_url(erlang:element(2, Context), File_path) of
{error, _try} -> {error, _try};
{ok, Full_path_result} ->
read_file(Full_path_result)
end
end,
fun result_to_response/1
).
-spec get_spa_file_contents(binary(), binary()) -> fun((howdy@context:context(any())) -> gleam@http@response:response(gleam@bit_builder:bit_builder())).
get_spa_file_contents(File_path, Default_file) ->
gleam@function:compose(
fun(Context) ->
case get_wildcard_segment_url(erlang:element(2, Context), File_path) of
{ok, Full_path_result} ->
case read_file(Full_path_result) of
{ok, File} ->
{ok, File};
{error, _@1} ->
read_file(Default_file)
end;
{error, _@2} ->
read_file(Default_file)
end
end,
fun result_to_response/1
).
-spec read_file(binary()) -> {ok, file_info()} | {error, error()}.
read_file(File_path) ->
case filelib:is_file(File_path) of
true ->
_pipe = gleam@erlang@file:read_bits(File_path),
_pipe@1 = gleam@result:map(
_pipe,
fun(Content) ->
{file_info, howdy@mime:from_path(File_path), Content}
end
),
gleam@result:replace_error(_pipe@1, internal_error);
false ->
{error, file_not_found}
end.
-spec file_response(bitstring(), binary()) -> gleam@http@response:response(gleam@bit_builder:bit_builder()).
file_response(File_contents, Content_type) ->
_pipe = howdy@response:of_bit_string(File_contents),
howdy@response:with_content_type(_pipe, Content_type).
-spec get_wildcard_segment_url(list(howdy@url_parser:url_segment()), binary()) -> {ok,
binary()} |
{error, error()}.
get_wildcard_segment_url(Segments, Root) ->
case begin
_pipe = gleam@list:last(Segments),
gleam@result:replace_error(_pipe, wildcard_path_not_found)
end of
{error, _try} -> {error, _try};
{ok, Segment} ->
case Segment of
{wildcard_segment, _@1, _@2, Url} ->
{ok, join_paths(Root, Url)};
_@3 ->
{error, wildcard_path_not_found}
end
end.
-spec join_paths(binary(), binary()) -> binary().
join_paths(Root, Path) ->
Modified_path = gleam@string:replace(Path, <<"\\"/utf8>>, <<"/"/utf8>>),
case {gleam@string:ends_with(Root, <<"/"/utf8>>),
gleam@string:starts_with(Modified_path, <<"/"/utf8>>)} of
{true, true} ->
gleam@string:concat(
[Root, gleam@string:drop_left(Modified_path, 1)]
);
{false, false} ->
gleam@string:concat([Root, <<"/"/utf8>>, Modified_path]);
{_@1, _@2} ->
gleam@string:concat([Root, Modified_path])
end.
-spec result_to_response({ok, file_info()} | {error, error()}) -> gleam@http@response:response(gleam@bit_builder:bit_builder()).
result_to_response(Result) ->
case Result of
{ok, File_info} ->
file_response(
erlang:element(3, File_info),
erlang:element(2, File_info)
);
{error, Error} ->
error_response(Error)
end.
-spec error_response(error()) -> gleam@http@response:response(gleam@bit_builder:bit_builder()).
error_response(Error) ->
case Error of
file_not_found ->
howdy@response:of_not_found(<<"File not found init"/utf8>>);
wildcard_path_not_found ->
howdy@response:of_internal_error(<<"Error finding content"/utf8>>);
internal_error ->
howdy@response:of_internal_error(<<"Error finding content"/utf8>>)
end.