Current section
Files
Jump to
Current section
Files
src/howdy@filter.erl
-module(howdy@filter).
-compile(no_auto_import).
-export([must_accept/2, accepts_json/1, accepts_html/1, authenticate/2]).
-spec must_accept(
fun((howdy@context:context()) -> gleam@http@response:response(gleam@bit_builder:bit_builder())),
binary()
) -> fun((howdy@context:context()) -> gleam@http@response:response(gleam@bit_builder:bit_builder())).
must_accept(Filter, Mime_type) ->
fun(Context) ->
case howdy@context@header:get_value(Context, <<"Accept"/utf8>>) of
{ok, Value} when Value =:= Mime_type ->
Filter(Context);
_@1 ->
howdy@response:of_not_found(<<""/utf8>>)
end
end.
-spec accepts_json(
fun((howdy@context:context()) -> gleam@http@response:response(gleam@bit_builder:bit_builder()))
) -> fun((howdy@context:context()) -> gleam@http@response:response(gleam@bit_builder:bit_builder())).
accepts_json(Filter) ->
must_accept(Filter, howdy@mime:from_extention(<<"json"/utf8>>)).
-spec accepts_html(
fun((howdy@context:context()) -> gleam@http@response:response(gleam@bit_builder:bit_builder()))
) -> fun((howdy@context:context()) -> gleam@http@response:response(gleam@bit_builder:bit_builder())).
accepts_html(Filter) ->
must_accept(Filter, howdy@mime:from_extention(<<"html"/utf8>>)).
-spec authenticate(
fun((howdy@context:context()) -> gleam@http@response:response(gleam@bit_builder:bit_builder())),
fun((howdy@context:context()) -> {ok, howdy@context@user:user()} |
{error, nil})
) -> fun((howdy@context:context()) -> gleam@http@response:response(gleam@bit_builder:bit_builder())).
authenticate(Filter, Auth) ->
fun(Context) -> case Auth(Context) of
{ok, User} ->
_pipe = erlang:setelement(4, Context, {some, User}),
Filter(_pipe);
{error, _@1} ->
_pipe@1 = howdy@response:of_string(<<""/utf8>>),
howdy@response:with_status(_pipe@1, 401)
end end.