Muliple/Varied plug pipelines - best practice

466 views
Skip to first unread message

Larry Weya

unread,
Aug 31, 2015, 4:27:47 AM8/31/15
to elixir-lang-talk
From my understanding plugs within a router are executed sequentially one by one and plugs like the CSRF plug allow options to be defined within the route to skip the CSRF check. How would I go about defining a different plug pipeline for a group of routes. The best I've been able to figure out is to define a separate router and have the main router forward matching requests to it. The (minor) issue I have with this is that if forces me to have a prefix for those routes which I can live with but would like to know if there is another way to do it.

José Valim

unread,
Aug 31, 2015, 5:00:54 AM8/31/15
to elixir-l...@googlegroups.com
You will have better luck regarding Phoenix questions in Phoenix mailing list:https://groups.google.com/forum/?utm_medium=email&utm_source=footer#!forum/phoenix-talk :)



José Valim
Skype: jv.ptec
Founder and Director of R&D

On Mon, Aug 31, 2015 at 10:27 AM, Larry Weya <larr...@gmail.com> wrote:
From my understanding plugs within a router are executed sequentially one by one and plugs like the CSRF plug allow options to be defined within the route to skip the CSRF check. How would I go about defining a different plug pipeline for a group of routes. The best I've been able to figure out is to define a separate router and have the main router forward matching requests to it. The (minor) issue I have with this is that if forces me to have a prefix for those routes which I can live with but would like to know if there is another way to do it.

--
You received this message because you are subscribed to the Google Groups "elixir-lang-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-ta...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-talk/d86d8223-bb3b-4cc0-8f6c-8f983dc1d01f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Larry Weya

unread,
Aug 31, 2015, 11:58:50 AM8/31/15
to elixir-lang-talk
I'm actually talking specifically about Plug and not Phoenix, sorry if I didnt make that clear.

José Valim

unread,
Aug 31, 2015, 12:06:26 PM8/31/15
to elixir-l...@googlegroups.com
Ah, ok. Strictly speaking, the Plug router does not support multiple pipelines. However, because plugs are just functions, you can easily wire up by creating one module per pipeline:

defmodule MyPipeline do
  use Plug.Builder

  plug :foo
  plug :bar
  plug :baz
  plug :dispatch

  def dispatch(conn, opts) do
    opts[:route].(conn)
  end
end

Then you can call it for the relevant routes in your route:

get "/foo/bar" do
  MyPipeline.call(conn, route: fn conn ->
    # Your route code
  end)
end

You can even move it to a private function and call it as my_pipeline(conn, fn conn -> ... end) passing the connection. That's pretty much what Phoenix does (it does support multiple pipelines, that's why I got confused by your question), except Phoenix hides the pipeline call from you.



José Valim
Skype: jv.ptec
Founder and Director of R&D

--
You received this message because you are subscribed to the Google Groups "elixir-lang-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-ta...@googlegroups.com.

Larry Weya

unread,
Sep 1, 2015, 9:52:13 AM9/1/15
to elixir-lang-talk, jose....@plataformatec.com.br
Exactly what I was looking for, thanks.
Reply all
Reply to author
Forward
Message has been deleted
0 new messages