In the interest of publishing more often and covering smaller topics, let's 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; its easy extendability (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, I'd say that 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 always 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.