Permalink
Browse files

Add a response module.

  • Loading branch information...
1 parent f0eaf6f commit d0d42f95c8ee10fd3f45f37755fbde05ab5e9452 @josevalim josevalim committed Feb 27, 2012
View
@@ -6,8 +6,7 @@ defmodule MyApp do
use Dynamo::Router
get "/foo/bar" do
- { :ok, req } = :cowboy_http_req.reply(200, [], "Hello World!", request)
- req
+ response.reply(200, [], "Hello World!")
end
end
@@ -24,16 +24,18 @@ defmodule Dynamo::Cowboy do
acceptors = Orddict.get options, :acceptors, 100
handler = Orddict.get options, :handler, Dynamo::Cowboy::Handler
dispatch = Orddict.get options, :dispatch, dispatch_for(app, handler)
+ verbose = Orddict.get options, :verbose, true
- options = Enum.reduce [:port, :acceptors, :handler], options, Orddict.delete(&2, &1)
+ options = Enum.reduce [:port, :acceptors, :handler, :verbose], options, Orddict.delete(&2, &1)
options = Orddict.put options, :dispatch, dispatch
:cowboy.start_listener(app, acceptors,
:cowboy_tcp_transport, [port: port],
:cowboy_http_protocol, options
)
- IO.puts "Running #{app} on port #{port} with Cowboy"
+ if verbose, do:
+ IO.puts "Running #{app} on port #{port} with Cowboy"
end
def shutdown(app) do
@@ -11,8 +11,13 @@ defmodule Dynamo::Cowboy::Handler do
end
def handle(req, app) do
- _ = app.service(Dynamo::Cowboy::Request.new(req), {})
- { :ok, req, app }
+ res = app.service(Dynamo::Cowboy::Request.new(req), Dynamo::Cowboy::Response.new(req))
+
+ if is_record(res, Dynamo::Cowboy::Response) do
+ { :ok, res.cowboy_request, app }
+ else:
+ raise "Expected service to return a Dynamo::Cowboy::Response, got #{inspect res}"
+ end
end
def terminate(_req, _app) do
@@ -1,12 +1,6 @@
defmodule Dynamo::Cowboy::Request do
require :cowboy_http_req, as: R
- @doc false
- # Used internally by cowboy to retrieve the original request.
- def cowboy_request(req) do
- _(req)
- end
-
@doc """
Builds a new Dynamo::Cowboy::Request based on
the original Cowboy request object.
@@ -0,0 +1,38 @@
+defmodule Dynamo::Cowboy::Response do
+ require :cowboy_http_req, as: R
+
+ @doc """
+ Builds a new Dynamo::Cowboy::Response based on
+ the original Cowboy request object.
+ """
+ def new(req) do
+ { __MODULE__, req }
+ end
+
+ @doc """
+ Returns the underlying cowboy request. This is used
+ internally by Dynamo but may also be used by other
+ developers (with caution)
+ """
+ def cowboy_request(res) do
+ _(res)
+ end
+
+ @doc """
+ Replies to the client with the given status, headers and body
+ """
+ def reply(status, headers, body, res) do
+ { :ok, req } = :cowboy_http_req.reply(status, headers, body, _(res))
+ _(req, res)
+ end
+
+ # Returns the original cowboy request object.
+
+ defp _(req) do
+ elem(req, 2)
+ end
+
+ defp _(value, req) do
+ setelem(req, 2, value)
+ end
+end
@@ -20,13 +20,13 @@ defmodule Dynamo::Router do
import Dynamo::Router::DSL
@overridable true
- def service(request, response) do
- dispatch(request.method, request.path_segments, request, response)
+ def service(req, res) do
+ dispatch(req.method, req.path_segments, req, res)
end
@overridable true
- def not_found(request, _response) do
- request.reply(404, [], "Status: 404")
+ def not_found(_req, res) do
+ res.reply(404, [], "")
end
end
end
@@ -4,7 +4,7 @@ defmodule Dynamo::Cowboy::RequestTest do
use ExUnit::Case
def setup_all do
- Dynamo::Cowboy.run __MODULE__, port: 8011
+ Dynamo::Cowboy.run __MODULE__, port: 8011, verbose: false
end
def teardown_all do
@@ -4,15 +4,15 @@ defmodule Dynamo::Cowboy::RouterApp do
use Dynamo::Router
get "/foo/bar" do
- request.reply(200, [], "Hello World!")
+ response.reply(200, [], "Hello World!")
end
end
defmodule Dynamo::Cowboy::RouterTest do
use ExUnit::Case
def setup_all do
- Dynamo::Cowboy.run RouterApp, port: 8012
+ Dynamo::Cowboy.run RouterApp, port: 8012, verbose: false
end
def teardown_all do
@@ -23,6 +23,10 @@ defmodule Dynamo::Cowboy::RouterTest do
assert_match { 200, _, "Hello World!" }, http_client.request :get, "/foo/bar"
end
+ test "404 response a router app" do
+ assert_match { 404, _, "" }, http_client.request :get, "/other"
+ end
+
defp http_client do
HTTPClient.new("http://127.0.0.1:8012")
end

0 comments on commit d0d42f9

Please sign in to comment.