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
6 changes: 3 additions & 3 deletions src/minirest.appup.src
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
%%-*-: erlang -*-
{"0.3.7",
{"0.3.9",
[
{<<"0.3.[0-6]">>,
{<<"0\\.3\\.[0-8]">>,
[ {load_module, minirest, brutal_purge, soft_purge, []},
{load_module, minirest_handler, brutal_purge, soft_purge, []}
]
},
{<<".*">>, []}
],
[
{<<"0.3.[0-6]">>,
{<<"0\\.3\\.[0-8]">>,
[ {load_module, minirest, brutal_purge, soft_purge, []},
{load_module, minirest_handler, brutal_purge, soft_purge, []}
]
Expand Down
25 changes: 19 additions & 6 deletions src/minirest.erl
Original file line number Diff line number Diff line change
Expand Up @@ -53,27 +53,34 @@ start_http(ServerName, Options, Handlers) ->
{ok, _} -> ok;
{error, {already_started, _}} -> ok;
{error, eaddrinuse} ->
?LOG(error, "Start ~s listener on ~p unsuccessfully: the port is occupied", [ServerName, get_port(Options)]),
?LOG(error, "Start ~s listener on ~s unsuccessfully: the port is occupied", [ServerName, format_bind(Options)]),
error(eaddrinuse);
{error, Any} ->
?LOG(error, "Start ~s listener on ~p unsuccessfully: ~0p", [ServerName, get_port(Options), Any]),
?LOG(error, "Start ~s listener on ~s unsuccessfully: ~0p", [ServerName, format_bind(Options), Any]),
error(Any)
end,
io:format("Start ~s listener on ~p successfully.~n", [ServerName, get_port(Options)]).
io:format("Start ~s listener on ~s successfully.~n", [ServerName, format_bind(Options)]).

-spec(start_https(atom(), list(), list()) -> {ok, pid()}).
start_https(ServerName, Options, Handlers) ->
Dispatch = cowboy_router:compile([{'_', handlers(Handlers)}]),
case cowboy:start_tls(ServerName, Options, #{env => #{dispatch => Dispatch}}) of
{ok, _} -> ok;
{error, eaddrinuse} ->
?LOG(error, "Start ~s listener on ~p unsuccessfully: the port is occupied", [ServerName, get_port(Options)]),
?LOG(error, "Start ~s listener on ~s unsuccessfully: the port is occupied", [ServerName, format_bind(Options)]),
error(eaddrinuse);
{error, Any} ->
?LOG(error, "Start ~s listener on ~p unsuccessfully: ~0p", [ServerName, get_port(Options), Any]),
?LOG(error, "Start ~s listener on ~s unsuccessfully: ~0p", [ServerName, format_bind(Options), Any]),
error(Any)
end,
io:format("Start ~s listener on ~p successfully.~n", [ServerName, get_port(Options)]).
io:format("Start ~s listener on ~s successfully.~n", [ServerName, format_bind(Options)]).

format_bind(Options) ->
Port = get_port(Options),
case get_ip(Options) of
undefined -> integer_to_binary(Port);
IP -> iolist_to_binary([IP, ":", integer_to_binary(Port)])
end.

-spec(stop_http(atom()) -> ok | {error, any()}).
stop_http(ServerName) ->
Expand All @@ -82,6 +89,12 @@ stop_http(ServerName) ->
get_port(#{socket_opts := SocketOpts}) ->
proplists:get_value(port, SocketOpts, 18083).

get_ip(#{socket_opts := SocketOpts}) ->
case proplists:get_value(ip, SocketOpts) of
undefined -> undefined;
IP -> inet:ntoa(IP)
end.

map({Prefix, MFArgs}) ->
map({Prefix, MFArgs, []});
map({Prefix, MFArgs, Options}) ->
Expand Down