forked from loafle/openapi-generator-original
* Initial erlang generation * Recfactor erlang codegen to make a minimal working example * ft/erlang_codegen Separate handlers by resourse, add minor codegen fixes and refactoring * Test commit * ft/erlang_codegen Modify reouting generation * ft/erlang_codegen Remove parsed request concept. Add minor refactoring and bugfixes * ft/erlang_codegen Use swagger spec from an internal directory instead of a provided path * ft/erlang_codegen Add basic response validation * ft/erlang_codegen Moved all the req validators to a separate file for test needs * ft/erlang_codegen Add basic param validation * Add refactoring: OperationIDs are atoms now Fix schema validation Add todo list * CAPI-23 Add auth context to request handling (#2) * CAPI-23 Fix routing to support different paths in one handler. Add auth context to request handling. Add an opportunity to pass custom middlewares to the server * CAPI-31 Add enum validation and some minor fixes (#4) * CAPI-31 Fix turbo fuck up with additional params (#5) * Capi 23/fix/basic logging (#6) * CAPI-23 Add understandable messages in case of bad requests. Add specs to shut up dialyzer and add some minor code refactoring * CAPI-23 Fix missed bracket in auth module (#7)
65 lines
2.0 KiB
Plaintext
65 lines
2.0 KiB
Plaintext
-module({{packageName}}_server).
|
|
|
|
|
|
-define(DEFAULT_ACCEPTORS_POOLSIZE, 100).
|
|
-define(DEFAULT_LOGIC_HANDLER, {{packageName}}_default_logic_handler).
|
|
|
|
-export([child_spec/2]).
|
|
|
|
-spec child_spec( ID :: any(), #{
|
|
ip => inet:ip_address(),
|
|
port => inet:port_number(),
|
|
net_opts => []
|
|
}) -> supervisor:child_spec().
|
|
|
|
child_spec(ID, #{
|
|
ip := IP ,
|
|
port := Port,
|
|
net_opts := NetOpts
|
|
} = Params) ->
|
|
AcceptorsPool = ?DEFAULT_ACCEPTORS_POOLSIZE,
|
|
{Transport, TransportOpts} = get_socket_transport(IP, Port, NetOpts),
|
|
LogicHandler = maps:get(logic_handler, Params, ?DEFAULT_LOGIC_HANDLER),
|
|
ExtraOpts = maps:get(cowboy_extra_opts, Params, []),
|
|
CowboyOpts = get_cowboy_config(LogicHandler, ExtraOpts),
|
|
ranch:child_spec({?MODULE, ID}, AcceptorsPool,
|
|
Transport, TransportOpts, cowboy_protocol, CowboyOpts).
|
|
|
|
get_socket_transport(IP, Port, Options) ->
|
|
Opts = [
|
|
{ip, IP},
|
|
{port, Port}
|
|
],
|
|
case {{packageName}}_utils:get_opt(ssl, Options) of
|
|
SslOpts = [_|_] ->
|
|
{ranch_ssl, Opts ++ SslOpts};
|
|
undefined ->
|
|
{ranch_tcp, Opts}
|
|
end.
|
|
|
|
get_cowboy_config(LogicHandler, ExtraOpts) ->
|
|
get_cowboy_config(LogicHandler, ExtraOpts, get_default_opts(LogicHandler)).
|
|
|
|
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, store_key(env, NewEnv, Opts));
|
|
|
|
get_cowboy_config(LogicHandler, [{Key, Value}| Rest], Opts) ->
|
|
get_cowboy_config(LogicHandler, Rest, store_key(Key, Value, Opts)).
|
|
|
|
get_default_dispatch(LogicHandler) ->
|
|
Paths = {{packageName}}_router:get_paths(LogicHandler),
|
|
{dispatch, cowboy_router:compile(Paths)}.
|
|
|
|
get_default_opts(LogicHandler) ->
|
|
[{env, [get_default_dispatch(LogicHandler)]}].
|
|
|
|
store_key(Key, Value, Opts) ->
|
|
lists:keystore(Key, 1, Opts, {Key, Value}).
|