toraritte / knowledge-gaps

All the stuff I don't know, but would like to (or should)
0 stars 0 forks source link

Understand `BackgroundJob` plug and router example in Phoenix Guides #6

Open toraritte opened 6 years ago

toraritte commented 6 years ago

https://hexdocs.pm/phoenix/routing.html#forward

defmodule BackgroundJob.Plug do
  def init(opts), do: opts
  def call(conn, opts) do
    conn
    |> Plug.Conn.assign(:name, Keyword.get(opts, :name, "Background Job"))
    |> BackgroundJob.Router.call(opts)
  end
end

defmodule BackgroundJob.Router do
  use Plug.Router

  plug :match
  plug :dispatch

  get "/", do: send_resp(conn, 200, "Welcome to #{conn.assigns.name}")
  get "/active", do: send_resp(conn, 200, "5 Active Jobs")
  get "/pending", do: send_resp(conn, 200, "3 Pending Jobs")
  match _, do: send_resp(conn, 404, "Not found")
end