Skip to content

Latest commit

 

History

History
128 lines (91 loc) · 3.47 KB

README.rst

File metadata and controls

128 lines (91 loc) · 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://github.com/skarab/ewgi.git
    $ svn checkout http://mochiweb.googlecode.com/svn/trunk/ \
      mochiweb-read-only
    
  2. Compile them.

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

    -module(ewex_web).
    
    -export([start/0,stop/0,
             loop/1,simple_app/1]).
    
    start() ->
        mochiweb_http:start([{name, ewex}, {loop, fun ?MODULE:loop/1},
                             {ip, "127.0.0.1"}, {port, 8889}]).
    
    stop() ->
        mochiweb_http:stop(ewex).
    
    loop(Req) ->
        Mod = ewgi_mochiweb:new(fun ?MODULE:simple_app/1),
        Mod:run(Req).
    
    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 http://127.0.0.1:8889/ (type halt(). in the Erlang shell when you want to stop)

The even shorter inets example

$ git clone git://github.com/skarab/ewgi.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.

Advantages

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

Reference implementations

The current server reference implementations include: