Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

-behaviour({{packageName}}_logic_handler).

-export([handle_request/2]).
-export([handle_request/3]).
{{#authMethods}}
{{#isApiKey}}
-export([authorize_api_key/2]).
Expand All @@ -15,6 +15,6 @@ authorize_api_key(_, _) -> {true, #{}}.
{{/isApiKey}}
{{/authMethods}}

handle_request(OperationID, Req) ->
io:format(user, "Got request to process: ~p~n", [{OperationID, Req}]),
handle_request(OperationID, Req, Context) ->
io:format(user, "Got request to process: ~p~n", [{OperationID, Req, Context}]),
{501, [], <<"Not Implemented">>}.
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
init(_Transport, Req, Opts) ->
{upgrade, protocol, cowboy_rest, Req, Opts}.

rest_init(Req0, [LogicHandler, ValidatorState]) ->
rest_init(Req0, [Operations, LogicHandler, ValidatorState]) ->
{Method, Req} = cowboy_req:method(Req0),
OperationID = {{packageName}}_router:get_operation_id(Method, ?MODULE),
OperationID = maps:get(Method, Operations, undefined),
State = #state{
operation_id = OperationID,
logic_handler = LogicHandler,
Expand Down Expand Up @@ -101,12 +101,13 @@ handle_request_json(
State = #state{
operation_id = OperationID,
logic_handler = LogicHandler,
validator_state = ValidatorState
validator_state = ValidatorState,
context = Context
}
) ->
case {{packageName}}_api:populate_request(OperationID, Req0, ValidatorState) of
{ok, Populated, Req1} ->
case LogicHandler:handle_request(OperationID, Populated) of
case LogicHandler:handle_request(OperationID, Populated, Context) of
{Code, Headers, Body} ->
{{packageName}}_api:validate_response(OperationID, Code, Body , ValidatorState),
PreparedBody = jsx:encode(Body),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
-module({{packageName}}_logic_handler).

{{#hasAuthMethods}}
-type context() :: #{binary() => any()}.
{{/hasAuthMethods}}

{{#authMethods}}
{{#isApiKey}}
-callback authorize_api_key(ApiKey :: binary(), OperationID :: atom()) -> Result :: boolean() | {boolean(), context()}.
{{/isApiKey}}
{{/authMethods}}


-callback handle_request(OperationID :: atom(), Request :: any()) -> {Status :: integer(), [Header :: binary()], Body :: any()}.
-callback handle_request(OperationID :: atom(), Request :: any(), Context :: context()) -> {Status :: integer(), [Header :: binary()], Body :: any()}.
Original file line number Diff line number Diff line change
@@ -1,49 +1,49 @@
-module({{packageName}}_router).

-export([get_paths/1]).
-export([get_operations/0]).
-export([get_operation_id/2]).

get_paths(LogicHandler) ->
ValidatorState = prepare_validator(),
Paths = lists:usort(maps:fold(
fun(_, #{path := Path, handler := Handler}, Acc) ->
[{Path, Handler} | Acc]
PreparedPaths = maps:fold(
fun(Path, #{operations := Operations, handler := Handler}, Acc) ->
[{Path, Handler, Operations} | Acc]
end,
[],
get_operations()
)),
group_paths()
),
[
{'_',
[{P, H, [LogicHandler, ValidatorState]} || {P, H} <- Paths]
[{P, H, [O, LogicHandler, ValidatorState]} || {P, H, O} <- PreparedPaths]
}
].

group_paths() ->
maps:fold(
fun(OperationID, #{path := Path, method := Method, handler := Handler}, Acc) ->
case maps:find(Path, Acc) of
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. С maps:update_with/4 не проще получится?
  2. Зачем ты мешаешь в одном выражении maps:put/3 и выражения для создания мап?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

c maps:update_with/4 не получилось проще

{ok, PathInfo0 = #{operations := Operations0}} ->
Operations = Operations0#{Method => OperationID},
PathInfo = PathInfo0#{operations => Operations},
Acc#{Path => PathInfo};
error ->
Operations = #{Method => OperationID},
PathInfo = #{handler => Handler, operations => Operations},
Acc#{Path => PathInfo}
end
end,
#{},
get_operations()
).

get_operations() ->
#{ {{#apiInfo}}{{#apis}}{{#operations}}{{#operation}}
'{{operationId}}' => #{
path => "{{path}}",
path => "{{basePathWithoutHost}}{{path}}",
method => <<"{{httpMethod}}">>,
handler => '{{classname}}'
}{{#hasMore}},{{/hasMore}}{{/operation}}{{#hasMore}},{{/hasMore}}{{/operations}}{{/apis}}{{/apiInfo}}
}.

get_operation_id(Method, Handler) ->
get_operation_id(Method, Handler, get_operations()).

get_operation_id(Method, Handler, Operations) ->
maps:fold(
fun
(OperationID, #{method := M, handler := H}, _) when H =:= Handler, M =:= Method ->
OperationID;
(_, _, Acc) ->
Acc
end,
undefined,
Operations
).

prepare_validator() ->
R = jsx:decode(element(2, file:read_file(get_swagger_path()))),
jesse_state:new(R, [{default_schema_ver, <<"http://json-schema.org/draft-04/schema#">>}]).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ child_spec(Id, #{
AcceptorsPool = ?DEFAULT_ACCEPTORS_POOLSIZE,
{Transport, TransportOpts} = get_socket_transport(Ip, Port, NetOpts),
LogicHandler = maps:get(logic_handler, Params, ?DEFAULT_LOGIC_HANDLER),
CowboyOpts = get_cowboy_config(LogicHandler),
ExtraOpts = maps:get(cowboy_extra_opts, Params, []),
CowboyOpts = get_cowboy_config(LogicHandler, ExtraOpts),
ranch:child_spec({?MODULE, Id}, AcceptorsPool,
Transport, TransportOpts, cowboy_protocol, CowboyOpts).

Expand All @@ -30,6 +31,22 @@ get_socket_transport(Ip, Port, Options) ->
{ranch_tcp, Opts}
end.

get_cowboy_config(LogicHandler) ->
get_cowboy_config(LogicHandler, ExtraOpts) ->
get_cowboy_config(LogicHandler, ExtraOpts, []).

get_cowboy_config(_LogicHandler, [], Opts) ->
Opts;

get_cowboy_config(LogicHandler, [{env, Env} | Rest], Opts) ->
NewEnv = case proplists:get_value(dispatch, Env) of
undefined -> [get_default_dispatch(LogicHandler) | Env];
_ -> Env
end,
get_cowboy_config(LogicHandler, Rest, [{env, NewEnv} | Opts]);

get_cowboy_config(LogicHandler, [O | Rest], Opts) ->
get_cowboy_config(LogicHandler, Rest, [O | Opts]).

get_default_dispatch(LogicHandler) ->
Paths = swagger_router:get_paths(LogicHandler),
[{env, [{dispatch, cowboy_router:compile(Paths)}]}].
{dispatch, cowboy_router:compile(Paths)}.