Hello,
I d like to be able to exclude some plugs out of a pipeline when i call pipe_through
Simething like this:
pipeline :pipeline_example do
plug OviceWeb.Plugs.FirstPlug
plug OviceWeb.Plugs.SecondPlug
end
pipe_through (:pipeline_example, [exclude: true])
It should build new pipline at compile time with new name that includes whats has been excluded + exclude keyword maybe.
I think starting point is doing some changes to build_pipes/2 found in deps/phoenix/lib/phoenix/router.ex in the official phoenix lib
But i dont know where the match/read is happening(i mean when the pipline is going to ge executed)
Also i d like to hear your opinion about the idea itself maybe there’s an easier way!
What would be excluded from the example above?
In general, I’d solve a situation like this by splitting apart pipeline_example
into the parts that should be common and the parts that aren’t.
3 Likes
Agreed with @al2o3cr
When it comes to composing plugs and structuring pipelines, I’ve found it simpler to frame it as generally additive rather than subtractive so including/extending vs excluding/reducing. Also note that you can “nest” pipelines since the pipeline
macro itself creates a function plug – example courtesy of @michalmuskala. This means you can create the “lowest common denominator” pipeline and extend them in “higher order” pipelines.
If needs must and excluding makes sense for what you’re doing, you could take a look at the underlying Plug library and how to define conditional plugs that use pattern matching to conditionally invoke (or not invoke) a plug.
2 Likes
@al2o3cr sorry it should be like this:
pipe_through (:pipeline_example, [exclude: Web.Plugs.SecondPlug])
But i got what you mean, i moved on with nesting pipelines
@al2o3cr @codeanpeace thanks for you feedback
Thank you, this is great i thought plug definitions happens only at compile time.
Now if i have to create plugs at runtime i will use unplug lib instead of trying creating something new.