
erlang-server.router.mustache Maven / Gradle / Ivy
-module({{packageName}}_router).
-export([get_paths/1]).
-type method() :: binary().
-type operations() :: #{method() => {{packageName}}_api:operation_id()}.
-type init_opts() :: {operations(), module()}.
-export_type([init_opts/0]).
-spec get_paths(LogicHandler :: module()) -> cowboy_router:routes().
get_paths(LogicHandler) ->
PreparedPaths = maps:fold(
fun(Path, #{operations := Operations, handler := Handler}, Acc) ->
[{Path, Handler, Operations} | Acc]
end, [], group_paths()
),
[{'_', [{P, H, {O, LogicHandler}} || {P, H, O} <- PreparedPaths]}].
group_paths() ->
maps:fold(
fun(OperationID, #{servers := Servers, base_path := BasePath, path := Path,
method := Method, handler := Handler}, Acc) ->
FullPaths = build_full_paths(Servers, BasePath, Path),
merge_paths(FullPaths, OperationID, Method, Handler, Acc)
end, #{}, get_operations()).
build_full_paths([], BasePath, Path) ->
[lists:append([BasePath, Path])];
build_full_paths(Servers, _BasePath, Path) ->
[lists:append([Server, Path]) || Server <- Servers ].
merge_paths(FullPaths, OperationID, Method, Handler, Acc) ->
lists:foldl(
fun(Path, Acc0) ->
case maps:find(Path, Acc0) of
{ok, PathInfo0 = #{operations := Operations0}} ->
Operations = Operations0#{Method => OperationID},
PathInfo = PathInfo0#{operations => Operations},
Acc0#{Path => PathInfo};
error ->
Operations = #{Method => OperationID},
PathInfo = #{handler => Handler, operations => Operations},
Acc0#{Path => PathInfo}
end
end, Acc, FullPaths).
get_operations() ->
#{ {{#apiInfo}}{{#apis}}{{#operations}}{{#operation}}
'{{operationIdOriginal}}' => #{
servers => [{{#servers}}"{{{url}}}"{{^-last}},{{/-last}}{{/servers}}],
base_path => "{{{basePathWithoutHost}}}",
path => "{{{path}}}",
method => <<"{{httpMethod}}">>,
handler => '{{classname}}'
}{{^-last}},{{/-last}}{{/operation}}{{^-last}},{{/-last}}{{/operations}}{{/apis}}{{/apiInfo}}
}.
© 2015 - 2025 Weber Informatics LLC | Privacy Policy