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 0000000..edb0328 Binary files /dev/null and b/lib/ex_bridge/mochiweb/request.exb differ