ewgi examples

1 view
Skip to first unread message

filippo pacini

unread,
Jul 19, 2009, 11:53:51 AM7/19/09
to ew...@googlegroups.com
As dmitrii has mentioned we have created a project on github to
collect some ewgi examples:
http://github.com/filippo/ewgi_examples/tree/master

For now we have:
- the usual hello world :-)
- HELLO WORLD a middleware example that trasform a response body to uppercase
- an example of streaming file
- a ewgi_deflate middleware component which might be the base for
serving gzipped content if the client supports it

Others will come.

cheers,
--
Filippo Pacini

Dmitrii Dimandt

unread,
Jul 19, 2009, 3:54:46 PM7/19/09
to ew...@googlegroups.com
I'll try to contribute whenever I can as well.


Actually, you could also include an example on how to create chainable middleware.



If you have


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
    






Filippo Pacini

unread,
Jul 20, 2009, 3:35:20 AM7/20/09
to ew...@googlegroups.com
Thanks Dmitrii,
I'll add the chain example too.
Reply all
Reply to author
Forward
0 new messages