I'm creating my first Elixir-Phoenix app. I've written a few plugs that I want to use in multiple controllers, right now there is a lot of code duplication since the Plug code is being repeated in all of my controllers.
My question is, is there a file where I can put all of my Plugs and then access and use them from different controllers?
# somefile to define my plugs
defp load_subject(conn, _) do
    subject = Subject |> Repo.get conn.params["subject_id"]
    assign(conn, :subject, subject)
end
defp load_topic(conn, _) do
    topic = Topic |> Repo.get conn.params["topic_id"]
    conn |> assign :topic, topic
end
Then use them in my controllers:
# First Controller
defmodule MyApp.FirstController do
    use MyApp.Web, :controller
    plug :load_subject
    plug :load_topic
    plug :action
    def some_action(conn, _) do
        # do something
    end
    def another_action(conn, _) do
        # do some other thing
    end
end
# Second Controller
defmodule MyApp.SecondController do
    use MyApp.Web, :controller
    plug :load_subject
    plug :load_topic
    plug :action
    def lame_action(conn, _) do
        # do something
    end
    def cool_action(conn, _) do
        # do some other thing
    end
end
				
                        
You can define the plugs in any module as public functions:
Now you can import it in your controllers and use the plugs:
Alternatively, you can also create a pipeline in your router:
And then
pipe_throughit in the relevant scopes.