Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
129 lines (91 sloc) 3.47 KB

EWGI, an Erlang webserver interface specification

EWGI (pronounced you-ghee) is a specification designed to allow web applications written in Erlang to run on any supported server. It also makes developing web applications simpler and more flexible by providing a common mechanism for reusing components. It was inspired by Python's PEP 333 and provides similar functionality to other projects such as Ruby's Rack .

"Hello world!" MochiWeb example

This sample application simply responds with 200 OK and a text/plain entity body of Hello world! for all requests.

Cheers to Geoff Cant for writing this example.

  1. Grab the latest ewgi and MochiWeb source trees.

    $ git clone git://
    $ svn checkout \
  2. Compile them.

    $ (cd ewgi/ && make)
    $ (cd mochiweb-read-only/ && make)
  3. Create a file called ewex_web.erl

    start() ->
        mochiweb_http:start([{name, ewex}, {loop, fun ?MODULE:loop/1},
                             {ip, ""}, {port, 8889}]).
    stop() ->
    loop(Req) ->
        Mod = ewgi_mochiweb:new(fun ?MODULE:simple_app/1),
    simple_app({ewgi_context, Request, _Response}) ->
        ResponseHeaders = [{"Content-type", "text/plain"}],
        Response = {ewgi_response, {200, "OK"}, ResponseHeaders,
                    [<<"Hello world!">>], undefined},
        {ewgi_context, Request, Response}.
  4. Compile it

    $ erlc ewex_web.erl
  5. Run

    $ erl -pa ewgi/ebin/ -pa mochiweb-read-only/ebin/ -eval \
    'ewex_web:start(), receive done -> done end.'
  6. Point your browser to (type halt(). in the Erlang shell when you want to stop)

The even shorter inets example

$ git clone git:// && (cd ewgi/ && make \
  && erl -pa ebin/ -eval 'application:start(inets)' \
  -eval 'application:set_env(ewgi, app_module, ewgi_testapp)' \
  -eval 'application:set_env(ewgi, app_function, testapp)' \
  -eval 'inets:start(httpd, [{port, 8889},
                             {server_name, "ewgi"},
                             {server_root, "."},
                             {document_root, "."},
                             {modules, [ewgi_inets]}])')

Middleware components

The real power of the EWGI interface specification is the ability to compose applications so that requests and responses can be modified by reusable components. For example, the specification includes an example middleware component which converts all text responses to upper-case.


  • Applications are server independent.
  • Middleware components can be reused.
  • Applications have a clean, functional interface.

Reference implementations

The current server reference implementations include: