From d14b83bdd5d325e2eadc3464f863de2bbd28d21c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sun, 20 Mar 2011 09:54:20 +0100 Subject: [PATCH] Pimp mochiweb example. --- .gitignore | 1 + examples/mochiweb.ex | 22 ------------------ examples/mochiweb/hello_world.ex | 36 +++++++++++++++++++++++++++++ lib/ex_bridge.ex | 5 ++++ lib/ex_bridge/mochiweb.ex | 2 ++ lib/ex_bridge/mochiweb/request.ex | 18 +++++++++++++++ lib/ex_bridge/mochiweb/request.exb | Bin 0 -> 2372 bytes 7 files changed, 62 insertions(+), 22 deletions(-) delete mode 100644 examples/mochiweb.ex create mode 100644 examples/mochiweb/hello_world.ex create mode 100644 lib/ex_bridge/mochiweb.ex create mode 100644 lib/ex_bridge/mochiweb/request.ex create mode 100644 lib/ex_bridge/mochiweb/request.exb diff --git a/.gitignore b/.gitignore index 068957c..e6b88b7 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ +lib/*.exb erl_crash.dump \ No newline at end of file diff --git a/examples/mochiweb.ex b/examples/mochiweb.ex deleted file mode 100644 index 89018be..0000000 --- a/examples/mochiweb.ex +++ /dev/null @@ -1,22 +0,0 @@ -% Add mochiweb as dependency -Erlang.code.add_path $"deps/mochiweb-1.5.2/ebin" - -module MochiwebSample - def start - options = { - 'name: 'http_8443, - 'port: 8443, - 'loop: -> (req) loop(req) - } - - Erlang.mochiweb_http.start options.to_list - end - - private - - def loop(req) - Erlang.apply(req, 'respond, [{200, [{$"Content-Type", $"text/html"}], $"Hello world"}]) - end -end - -MochiwebSample.start \ No newline at end of file diff --git a/examples/mochiweb/hello_world.ex b/examples/mochiweb/hello_world.ex new file mode 100644 index 0000000..f3c0fe0 --- /dev/null +++ b/examples/mochiweb/hello_world.ex @@ -0,0 +1,36 @@ +% This is a simple example of running Mochiweb with ExBridge. +% Notice that the code to start a given webserver is not +% agnostic, but the request interaction is. +% +% You can run this example from the repo root directoy: +% +% elixir --no-halt examples/mochiweb/hello_world.ex +% +% And then access localhost:3000 in your browser. + +% Add mochiweb as dependency +Erlang.code.add_path $"deps/mochiweb-1.5.2/ebin" + +% Load code inside lib +Code.unshift_path "lib" +Code.require "ex_bridge/mochiweb" + +module MochiwebSample + def start + options = { + 'name: 'mochiweb_sample, + 'port: 3000, + 'loop: -> (req) loop(ExBridge.request('mochiweb, req)) + } + + Erlang.mochiweb_http.start options.to_list + end + + private + + def loop(request) + request.respond 200, { "Content-Type": "text/plain" }, "Hello world\n" + end +end + +{ 'ok, pid } = MochiwebSample.start \ No newline at end of file diff --git a/lib/ex_bridge.ex b/lib/ex_bridge.ex index 1b75f97..0ae6ee0 100644 --- a/lib/ex_bridge.ex +++ b/lib/ex_bridge.ex @@ -1,2 +1,7 @@ +% elixir: cache + module ExBridge + def request('mochiweb, request) + ExBridge::Mochiweb::Request.new(request) + end end \ No newline at end of file diff --git a/lib/ex_bridge/mochiweb.ex b/lib/ex_bridge/mochiweb.ex new file mode 100644 index 0000000..9d11f44 --- /dev/null +++ b/lib/ex_bridge/mochiweb.ex @@ -0,0 +1,2 @@ +Code.require "ex_bridge" +Code.require "ex_bridge/mochiweb/request" \ No newline at end of file diff --git a/lib/ex_bridge/mochiweb/request.ex b/lib/ex_bridge/mochiweb/request.ex new file mode 100644 index 0000000..d970707 --- /dev/null +++ b/lib/ex_bridge/mochiweb/request.ex @@ -0,0 +1,18 @@ +% elixir: cache + +object ExBridge::Mochiweb::Request + def constructor(request) + { 'request: request } + end + + def respond(status, headers, body) + response = { status, convert_headers(headers), body.to_bin } + Erlang.apply(@request, 'respond, [response]) + end + + private + + def convert_headers(headers) + headers.to_list.map -> ({key, value}) {key.to_bin, value.to_bin} + end +end \ No newline at end of file diff --git a/lib/ex_bridge/mochiweb/request.exb b/lib/ex_bridge/mochiweb/request.exb new file mode 100644 index 0000000000000000000000000000000000000000..edb03281e480d9826b3da3fba6988d1971b601d9 GIT binary patch literal 2372 zcmeHIO^72!6t3>-{LIc@O*$S#56xyy!c4NW0WlYACM03O*&W8ei_mt`m83ICS5I|i zHhL2GvUqYYf`YIY?|YfMg0LqM6x@S1!IK9?J&O2Vr32YT))97Z+wf8Ke(P1es;_U2 z2_g27hUA6T>_A1~Kr|YMvNw!wif*HEEKa8))hDD`eKQp*t&U_Ws&7kmQXNEkIPF%& z%Nr}KCRvWEny1(mvk9#9SNq2C6&U|KXzd^FYY95i1UQddesvM}0M^pnZp|L@K|i#kAI|Z!Uz0t|gF3RWwSwXETX@jmc}SpF(ZF$ z+}nEKzP0_!ov*(A{+svCN9d4G0wrQ?d_cO<9f_VcmX$e3}{deia|Z7n_Z@Is(RvtRQ~%_ z?84#hcT){VOz@dqpi58ewdKP3YsErJ;-P`F;*UC5dzH>!$8A+?T403LSle?u-45k0EfE)4L~)C+)i`&{yyEh!4yW6WCpc~L z4=dbpxqX#yQ{Hq6oH5SYwMwVlaUemZ%=cU{Z8=-p1tfj*`@D1>)j1d zL-PRkI^sRQ-_VIFHy&_nXyF`~$sXC-#&642TQhTdw{$0#!E?tn14D9FW%DHfVlUpCb{P<{C}W4M9&{;_=6KXWO0gf%&wAF?g7 z9*)S0D}YM?)U5*60LYSCfD!&enIo_~E6GKFdX4e{y=~)1%++XC(X(TOXQV Wm9acYw*H?Szu@&}tp4;_ee@46i&@G5 literal 0 HcmV?d00001