Skip to content

Commit

Permalink
Pimp mochiweb example.
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Mar 20, 2011
1 parent c9f01f5 commit d14b83b
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 22 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1 +1,2 @@
lib/*.exb
erl_crash.dump
22 changes: 0 additions & 22 deletions examples/mochiweb.ex

This file was deleted.

36 changes: 36 additions & 0 deletions 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
5 changes: 5 additions & 0 deletions lib/ex_bridge.ex
@@ -1,2 +1,7 @@
% elixir: cache

module ExBridge
def request('mochiweb, request)
ExBridge::Mochiweb::Request.new(request)
end
end
2 changes: 2 additions & 0 deletions lib/ex_bridge/mochiweb.ex
@@ -0,0 +1,2 @@
Code.require "ex_bridge"
Code.require "ex_bridge/mochiweb/request"
18 changes: 18 additions & 0 deletions 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
Binary file added lib/ex_bridge/mochiweb/request.exb
Binary file not shown.

0 comments on commit d14b83b

Please sign in to comment.