【问题标题】:How can i get the conn struct after Phoenix controller actionPhoenix 控制器操作后如何获取 conn 结构
【发布时间】:2020-01-08 04:14:44
【问题描述】:

我想在我的项目的所有控制器操作之后向我的 Elasticsearch 发送一些连接信息,例如:控制器操作响应、请求参数和端点。

如果我们需要在动作解决后处理控制器返回的conn结构,我们该怎么办?

【问题讨论】:

  • 请提供一些上下文,例如源代码或伪源代码,显示您想要做什么以及期望得到什么。

标签: elixir phoenix-framework plug


【解决方案1】:

我在我的应用程序中创建了一个插件:

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

并将其设置在“MyAppWeb.Router”之后的“MyAppWeb.Endpoint”中:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多