Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge inets service configuration with simple_bridge configuration #83

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
31 changes: 30 additions & 1 deletion src/inets_bridge_modules/inets_simple_bridge_sup.erl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ init([]) ->
case proplists:get_value(httpd, Services) of
undefined ->
build_config(HasMimetypes);
_ -> ok
HttpdConfig ->
merge_config(HasMimetypes, HttpdConfig)
end
end,

Expand Down Expand Up @@ -81,6 +82,34 @@ build_config(HasMimetypes) ->
application:set_env(inets, services, NewServices)
end.

merge_config(HasMimetypes, HttpdConfig) ->
io:format("Configuration for inets httpd exists, merging with simple_bridge.config...~n"),
{Address, Port} = simple_bridge_util:get_address_and_port(inets),
{DocRoot, StaticPaths} = simple_bridge_util:get_docroot_and_static_paths(inets),
io:format("Starting Inets Server at ~p:~p~n", [Address, Port]),
io:format("Static Paths: ~p~nDocument Root for Static: ~s~n", [StaticPaths, DocRoot]),

Modules = proplists:get_value(modules, HttpdConfig, []),

DefaultHttpd = [
{bind_address, simple_bridge_util:parse_ip(Address)},
{port, Port},
{server_name, "simple_bridge_inets"},
{server_root, "."},
{document_root, DocRoot},
{modules, lists:append(Modules, [simple_bridge_util:get_anchor_module(inets)])},
{mime_types, build_mimetypes(HasMimetypes)}
],

HttpdConfig0 = proplists:delete(modules, HttpdConfig),

MergedHttpd = lists:umerge(lists:sort(HttpdConfig0), lists:sort(DefaultHttpd)),

{ok, Services} = application:get_env(inets, services),

NewServices = lists:keyreplace(httpd, 1, Services, {httpd, MergedHttpd}),
application:set_env(inets, services, NewServices).

build_mimetypes(true = _HasMimetypes) ->
[{binary_to_list(Ext), binary_to_list(hd(mimetypes:ext_to_mimes(Ext)))}
|| Ext <- mimetypes:extensions()];
Expand Down