PageRenderTime 82ms CodeModel.GetById 40ms app.highlight 5ms RepoModel.GetById 35ms app.codeStats 1ms

/README.rst

http://github.com/skarab/ewgi
ReStructuredText | 128 lines | 91 code | 37 blank | 0 comment | 0 complexity | 75a0061f327a0278a17efd237fc6c609 MD5 | raw file
  1EWGI, an Erlang webserver interface specification
  2=================================================
  3
  4EWGI (pronounced `you-ghee`) is a specification designed to allow web
  5applications written in Erlang to run on any supported server.  It
  6also makes developing web applications simpler and more flexible by
  7providing a common mechanism for reusing components.  It was inspired
  8by Python's `PEP 333`_ and provides similar functionality to other
  9projects such as Ruby's `Rack`_ .
 10
 11"Hello world!" MochiWeb example
 12-------------------------------
 13
 14This sample application simply responds with 200 OK and a
 15``text/plain`` entity body of ``Hello world!`` for all requests.
 16
 17Cheers to `Geoff Cant`_ for writing this example.
 18
 19#. Grab the latest ewgi and `MochiWeb`_ source trees.
 20
 21   ::
 22  
 23    $ git clone git://github.com/skarab/ewgi.git
 24    $ svn checkout http://mochiweb.googlecode.com/svn/trunk/ \
 25      mochiweb-read-only
 26
 27#. Compile them.
 28
 29   ::
 30  
 31    $ (cd ewgi/ && make)
 32    $ (cd mochiweb-read-only/ && make)
 33
 34#. Create a file called ``ewex_web.erl``
 35
 36   ::
 37  
 38    -module(ewex_web).
 39    
 40    -export([start/0,stop/0,
 41             loop/1,simple_app/1]).
 42    
 43    start() ->
 44        mochiweb_http:start([{name, ewex}, {loop, fun ?MODULE:loop/1},
 45                             {ip, "127.0.0.1"}, {port, 8889}]).
 46    
 47    stop() ->
 48        mochiweb_http:stop(ewex).
 49    
 50    loop(Req) ->
 51        Mod = ewgi_mochiweb:new(fun ?MODULE:simple_app/1),
 52        Mod:run(Req).
 53   
 54    simple_app({ewgi_context, Request, _Response}) ->
 55        ResponseHeaders = [{"Content-type", "text/plain"}],
 56        Response = {ewgi_response, {200, "OK"}, ResponseHeaders,
 57                    [<<"Hello world!">>], undefined},
 58        {ewgi_context, Request, Response}.
 59
 60#. Compile it
 61
 62   ::
 63  
 64   $ erlc ewex_web.erl
 65
 66#. Run
 67
 68   ::
 69  
 70    $ erl -pa ewgi/ebin/ -pa mochiweb-read-only/ebin/ -eval \
 71    'ewex_web:start(), receive done -> done end.'
 72
 73#. Point your browser to ``http://127.0.0.1:8889/`` (type ``halt().``
 74   in the Erlang shell when you want to stop)
 75
 76
 77The even shorter inets example
 78------------------------------
 79
 80::
 81
 82 $ git clone git://github.com/skarab/ewgi.git && (cd ewgi/ && make \
 83   && erl -pa ebin/ -eval 'application:start(inets)' \
 84   -eval 'application:set_env(ewgi, app_module, ewgi_testapp)' \
 85   -eval 'application:set_env(ewgi, app_function, testapp)' \
 86   -eval 'inets:start(httpd, [{port, 8889},
 87                              {server_name, "ewgi"},
 88                              {server_root, "."},
 89                              {document_root, "."},
 90                              {modules, [ewgi_inets]}])')
 91
 92Middleware components
 93---------------------
 94
 95The real power of the EWGI interface specification is the ability to
 96compose applications so that requests and responses can be modified by
 97reusable components.  For example, the specification includes an
 98example middleware component which converts all text responses to
 99upper-case.
100
101Advantages
102----------
103
104* Applications are `server independent.`
105* Middleware components can be reused.
106* Applications have a clean, functional interface.
107
108Reference implementations
109-------------------------
110
111The current server reference implementations include:
112
113* `MochiWeb`_
114* `Yaws`_
115* `inets`_
116
117.. _PEP 333:
118    http://www.python.org/dev/peps/pep-0333/
119.. _Rack:
120    http://rack.rubyforge.org/
121.. _MochiWeb:
122    http://code.google.com/p/mochiweb/
123.. _Yaws:
124    http://yaws.hyber.org/
125.. _inets:
126    http://erlang.org/doc/apps/inets/http_server.html
127.. _Geoff Cant:
128    http://github.com/archaelus/