View Source Ewebmachine.Builder.Handlers (ewebmachine v2.3.2)
use
this module will use Plug.Builder
(so a plug pipeline
described with the plug module_or_function_plug
macro), but gives
you an :add_handler
local function plug which adds to the conn
the locally defined ewebmachine handlers (see Ewebmachine.Handlers
).
So :
- Construct your automate decision handler through multiple
:add_handler
plugs - Pipe the plug
Ewebmachine.Plug.Run
to run the HTTP automate which will call these handlers to take decisions. - Pipe the plug
Ewebmachine.Plug.Send
to send and halt any conn previsously passed through an automate run.
To define handlers, use the following helpers :
- the handler specific macros (like
Ewebmachine.Builder.Handlers.resource_exists/1
) - the macro
defh/2
to define any helpers, usefull for body producing handlers or to have multiple function clauses - in handler implementation
conn
andstate
binding are available - the response of the handler implementation is wrapped, so that
returning
:my_response
is the same as returning{:my_response,conn,state}
Below a full example :
defmodule MyJSONApi do
use Ewebmachine.Builder.Handlers
plug :cors
plug :add_handlers, init: %{}
content_types_provided do: ["application/json": :to_json]
defh to_json, do: Poison.encode!(state[:json_obj])
defp cors(conn,_), do:
put_resp_header(conn,"Access-Control-Allow-Origin","*")
end
defmodule GetUser do
use Ewebmachine.Builder.Handlers
plug MyJSONApi
plug :add_handlers
plug Ewebmachine.Plug.Run
plug Ewebmachine.Plug.Send
resource_exists do:
pass( !is_nil(user=DB.User.get(conn.params["q"])), json_obj: user)
end
defmodule GetOrder do
use Ewebmachine.Builder.Handlers
plug MyJSONApi
plug :add_handlers
plug Ewebmachine.Plug.Run
plug Ewebmachine.Plug.Send
resource_exists do:
pass(!is_nil(order=DB.Order.get(conn.params["q"])), json_obj: order)
end
defmodule API do
use Plug.Router
plug :match
plug :dispatch
get "/get/user", do: GetUser.call(conn,[])
get "/get/order", do: GetOrder.call(conn,[])
end
Summary
Functions
define a resource handler function as described at
Ewebmachine.Handlers
.
Shortcut macro for : {response,var!(conn),Enum.into(update_state,var!(state))}
Functions
define a resource handler function as described at
Ewebmachine.Handlers
.
Since there is a specific macro in this module for each handler, this macro is useful :
- to define body producing and body processing handlers (the one
referenced in the response of
Ewebmachine.Handlers.content_types_provided/2
orEwebmachine.Handlers.content_types_accepted/2
) - to explicitly take the
conn
and thestate
parameter, which allows you to add guards and pattern matching for instance to define multiple clauses for the handler
defh to_html, do: "hello you"
defh from_json, do: pass(:ok, json: Poison.decode!(read_body conn))
defh resources_exists(conn,%{obj: obj}) when obj !== nil, do: true
defh resources_exists(conn,_), do: false
Shortcut macro for : {response,var!(conn),Enum.into(update_state,var!(state))}
use it if your handler wants to add some value to a collectable state (a map for instance), but using default "conn" current binding.
for instance a resources_exists implementation "caching" the result in the state could be :
pass (user=DB.get(state.id)) != nil, current_user: user
# same as returning :
{true,conn,%{id: "arnaud", current_user: %User{id: "arnaud"}}}