~udia/servy

2f71ed94004a5e02834974afc0c52983566868c2 — Alexander Wong 2 years ago 72a156c master
Final OTP Application
4 files changed, 24 insertions(+), 6 deletions(-)

M README.md
M lib/servy.ex
M lib/servy/kickstarter.ex
M mix.exs
M README.md => README.md +12 -0
@@ 1036,3 1036,15 @@ opts = [strategy: :one_for_one, max_restarts: 5, max_seconds: 10]
# These children will be supervised with the one_for_one strategy, allowing 5 restarts within 10 seconds before error occurs.
Supervisor.init(children, opts)
```

### Final OTP Application

An `application` is a first class citizen in Elixir.
See [Application](https://hexdocs.pm/elixir/master/Application.html) for more details.

Application environment configuration can be specified directly in the `mix.exs` file, or by `config/config.exs` files.
  - The `config` directory files are no longer generated by default
  - Configuration settings set in `config` directory are restricted to this project, if the project is a dependency of another application then the contents of `config/config.exs` are never loaded.

Within the application callback module, the `start/2` callback is what is invoked when calling `iex -S mix` or `mix run` and `mix run --no-halt`.


M lib/servy.ex => lib/servy.ex +6 -4
@@ 1,7 1,9 @@
defmodule Servy do
  def hello(name) do
    "Hello, #{name}!"
  use Application

  def start(_type, _args) do
    IO.puts "Starting the application..."
    # {:ok, sup_id} = Servy.Supervisor.start_link()
    Servy.Supervisor.start_link()
  end
end

# IO.puts Servy.hello("Elixir")

M lib/servy/kickstarter.ex => lib/servy/kickstarter.ex +2 -1
@@ 21,7 21,8 @@ defmodule Servy.KickStarter do

    # server_pid = spawn(Servy.HttpServer, :start, [4000])
    # Process.link(server_pid)
    server_pid = spawn_link(Servy.HttpServer, :start, [4000])
    port = Application.get_env(:servy, :port)
    server_pid = spawn_link(Servy.HttpServer, :start, [port])

    Process.register(server_pid, :http_server)
    server_pid

M mix.exs => mix.exs +4 -1
@@ 4,6 4,7 @@ defmodule Servy.MixProject do
  def project do
    [
      app: :servy,
      description: "A humble HTTP server",
      version: "0.1.0",
      elixir: "~> 1.11",
      start_permanent: Mix.env() == :prod,


@@ 14,7 15,9 @@ defmodule Servy.MixProject do
  # Run "mix help compile.app" to learn about applications.
  def application do
    [
      extra_applications: [:logger, :eex]
      extra_applications: [:logger, :eex],
      mod: {Servy, []},
      env: [port: 3000]
    ]
  end