Current section
Files
Jump to
Current section
Files
src/docs@static.erl
-module(docs@static).
-compile([no_auto_import, nowarn_unused_vars]).
-export([priv_directory/0, middleware/1]).
-spec priv_directory() -> binary().
priv_directory() ->
sprocket_ffi:priv_directory().
-spec middleware(
fun((gleam@http@request:request(NQE)) -> gleam@http@response:response(gleam@bit_builder:bit_builder()))
) -> fun((gleam@http@request:request(NQE)) -> gleam@http@response:response(gleam@bit_builder:bit_builder())).
middleware(Service) ->
fun(Request) ->
Request_path = case erlang:element(8, Request) of
<<"/"/utf8>> ->
<<"/index.html"/utf8>>;
Path ->
Path
end,
Path@1 = begin
_pipe = Request_path,
_pipe@1 = gleam@string:replace(_pipe, <<".."/utf8>>, <<""/utf8>>),
_pipe@2 = gleam@string:replace(_pipe@1, <<"//"/utf8>>, <<"/"/utf8>>),
_pipe@3 = gleam@string:append(<<"/static"/utf8>>, _pipe@2),
gleam@string:append(sprocket_ffi:priv_directory(), _pipe@3)
end,
File_contents = begin
_pipe@4 = Path@1,
_pipe@5 = gleam@erlang@file:read_bits(_pipe@4),
_pipe@6 = gleam@result:nil_error(_pipe@5),
gleam@result:map(_pipe@6, fun gleam@bit_builder:from_bit_string/1)
end,
Extension = begin
_pipe@7 = Path@1,
_pipe@8 = gleam@string:split(_pipe@7, <<"."/utf8>>),
_pipe@9 = gleam@list:last(_pipe@8),
gleam@result:unwrap(_pipe@9, <<""/utf8>>)
end,
case File_contents of
{ok, Bits} ->
Content_type = case Extension of
<<"html"/utf8>> ->
<<"text/html"/utf8>>;
<<"css"/utf8>> ->
<<"text/css"/utf8>>;
<<"js"/utf8>> ->
<<"application/javascript"/utf8>>;
<<"png"/utf8>> ->
<<"image/jpeg"/utf8>>;
<<"jpg"/utf8>> ->
<<"image/jpeg"/utf8>>;
<<"gif"/utf8>> ->
<<"image/gif"/utf8>>;
<<"svg"/utf8>> ->
<<"image/svg+xml"/utf8>>;
<<"ico"/utf8>> ->
<<"image/x-icon"/utf8>>;
_ ->
<<"octet-stream"/utf8>>
end,
{response, 200, [{<<"content-type"/utf8>>, Content_type}], Bits};
{error, _} ->
Service(Request)
end
end.