Skip to content

Commit

Permalink
fix: restructured the plugins (#224)
Browse files Browse the repository at this point in the history
  • Loading branch information
Taure committed Oct 28, 2023
1 parent 70bb5c2 commit 517a9e7
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 33 deletions.
2 changes: 2 additions & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
* @burbas
* @Taure
13 changes: 2 additions & 11 deletions src/plugins/nova_cors_plugin.erl
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@
%%--------------------------------------------------------------------
-spec pre_request(Req :: cowboy_req:req(), Options :: map()) ->
{ok, Req0 :: cowboy_req:req()}.
pre_request(Req, Options) ->
ParsedOptions = nova_plugin_utilities:parse_options(Options),
ReqWithOptions = set_headers(Req, ParsedOptions),
pre_request(Req, #{allow_origins := Origins}) ->
ReqWithOptions = add_cors_headers(Req, Origins),
continue(ReqWithOptions).

%%--------------------------------------------------------------------
Expand Down Expand Up @@ -61,14 +60,6 @@ continue(#{method := <<"OPTIONS">>} = Req) ->
{stop, Reply};
continue(Req) ->
{ok, Req}.

set_headers(Req, []) -> Req;
set_headers(Req, [{allow_origins, Origins}|T]) ->
CorsReq = add_cors_headers(Req, Origins),
set_headers(CorsReq, T);
set_headers(Req, [_H|T]) ->
set_headers(Req, T).

add_cors_headers(Req, Origins) ->
OriginsReq = cowboy_req:set_resp_header(
<<"Access-Control-Allow-Origin">>, Origins, Req),
Expand Down
14 changes: 0 additions & 14 deletions src/plugins/nova_plugin_utilities.erl

This file was deleted.

19 changes: 11 additions & 8 deletions src/plugins/nova_request_plugin.erl
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
-spec pre_request(Req :: cowboy_req:req(), Options :: map()) ->
{ok, Req0 :: cowboy_req:req()}.
pre_request(Req, Options) ->
Options0 = nova_plugin_utilities:parse_options(Options),
ListOptions = maps:to_list(Options),
%% Read the body and put it into the Req object
BodyReq = case cowboy_req:has_body(Req) of
true ->
read_body(Req, <<>>);
false ->
Req#{body => <<>>}
end,
{ok, _} = modulate_state(BodyReq, Options0).
{ok, _} = modulate_state(BodyReq, ListOptions).

%%--------------------------------------------------------------------
%% @doc
Expand Down Expand Up @@ -65,9 +65,9 @@ plugin_info() ->
modulate_state(Req, []) ->
{ok, Req};

modulate_state( Req = #{method := Method}, [decode_json_body|Tail]) when Method =:= <<"GET">>; Method =:= <<"DELETE">> ->
modulate_state( Req = #{method := Method}, [{decode_json_body, true}|Tail]) when Method =:= <<"GET">>; Method =:= <<"DELETE">> ->
modulate_state(Req, Tail);
modulate_state(Req = #{headers := #{<<"content-type">> := <<"application/json", _/binary>>}, body := Body}, [decode_json_body|Tl]) ->
modulate_state(Req = #{headers := #{<<"content-type">> := <<"application/json", _/binary>>}, body := Body}, [{decode_json_body, true}|Tl]) ->
%% Decode the data
JsonLib = nova:get_env(json_lib, thoas),
case erlang:apply(JsonLib, decode, [Body]) of
Expand All @@ -81,15 +81,18 @@ modulate_state(Req = #{headers := #{<<"content-type">> := <<"application/json",
{stop, Req400}
end;
modulate_state(#{headers := #{<<"content-type">> := <<"application/x-www-form-urlencoded", _/binary>>}, body := Body} = Req,
[read_urlencoded_body|Tl]) ->
[{read_urlencoded_body, true}|Tl]) ->
Data = cow_qs:parse_qs(Body),
%% First read in the body
Params = maps:from_list(Data),
modulate_state(Req#{params => Params}, Tl);
modulate_state(Req, [parse_qs|T1]) ->
modulate_state(Req, [{parse_qs, Type}|T1]) ->
Qs = cowboy_req:parse_qs(Req),
MapQs = maps:from_list(Qs),
modulate_state(Req#{parsed_qs => MapQs}, T1);
case Type of
true -> MapQs = maps:from_list(Qs),
modulate_state(Req#{parsed_qs => MapQs}, T1);
list -> modulate_state(Req#{parsed_qs => Qs}, T1)
end;
modulate_state(State, [_|Tl]) ->
modulate_state(State, Tl).

Expand Down

0 comments on commit 517a9e7

Please sign in to comment.