Use proper plug system when calling controller action
This commit is contained in:
parent
03027baa11
commit
8c3b9f73a1
3 changed files with 16 additions and 13 deletions
11
lib/controller_plug.ex
Normal file
11
lib/controller_plug.ex
Normal file
|
@ -0,0 +1,11 @@
|
|||
defmodule ControllerPlug do
|
||||
@behaviour Plug
|
||||
|
||||
@impl Plug
|
||||
def init(action), do: action
|
||||
|
||||
@impl Plug
|
||||
def call(conn, action) do
|
||||
action.(conn, conn.params)
|
||||
end
|
||||
end
|
|
@ -1,6 +1,5 @@
|
|||
defmodule Feeniks.Router do
|
||||
import PlugHelpers
|
||||
import RouterHelpers
|
||||
|
||||
@behaviour Plug
|
||||
|
||||
|
@ -16,7 +15,9 @@ defmodule Feeniks.Router do
|
|||
&__MODULE__.accepts_json/1
|
||||
])
|
||||
|
||||
@index @common_browser ++ [build_action(&Feeniks.Controller.index/1)]
|
||||
@index_action module_plug(ControllerPlug, &Feeniks.Controller.index/2)
|
||||
@index @common_browser ++ [@index_action]
|
||||
@index_api @common_api ++ [@index_action]
|
||||
|
||||
@impl Plug
|
||||
def init(opts), do: opts
|
||||
|
@ -40,14 +41,12 @@ defmodule Feeniks.Router do
|
|||
|
||||
defp match(conn, "GET", _) do
|
||||
conn
|
||||
|> common_browser()
|
||||
|> Feeniks.Controller.index(conn.params)
|
||||
|> run_plugs(@index)
|
||||
end
|
||||
|
||||
defp match(conn, "POST", _) do
|
||||
conn
|
||||
|> run_plugs(@common_api)
|
||||
|> Feeniks.Controller.index(conn.params)
|
||||
|> run_plugs(@index_api)
|
||||
end
|
||||
|
||||
defp match(conn, _, _) do
|
||||
|
|
|
@ -1,7 +0,0 @@
|
|||
defmodule RouterHelpers do
|
||||
@spec build_action((Plug.Conn.t(), Plug.Conn.params() -> Plug.Conn.t())) ::
|
||||
(Plug.Conn.t() -> Plug.Conn.t())
|
||||
def build_action(action) do
|
||||
fn conn -> action.(conn, conn.params) end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue