-
Notifications
You must be signed in to change notification settings - Fork 5
Using Cowmachine
Marc Worrell edited this page Jul 27, 2018
·
7 revisions
Cowmachine is at Hex, in your rebar.config
file use:
{deps, [
{cowmachine, "1.0.0"}
]}.
You can also use the direct Git url and use the development version:
{deps, [
{cowmachine, {git, "", {branch, "master"}}}
]}.
Cowmachine can be called from your Cowboy middleware:
-spec execute(Req, Env) -> {ok, Req, Env} | {stop, Req}
when Req::cowboy_req:req(), Env::cowboy_middleware:env().
execute(Req, Env) ->
% Replace below with your own controller module
Controller = mycontroller,
ControllerOpts = [ ],
ControllerRequestArg = Req,
% Set options for the cowmachine and handle the request
Options = #{
on_welformed =>
fun(Ctx) ->
% Perform anything after well-formedness check of your request
% Examples are parsing the query args, or authentication
end
},
cowmachine:request(Controller, ControllerOpts, Req, Env, Options, ControllerRequestArg).
You can use the Zotonic Dispatch Compiler to match your controller paths against the request.
The controller provides the callback functions to handle the request.
The controller is an Erlang module implementing these callback functions, only functions that return some non-default value need to be implemented.