In the interest of publishing more often and covering smaller topics, lets review web-server-agnostic middleware for Erlang.
The current standard for Erlang web application middleware is EWGI, inspired by Python’s PEP 333 and providing similar functionality as Ruby’s Rack. All major Erlang web servers support EWGI, and most web frameworks.
Created by Rusty Klophaus, SimpleBridge “takes the pain out of coding to multiple Erlang web servers by creating a standardized interface.” It is used by the soon to be released new version of The Nitrogen Web Framework. According to Rusty, SimpleBridge has some improvements over EWGI, such as a smaller code base; more easily extendable (Takes about 150 lines to add support for a new HTTP server, vs. ~350 for EWGI; support for multipart file uploads, with size limits and handle-able errors; static file support; more specific interface functions for getting and setting cookies.
However, based on my experience with Rack, Multipart file uploads, static file support, and anything but basic cookie manipulation are all best handled by a web server such as nginx or a reverse proxy like HAProxy anyway. Perhaps it is better that EWGI tries to do less in this case…
Let’s look at a simple EWGI example with MochiWeb (shamelessly pulled from the docs)…
First we startup mochiweb calling the loop/1 function. Mochiweb will call our loop/1 function whenever a request is received.

In loop/1 we setup a call to our simple implementation of the EWGI interface.

Now the EWGI implementation is called with a tuple that aways starts with ewgi_context, and contains a Request and a Response. This code returns a 5-tuple response (further details in the documentation).

EWGI solves the problem of creating a standard interface to middleware components. It has become the standard, and the power of such standards lies in their implementations’ ubiquity. Making your own hit web framework at home in your spare time has never been easier.
on Dec 29th, 2009 at 9:11 am
What is this, I do not fully understand all the capabilities of EWGI.
I have been thinking of writing a web-serving application using Mochiweb and Webmachine as software to build upon. Creating a web-server done through the unix philosophy where each component does one task well. Where my software would come in and create on top of mochiweb and webmachine the ability to create virtual hosts and serve content.
Is EWGI something akin to this?
on Dec 30th, 2009 at 8:00 am
The problem with ewgi is that you can’t run webmachine behind it. At least not yet, IIRC. It doesn’t support streaming, or multiparts or…
on Dec 30th, 2009 at 10:46 am
Yes your’s and Rusty’s critiques of EWGI’s limitations are valid. I don’t think you can use EWGI with webmachine – it’s more of a ‘full stack’ in that it provides access to the request, headers, and allows you to build the response with it’s own API. Webmachine is great – very powerful tool but it’s got it’s own philosophy and it only runs w/mochiweb – if you want to write the equivalent of Rack::Bug (an excellent debugging Rack middleware component for Ruby’s Rack), you would need to write a custom version to work with webmachine as far as I can tell. Honestly i haven’t used webmachine yet but it’s on the list of rainy day projects….
on Dec 30th, 2009 at 10:49 am
Honestly I don’t yet know if you can use EWGI with webmachine (per the other comment)… i put out a question to the webmachine guys and i’ll let you know what i hear
on Dec 31st, 2009 at 1:08 pm
Recently Davide has done some work on ewgi to add streaming and multipart request/responses.
But I don’t think the changes are all merged in the master repository.
As I said to Dmitrii via twitter I’m interested in trying to port webmachine on top of ewgi for a project I’m working on, so I’ll look into this at the benning of the year.
on Jan 3rd, 2010 at 3:03 am
[...] The State of Erlang Web Middleware – EWGI and SimpleBridge – Erlang Inside (tags: erlang web programming) [...]