Current section

Files

Jump to
sprocket src docs@routes.erl
Raw

src/docs@routes.erl

-module(docs@routes).
-compile([no_auto_import, nowarn_unused_vars]).
-export([router/1, string_body_middleware/1, stack/1]).
-spec not_found() -> gleam@http@response:response(binary()).
not_found() ->
_pipe = gleam@http@response:new(404),
_pipe@1 = gleam@http@response:set_body(_pipe, <<"Page not found"/utf8>>),
gleam@http@response:prepend_header(
_pipe@1,
<<"content-type"/utf8>>,
<<"text/plain"/utf8>>
).
-spec router(docs@app_context:app_context()) -> fun((gleam@http@request:request(binary())) -> gleam@http@response:response(binary())).
router(Ctx) ->
fun(Request) ->
case {erlang:element(2, Request),
gleam@http@request:path_segments(Request)} of
{get, _} ->
docs@controllers@index:index(Request, Ctx);
{_, _} ->
not_found()
end
end.
-spec bad_request() -> gleam@http@response:response(binary()).
bad_request() ->
_pipe = gleam@http@response:new(400),
_pipe@1 = gleam@http@response:set_body(
_pipe,
<<"Bad request. Please try again"/utf8>>
),
gleam@http@response:prepend_header(
_pipe@1,
<<"content-type"/utf8>>,
<<"text/plain"/utf8>>
).
-spec string_body_middleware(
fun((gleam@http@request:request(binary())) -> gleam@http@response:response(binary()))
) -> fun((gleam@http@request:request(bitstring())) -> gleam@http@response:response(gleam@bit_builder:bit_builder())).
string_body_middleware(Service) ->
fun(Request) ->
_pipe = case gleam@bit_string:to_string(erlang:element(4, Request)) of
{ok, Body} ->
Service(gleam@http@request:set_body(Request, Body));
{error, _} ->
bad_request()
end,
gleam@http@response:map(_pipe, fun gleam@bit_builder:from_string/1)
end.
-spec stack(docs@app_context:app_context()) -> fun((gleam@http@request:request(bitstring())) -> gleam@http@response:response(gleam@bit_builder:bit_builder())).
stack(Ctx) ->
_pipe = router(Ctx),
_pipe@1 = string_body_middleware(_pipe),
_pipe@2 = docs@log_requests:middleware(_pipe@1),
_pipe@3 = docs@static:middleware(_pipe@2),
gleam@http@service:prepend_response_header(
_pipe@3,
<<"made-with"/utf8>>,
<<"Gleam"/utf8>>
).