2

I want to send some conn info to my Elasticsearch after all controller actions of my project, example: The controller action response, the request params and the endpoint.

What can we do if we need to work with the conn struct returned by the controller after the action is resolved?

1 Answers1

3

I got it creating a plug in my application:

defmodule MyAppWeb.Plugs.RequestLogger do
  @moduledoc false

  alias Plug.Conn

  def init(opts), do: opts

  def call(conn, _opts) do
    IO.puts("Getting conn after all controller actions here " <> Integer.to_string(conn.status))
    conn
  end
end

And setting it in "MyAppWeb.Endpoint" after the "MyAppWeb.Router":

defmodule MyAppWeb.Endpoint do
  # ...
  plug MyAppWeb.Router
  plug MyAppWeb.Plugs.RequestLogger
end