Question
The documentation for Router
mentions that all routing macros (get
, post
, etc…) can take a plug_opts
and options
arguments.
From what I understand, when doing this:
get("/foo/bar", FooController, :bar, [a: 1, b: 2])
The Router will call the function bar
in FooController with a Conn
and its params.
However, the extra options ([a: 1, b:2]
in my cases) are not available to the function.
Should they be ? Is there a way to get those params ?
Side note
To give some context, I would like to be able to write something like this:
get "/foo/bar", FooController, :bar, [role: "some_role"]
get "/foo/baz", FooController, :baz, [role: "some_other_role"]
And I want my controller’s function to be able to receive this “role” field, to test if the conn contains the proper role.
Since the [role: ...]
plug options would be different for each route, I don’t think I can use a pipeline
for that, since I would not be able to initialize the pipeline’s options:
pipeline "check_role" do
plug MyPlugThatCheckRole, [role: ??????] # How do I configure
end
scope "/foo" do
pipe_through "check_role"
get "/foo/bar", FooController, :bar, ????? # How would I pass the role here ?
get "/foo/baz", FooController, :baz, ????? # How would I pass the role here ?
end
defmodule MyPlugThatChecksRole do
def init(???) do
# This is only called once when the plug is defined in a pipeline, right ?
end
def call(conn, opts) do
## Is there a way to send the options from the `get "/foo/bar`, FooController, :bar, ....` line here ???
end
end