When you use Mix to create an application, it always adds a root supervisor file to the project. Note how the 'child spec' array in the supervise function is empty.
app.ex:
defmodule App.Supervisor do
use Supervisor
def start_link do
Supervisor.start_link(__MODULE__, :ok)
end
def init(args) do
supervise([], [strategy: :one_for_one])
end
end
An entry point for the application is also created for you. Working through some of the examples I found online, I wrote the following:
defmodule App do
def start(_type, _args) do
dispatch = :cowboy_router.compile([
{
:_,
[
# Simple JSON test.
{"/test", app.Handle.test, []},
]
}
])
{:ok, _} = :cowboy.start_http(
:http,
100,
[{:port, 8080}],
[{ :env, [{:dispatch, dispatch}]}]
)
App.Supervisor.start_link()
end
end
This application works, but it also works if I remove the call to App.Supervisor.start_link() in App.start().
So what's the supervisor for in this case? If the child spec for the supervisor is empty, then what's the point of it?
For example, in the Elixir example found here - https://github.com/IdahoEv/cowboy-elixir-example/blob/master/lib/cowboy_elixir_example.ex - You can see that the call to start the supervisor is commented out on line 65.
But in an official Cowboy Erlang example, this file - https://github.com/ninenines/cowboy/blob/master/examples/hello_world/src/hello_world_app.erl - Creates a similar root supervisor with no child spec, and then calls it in the main application file here, on line 22 - https://github.com/ninenines/cowboy/blob/master/examples/hello_world/src/hello_world_app.erl