elixir-plug / plug_cowboy

Plug adapter for the Cowboy web server

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Outdated example in the documentation?

ryanzidago opened this issue · comments

Elixir 1.10.2 (compiled with Erlang/OTP 21)

Mix 1.10.2 (compiled with Erlang/OTP 21)

Plug 2.1

Hello all,

Should I open a quick pull-request to update the documentation?

When I tried to add the Plug to my application's Supervision tree as specified by the documentation on Hex:

children = [
  {Plug.Cowboy, scheme: :http, plug: MyApp, options: [port: 4040]}
]

Supervisor.start_link(children, strategy: :one_for_one)

I got the following error message after running the command mix run --no-halt:

20:00:38.644 [info]  Starting Concise application.

20:00:38.689 [info]  Application concise exited: Concise.Application.start(:normal, []) returned an error: shutdown: failed to start child: {:ranch_listener_sup, ConcisePlug.HTTP}
    ** (EXIT) shutdown: failed to start child: :ranch_acceptors_sup
        ** (EXIT) :badarg
** (Mix) Could not start application concise: Concise.Application.start(:normal, []) returned an error: shutdown: failed to start child: {:ranch_listener_sup, ConcisePlug.HTTP}
    ** (EXIT) shutdown: failed to start child: :ranch_acceptors_sup
        ** (EXIT) :badarg

However, after following the instructions provided by the project's README, it worked out fine at the end.

children = [
      Plug.Cowboy.child_spec(scheme: :http, plug: MyRouter, port: 4001)
    ]

    # See https://hexdocs.pm/elixir/Supervisor.html
    # for other strategies and supported options
    opts = [strategy: :one_for_one, name: MyApp.Supervisor]
    Supervisor.start_link(children, opts)

Which Plug.Cowboy version are you using? I fixed a bug related to this yesterday. So maybe it will work on master?

In any case, we now prefer the approach without the options: key, so a PR to update the docs is definitely welcome!

I am using plug_cowboy 2.1.3.

Then, I'll open up a PR and we'll see from there.

So I had this in the project root:

defmodule Example do
  def init(opts), do: opts
  def call(conn, _), do: Plug.Conn.send_resp(200, "ok")
end

children = [{Plug.Cowboy, scheme: :http, plug: Example, options: [port: 4001]}]
Supervisor.start_link(children, strategy: :one_for_one)

as well as this:

defmodule Example do
  def init(opts), do: opts
end

children = [{Plug.Cowboy, scheme: :http, plug: Example, port: 4001}]
Supervisor.start_link(children, strategy: :one_for_one)

and ran it with mix run example.exs and both worked.

Naturally right now as I'm trying to reproduce step by step the error it works ..!
I might have missed something somewhere earlier on.

Well, thanks for checking this issue with me.

I'll close it but if needed, I'll happily create a pull-request to update the documentation regarding adding the Plug to the Supervision tree as specified in the README.

You were probably in an earlier version. A PR is definitely welcome!