Forward options from a Plug.Router to another Plug.Router


I have a Plug.Router application that receives some options. I need to pass these options to other plugs via forward but I don’t know how to do it.


This is the main router. It receives requests and decides where to forward them.

defmodule MyApp.Web.Router do
  use Plug.Router


  #Here I check that I get options! 
  def init(father_opts), do: IO.puts("#{__MODULE__} => #{inspect father_opts}")

  forward "/api/v1", to: MyApp.Web.Route.API.V1, init_opts: father_opts??

As you can probably guess, this won’t work. I want my forward call to access the father_opts this router is receiving, but I can’t access them.

At first I though about the following code snippet:

def init(opts), do: opts
def call(conn, father_opts) do
  forward "/api/v1", to: MyApp.Web.Route.API.V1, init_opts: father_opts

But this doesn’t work because I can’t put a forward inside a call.

So how do I achieve my objective using forward?

You should be able to just call the MyApp.Web.Route.API.V1 init/call functions directly, that’s basically all forward does (except forward has to be in the router for ease of use).

So I can’t just pass these parameters using forward, right? thanks for the info!

1 Like