I'll try to contribute whenever I can as well.
Actually, you could also include an example on how to create chainable middleware.
get_chain(Modules) ->
F = fun(Ctx) ->
beepbeep_mw:stack(Ctx, Modules)
end,
F.
and each middleware module is implemented as follows:
-module(middleware_module)
run(Ctx, App) ->
Ctx1 = modify_request(Ctx), %% optional
Ctx2 = App(Ctx1),
Ctx3 = modify_response(Ctx2), %% optional
Ctx.
%% or
%% run(Ctx, App, AdditionalModuleOptions)
Then you can invoke
F = get_chain([
middleware_component1,
middleware_component2
{middleware_component3, [with, options]}
]),
F(EwgiContext).
And let the middleware chain take care of itself :)
So, to turn hello_world and uppercase into such a chain:
-module(ewgi_hello).
run(Ctx, _App) ->
ResponseHeaders = [{"Content-type", "text/plain"}],
Response = {ewgi_response, {200, "OK"}, ResponseHeaders,
[<<"Hello world!">>], undefined},
{ewgi_context, Request, Response}.
%% actually the last line should be App(Ctx)
%% if this is the last middleware in chain
%% Ctx will be returned
====================
-module(ewgi_to_upper).
run(Ctx, App) ->
Ctx1 = App(Ctx),
Body = ewgi_api:response_message_body(Ctx),
Body1 = [string:to_upper(erlang:binary_to_list(B)) || B <- Body],
ewgi_api:response_message_body(Body1, Ctx1).
=====================
-module(ewgi_examples_web).
dispatch("/HELLO", Ctx) ->
F = get_chain([ewgi_to_upper, ewgi_hello]),
F(Ctx);
Such a chain allows for a more flexible manipulation of requests and responses than F(G(H(...X(Ctx)))) which only operates on responses