diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 6f7fbee..a6aa67f 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -15,7 +15,7 @@ jobs: strategy: matrix: - otp_version: ['24', '23', '22', '21'] + otp_version: ['24', '23', '22'] os: [ubuntu-latest] steps: diff --git a/elvis.config b/elvis.config index c96c782..c23cd5d 100644 --- a/elvis.config +++ b/elvis.config @@ -22,17 +22,18 @@ ]}}, {elvis_style, used_ignored_variable}, {elvis_style, no_behavior_info}, - {elvis_style, state_record_and_type}, + {elvis_style, atom_naming_convention, + #{ignore => [elli_http]}}, + {elvis_style, state_record_and_type, + #{ignore => [elli]}}, {elvis_style, no_spec_with_records}, {elvis_style, dont_repeat_yourself}, - {elvis_style, no_debug_call} + {elvis_style, no_debug_call}, + {elvis_style, god_modules, + #{ignore => [elli_request]}} ], ruleset => erl_files }, - #{dirs => ["."], - filter => "Makefile", - ruleset => makefiles - }, #{dirs => ["."], filter => "rebar.config", ruleset => rebar_config diff --git a/rebar.config b/rebar.config index 07ba484..8c24567 100644 --- a/rebar.config +++ b/rebar.config @@ -26,7 +26,7 @@ {project_plugins, [ {covertool, "2.0.3"}, - {rebar3_lint, "v0.1.10"} + {rebar3_lint, "1.0.1"} ]}. {provider_hooks, [{pre, [{eunit, lint}]}]}. diff --git a/src/elli.erl b/src/elli.erl index 9f233ce..8f7660e 100644 --- a/src/elli.erl +++ b/src/elli.erl @@ -9,6 +9,7 @@ -behaviour(gen_server). -include("elli.hrl"). -include("elli_util.hrl"). +-include_lib("kernel/include/logger.hrl"). %% API -export([start_link/0, diff --git a/src/elli_example_callback.erl b/src/elli_example_callback.erl index 1dd5ed9..84564ee 100644 --- a/src/elli_example_callback.erl +++ b/src/elli_example_callback.erl @@ -15,6 +15,7 @@ -behaviour(elli_handler). -include_lib("kernel/include/file.hrl"). +-include_lib("kernel/include/logger.hrl"). %% %% ELLI REQUEST CALLBACK diff --git a/src/elli_http.erl b/src/elli_http.erl index 2baaac3..8b86fd2 100644 --- a/src/elli_http.erl +++ b/src/elli_http.erl @@ -6,7 +6,7 @@ -module(elli_http). -include("elli.hrl"). -include("elli_util.hrl"). - +-include_lib("kernel/include/logger.hrl"). %% API -export([start_link/4]). @@ -324,17 +324,17 @@ execute_callback(#req{callback = {Mod, Args}} = Req) -> catch throw:{ResponseCode, Headers, Body} when is_integer(ResponseCode) -> {response, ResponseCode, Headers, Body}; - ?WITH_STACKTRACE(throw, Exc, Stacktrace) + throw:Exc:Stacktrace -> handle_event(Mod, request_throw, [Req, Exc, Stacktrace], Args), {response, 500, [], <<"Internal server error">>}; - ?WITH_STACKTRACE(error, Error, Stacktrace) + error:Error:Stacktrace -> handle_event(Mod, request_error, [Req, Error, Stacktrace], Args), {response, 500, [], <<"Internal server error">>}; - ?WITH_STACKTRACE(exit, Exit, Stacktrace) + exit:Exit:Stacktrace -> handle_event(Mod, request_exit, [Req, Exit, Stacktrace], Args), @@ -716,7 +716,6 @@ is_header_defined(Key, Headers) -> get_header(Key, Headers) -> get_header(Key, Headers, undefined). --ifdef(OTP_RELEASE). get_header(Key, Headers, Default) -> CaseFoldedKey = string:casefold(Key), case lists:search(fun({N, _}) -> string:equal(CaseFoldedKey, N, true) end, Headers) of @@ -725,24 +724,6 @@ get_header(Key, Headers, Default) -> false -> Default end. --else. -get_header(Key, Headers, Default) -> - CaseFoldedKey = string:casefold(Key), - case search(fun({N, _}) -> string:equal(CaseFoldedKey, N, true) end, Headers) of - {value, {_, Value}} -> - Value; - false -> - Default - end. - -search(Pred, [Hd|Tail]) -> - case Pred(Hd) of - true -> {value, Hd}; - false -> search(Pred, Tail) - end; -search(Pred, []) when is_function(Pred, 1) -> - false. --endif. default_scheme_port(<<"http">>) -> 80; @@ -755,58 +736,31 @@ default_scheme_port(_) -> %% PATH HELPERS %% --ifdef(OTP_RELEASE). - -if(?OTP_RELEASE >= 22). - parse_path(SocketScheme, HostHeader, {abs_path, FullPath}) -> - case uri_string:parse(FullPath) of - URIMap when not is_map_key(host, URIMap) , - SocketScheme =/= undefined , - HostHeader =/= undefined -> - HostMap = uri_string:parse( - unicode:characters_to_binary([SocketScheme, "://", HostHeader])), - Host = maps:get(host, HostMap, HostHeader), - Scheme = maps:get(scheme, HostMap, SocketScheme), - Port = maps:get(port, HostMap, default_scheme_port(Scheme)); - URIMap -> - Host = maps:get(host, URIMap, HostHeader), - Scheme = maps:get(scheme, URIMap, SocketScheme), - Port = maps:get(port, URIMap, default_scheme_port(Scheme)) - end, +parse_path(SocketScheme, HostHeader, {abs_path, FullPath}) -> + case uri_string:parse(FullPath) of + URIMap when not is_map_key(host, URIMap) , + SocketScheme =/= undefined , + HostHeader =/= undefined -> + HostMap = uri_string:parse( + unicode:characters_to_binary([SocketScheme, "://", HostHeader])), + Host = maps:get(host, HostMap, HostHeader), + Scheme = maps:get(scheme, HostMap, SocketScheme), + Port = maps:get(port, HostMap, default_scheme_port(Scheme)); + URIMap -> + Host = maps:get(host, URIMap, HostHeader), + Scheme = maps:get(scheme, URIMap, SocketScheme), + Port = maps:get(port, URIMap, default_scheme_port(Scheme)) + end, - Path = maps:get(path, URIMap, <<>>), - Query = maps:get(query, URIMap, <<>>), - - {ok, {Scheme, Host, Port}, - {Path, split_path(Path), uri_string:dissect_query(Query)}}; - parse_path(_Scheme, _HostHeader, {absoluteURI, Scheme, Host, Port, Path}) -> - setelement(2, parse_path(undefined, undefined, {abs_path, Path}), {Scheme, Host, Port}); - parse_path(_, _, _) -> - {error, unsupported_uri}. - -else. - parse_path(_Scheme, _HostHeader, {abs_path, FullPath}) -> - Parsed = case binary:split(FullPath, [<<"?">>]) of - [URL] -> {FullPath, split_path(URL), []}; - [URL, Args] -> {FullPath, split_path(URL), split_args(Args)} - end, - {ok, {undefined, undefined, undefined}, Parsed}; - parse_path(Scheme, HostHeader, {absoluteURI, Scheme, Host, Port, Path}) -> - setelement(2, parse_path(Scheme, HostHeader, {abs_path, Path}), {Scheme, Host, Port}); - parse_path(_, _, _) -> - {error, unsupported_uri}. - -endif. --else. - %% same as else branch above. can drop this when only OTP 21+ is supported - parse_path(Scheme, HostHeader, {abs_path, FullPath}) -> - Parsed = case binary:split(FullPath, [<<"?">>]) of - [URL] -> {FullPath, split_path(URL), []}; - [URL, Args] -> {FullPath, split_path(URL), split_args(Args)} - end, - {ok, {undefined, undefined, undefined}, Parsed}; - parse_path(Scheme, HostHeader, {absoluteURI, Scheme, Host, Port, Path}) -> - setelement(2, parse_path(Scheme, HostHeader, {abs_path, Path}), {Scheme, Host, Port}); - parse_path(_, _, _) -> - {error, unsupported_uri}. --endif. + Path = maps:get(path, URIMap, <<>>), + Query = maps:get(query, URIMap, <<>>), + + {ok, {Scheme, Host, Port}, + {Path, split_path(Path), uri_string:dissect_query(Query)}}; +parse_path(_Scheme, _HostHeader, {absoluteURI, Scheme, Host, Port, Path}) -> + setelement(2, parse_path(undefined, undefined, {abs_path, Path}), {Scheme, Host, Port}); +parse_path(_, _, _) -> + {error, unsupported_uri}. split_path(Path) -> [P || P <- binary:split(Path, [<<"/">>], [global]), @@ -841,7 +795,7 @@ handle_event(Mod, Name, EventArgs, ElliArgs) -> try Mod:handle_event(Name, EventArgs, ElliArgs) catch - ?WITH_STACKTRACE(EvClass, EvError, Stacktrace) + EvClass:EvError:Stacktrace -> ?LOG_ERROR("~p:handle_event/3 crashed ~p:~p~n~p", [Mod, EvClass, EvError, Stacktrace]) end. diff --git a/src/elli_util.hrl b/src/elli_util.hrl index 557ec28..2ef7755 100644 --- a/src/elli_util.hrl +++ b/src/elli_util.hrl @@ -1,17 +1,2 @@ - --ifdef(OTP_RELEASE). --include_lib("kernel/include/logger.hrl"). --else. --define(LOG_ERROR(Str), error_logger:error_msg(Str)). --define(LOG_ERROR(Format,Data), error_logger:error_msg(Format, Data)). --define(LOG_INFO(Format,Data), error_logger:info_msg(Format, Data)). --endif. - --ifdef(OTP_RELEASE). --define(WITH_STACKTRACE(T, R, S), T:R:S ->). --else. --define(WITH_STACKTRACE(T, R, S), T:R -> S = erlang:get_stacktrace(),). --endif. - %% Bloody useful -define(IF(Test,True,False), case Test of true -> True; false -> False end). diff --git a/test/elli_tests.erl b/test/elli_tests.erl index 123a9b4..2519907 100644 --- a/test/elli_tests.erl +++ b/test/elli_tests.erl @@ -7,13 +7,8 @@ -define(VTB(T1, T2, LB, UB), time_diff_to_micro_seconds(T1, T2) >= LB andalso time_diff_to_micro_seconds(T1, T2) =< UB). --ifdef(OTP_RELEASE). + -include_lib("kernel/include/logger.hrl"). --else. --define(LOG_ERROR(Str), error_logger:error_msg(Str)). --define(LOG_ERROR(Format,Data), error_logger:error_msg(Format, Data)). --define(LOG_INFO(Format,Data), error_logger:info_msg(Format, Data)). --endif. time_diff_to_micro_seconds(T1, T2) -> erlang:convert_time_unit(