plug

How do you run middleware functions post response in Phoenix framework?

孤者浪人 提交于 2021-01-27 22:18:41
问题 I'm developing a simple website in Elixir with Phoenix. I'd like to add some custom middleware that runs after a response has been generated. For example, in order to log the total number of bytes in each response I'd like to have a Plug like this defmodule HelloWeb.Plugs.ByteLogger do import Plug.Conn require Logger def init(default), do: default def call(conn, default) do log("bytes sent: #{String.length(conn.resp_body)}") end end Trying to use this plug in one of the Phoenix pipelines in

Is there a way to have a Phoenix Plug just for one route?

∥☆過路亽.° 提交于 2020-05-15 08:11:44
问题 In Phoenix I have my routes as follow : scope "/", ManaWeb do pipe_through [:browser, :auth] get "/register", RegistrationController, :new post "/register", RegistrationController, :register end However I would like to set a Plug for the last route (POST). How would I go about that with current tools ? 回答1: As it is stated in the documentation for Phoenix.Router.pipeline/2 Every time pipe_through/1 is called, the new pipelines are appended to the ones previously given. That said, this would

HMAC, Elixir, Plug.Conn (trying to call read_body more than once)

六月ゝ 毕业季﹏ 提交于 2019-12-10 09:44:46
问题 I'm struggling with an issue where something is reading the body of an http request before Plug.Parsers.JSON gets it in the pipeline. Because of this, read_body in the plug for json times out--you can't read the body twice. We have an HMAC implementation in an earlier plug in our pipeline and it reads the body in some cases. Is there a pattern for how use of the body is to behave in Plug? I mean, if we can only read it once, and it has to be decoded in Plug.Parsers.JSON, well...it's not going

HMAC, Elixir, Plug.Conn (trying to call read_body more than once)

安稳与你 提交于 2019-12-06 01:22:22
I'm struggling with an issue where something is reading the body of an http request before Plug.Parsers.JSON gets it in the pipeline. Because of this, read_body in the plug for json times out--you can't read the body twice. We have an HMAC implementation in an earlier plug in our pipeline and it reads the body in some cases. Is there a pattern for how use of the body is to behave in Plug? I mean, if we can only read it once, and it has to be decoded in Plug.Parsers.JSON, well...it's not going to work. Follow on question. Do we need to include the request body when we generate the HMAC hash? I