Forward options from a Plug.Router to another Plug.Router
Background
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.
Code
This is the main router. It receives requests and decides where to forward them.
defmodule MyApp.Web.Router do
use Plug.Router
plug(:match)
plug(:dispatch)
#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??
end
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
end
But this doesn't work because I can't put a forward
inside a call
.
So how do I achieve my objective using forward
?
elixir plug
add a comment |
Background
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.
Code
This is the main router. It receives requests and decides where to forward them.
defmodule MyApp.Web.Router do
use Plug.Router
plug(:match)
plug(:dispatch)
#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??
end
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
end
But this doesn't work because I can't put a forward
inside a call
.
So how do I achieve my objective using forward
?
elixir plug
add a comment |
Background
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.
Code
This is the main router. It receives requests and decides where to forward them.
defmodule MyApp.Web.Router do
use Plug.Router
plug(:match)
plug(:dispatch)
#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??
end
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
end
But this doesn't work because I can't put a forward
inside a call
.
So how do I achieve my objective using forward
?
elixir plug
Background
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.
Code
This is the main router. It receives requests and decides where to forward them.
defmodule MyApp.Web.Router do
use Plug.Router
plug(:match)
plug(:dispatch)
#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??
end
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
end
But this doesn't work because I can't put a forward
inside a call
.
So how do I achieve my objective using forward
?
elixir plug
elixir plug
asked Nov 21 '18 at 14:58


Flame_PhoenixFlame_Phoenix
6,0341778162
6,0341778162
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
There is an option adding a top-level plug that will store the father options on private
and you can fetch that on children call
.
Something like:
defmodule Example.Router do
def init(opts), do: opts
def call(conn, options) do
Example.RouterMatch.call(Plug.Conn.put_private(conn, :father_options, options), Example.RouterMatch.init(options))
end
end
defmodule Example.RouterMatch do
use Plug.Router
plug :match
plug :dispatch
forward "/check", to: Example.Route.Check
forward "/dispatch", to: Example.Plug.Dispatch
end
Then you can fetch options on the conn in Example.Route.Check.call/2
.
The issue with this approach is that it is not forwarding anything. I want to pass options when I forward the request. Having a top level router won't help with that.
– Flame_Phoenix
Nov 22 '18 at 8:14
1
Yes, it is not forwarding but making it available on the children. After reading the plug code I don't think that it is possible and because that I added this suggestion.
– Marcos Tapajós
Nov 22 '18 at 12:15
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53414813%2fforward-options-from-a-plug-router-to-another-plug-router%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
There is an option adding a top-level plug that will store the father options on private
and you can fetch that on children call
.
Something like:
defmodule Example.Router do
def init(opts), do: opts
def call(conn, options) do
Example.RouterMatch.call(Plug.Conn.put_private(conn, :father_options, options), Example.RouterMatch.init(options))
end
end
defmodule Example.RouterMatch do
use Plug.Router
plug :match
plug :dispatch
forward "/check", to: Example.Route.Check
forward "/dispatch", to: Example.Plug.Dispatch
end
Then you can fetch options on the conn in Example.Route.Check.call/2
.
The issue with this approach is that it is not forwarding anything. I want to pass options when I forward the request. Having a top level router won't help with that.
– Flame_Phoenix
Nov 22 '18 at 8:14
1
Yes, it is not forwarding but making it available on the children. After reading the plug code I don't think that it is possible and because that I added this suggestion.
– Marcos Tapajós
Nov 22 '18 at 12:15
add a comment |
There is an option adding a top-level plug that will store the father options on private
and you can fetch that on children call
.
Something like:
defmodule Example.Router do
def init(opts), do: opts
def call(conn, options) do
Example.RouterMatch.call(Plug.Conn.put_private(conn, :father_options, options), Example.RouterMatch.init(options))
end
end
defmodule Example.RouterMatch do
use Plug.Router
plug :match
plug :dispatch
forward "/check", to: Example.Route.Check
forward "/dispatch", to: Example.Plug.Dispatch
end
Then you can fetch options on the conn in Example.Route.Check.call/2
.
The issue with this approach is that it is not forwarding anything. I want to pass options when I forward the request. Having a top level router won't help with that.
– Flame_Phoenix
Nov 22 '18 at 8:14
1
Yes, it is not forwarding but making it available on the children. After reading the plug code I don't think that it is possible and because that I added this suggestion.
– Marcos Tapajós
Nov 22 '18 at 12:15
add a comment |
There is an option adding a top-level plug that will store the father options on private
and you can fetch that on children call
.
Something like:
defmodule Example.Router do
def init(opts), do: opts
def call(conn, options) do
Example.RouterMatch.call(Plug.Conn.put_private(conn, :father_options, options), Example.RouterMatch.init(options))
end
end
defmodule Example.RouterMatch do
use Plug.Router
plug :match
plug :dispatch
forward "/check", to: Example.Route.Check
forward "/dispatch", to: Example.Plug.Dispatch
end
Then you can fetch options on the conn in Example.Route.Check.call/2
.
There is an option adding a top-level plug that will store the father options on private
and you can fetch that on children call
.
Something like:
defmodule Example.Router do
def init(opts), do: opts
def call(conn, options) do
Example.RouterMatch.call(Plug.Conn.put_private(conn, :father_options, options), Example.RouterMatch.init(options))
end
end
defmodule Example.RouterMatch do
use Plug.Router
plug :match
plug :dispatch
forward "/check", to: Example.Route.Check
forward "/dispatch", to: Example.Plug.Dispatch
end
Then you can fetch options on the conn in Example.Route.Check.call/2
.
edited Nov 21 '18 at 15:53
answered Nov 21 '18 at 15:30
Marcos TapajósMarcos Tapajós
1915
1915
The issue with this approach is that it is not forwarding anything. I want to pass options when I forward the request. Having a top level router won't help with that.
– Flame_Phoenix
Nov 22 '18 at 8:14
1
Yes, it is not forwarding but making it available on the children. After reading the plug code I don't think that it is possible and because that I added this suggestion.
– Marcos Tapajós
Nov 22 '18 at 12:15
add a comment |
The issue with this approach is that it is not forwarding anything. I want to pass options when I forward the request. Having a top level router won't help with that.
– Flame_Phoenix
Nov 22 '18 at 8:14
1
Yes, it is not forwarding but making it available on the children. After reading the plug code I don't think that it is possible and because that I added this suggestion.
– Marcos Tapajós
Nov 22 '18 at 12:15
The issue with this approach is that it is not forwarding anything. I want to pass options when I forward the request. Having a top level router won't help with that.
– Flame_Phoenix
Nov 22 '18 at 8:14
The issue with this approach is that it is not forwarding anything. I want to pass options when I forward the request. Having a top level router won't help with that.
– Flame_Phoenix
Nov 22 '18 at 8:14
1
1
Yes, it is not forwarding but making it available on the children. After reading the plug code I don't think that it is possible and because that I added this suggestion.
– Marcos Tapajós
Nov 22 '18 at 12:15
Yes, it is not forwarding but making it available on the children. After reading the plug code I don't think that it is possible and because that I added this suggestion.
– Marcos Tapajós
Nov 22 '18 at 12:15
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53414813%2fforward-options-from-a-plug-router-to-another-plug-router%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown