Permalink
Browse files

WIP middleware support for the init callback.

  • Loading branch information...
1 parent 658f1e2 commit f69d8b5782e63b9426c1da68c28213a6b457a28d @knutin committed Apr 17, 2013
Showing with 34 additions and 9 deletions.
  1. +2 −2 src/elli_example_callback_handover.erl
  2. +11 −6 src/elli_http.erl
  3. +21 −1 src/elli_middleware.erl
@@ -4,9 +4,9 @@
init(Req, Args) ->
case elli_request:path(Req) of
[<<"hello">>, <<"world">>] ->
- handover;
+ {ok, handover};
_ ->
- pure
+ ignore
end.
handle(Req, Args) ->
View
@@ -71,8 +71,8 @@ handle_request(S, PrevB, Opts, {Mod, Args} = Callback) ->
Req = mk_req(Method, RawPath, RequestHeaders, <<>>, V, S, Callback),
- case behaviour(Req) of
- pure ->
+ case init(Req) of
+ {ok, standard} ->
{RequestBody, B2} = get_body(S, RequestHeaders, B1, Opts, Callback), t(body_end),
Req1 = Req#req{body = RequestBody},
@@ -81,7 +81,7 @@ handle_request(S, PrevB, Opts, {Mod, Args} = Callback) ->
t(user_end),
handle_response(Req1, B2, Response);
- handover ->
+ {ok, handover} ->
Req1 = Req#req{body = B1},
Mod:handle(Req1, Args)
end.
@@ -556,12 +556,17 @@ split_args(Qs) ->
%% CALLBACK HELPERS
%%
-behaviour(#req{callback = {Mod, Args}} = Req) ->
+init(#req{callback = {Mod, Args}} = Req) ->
case erlang:function_exported(Mod, init, 2) of
true ->
- Mod:init(Req, Args);
+ case Mod:init(Req, Args) of
+ ignore ->
+ {ok, standard};
+ {ok, Behaviour} ->
+ {ok, Behaviour}
+ end;
false ->
- pure
+ {ok, standard}
end.
handle_event(Mod, Name, EventArgs, ElliArgs) ->
@@ -30,12 +30,16 @@
-module(elli_middleware).
-behaviour(elli_handler).
--export([handle/2, handle_event/3]).
+-export([init/2, handle/2, handle_event/3]).
%%
%% ELLI CALLBACKS
%%
+init(Req, Args) ->
+ do_init(Req, mods(Args)).
+
+
handle(CleanReq, Config) ->
Mods = mods(Config),
PreReq = preprocess(CleanReq, Mods),
@@ -57,6 +61,22 @@ handle_event(Event, Args, Config) ->
%% MIDDLEWARE LOGIC
%%
+do_init(_, []) ->
+ {ok, standard};
+do_init(Req, [{Mod, Args} | Mods]) ->
+ case erlang:function_exported(Mod, init, 2) of
+ true ->
+ case Mod:init(Req, Args) of
+ ignore ->
+ do_init(Req, Mods);
+ Result ->
+ Result
+ end;
+ false ->
+ do_init(Req, Mods)
+ end.
+
+
process(_Req, []) ->
{404, [], <<"Not Found">>};
process(Req, [{Mod, Args} | Mods]) ->

0 comments on commit f69d8b5

Please sign in to comment.