Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
62 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
lib/*.exb | ||
erl_crash.dump |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,7 @@ | ||
% elixir: cache | ||
|
||
module ExBridge | ||
def request('mochiweb, request) | ||
ExBridge::Mochiweb::Request.new(request) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Code.require "ex_bridge" | ||
Code.require "ex_bridge/mochiweb/request" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 not shown.