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 .
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.
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
Compile them.
$ (cd ewgi/ && make) $ (cd mochiweb-read-only/ && make)
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}.
Compile it
$ erlc ewex_web.erl
Run
$ erl -pa ewgi/ebin/ -pa mochiweb-read-only/ebin/ -eval \ 'ewex_web:start(), receive done -> done end.'
Point your browser to
http://127.0.0.1:8889/
(typehalt().
in the Erlang shell when you want to stop)
$ 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]}])')
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.
The current server reference implementations include: