diff options
author | Seth Vargo <sethvargo@gmail.com> | 2013-05-16 16:47:34 -0400 |
---|---|---|
committer | Seth Vargo <sethvargo@gmail.com> | 2013-05-16 16:47:34 -0400 |
commit | fa417efc4aec935b0d5c48db642a42d87b13fc9a (patch) | |
tree | 7c03f422b442d110b81a43155058189afe502620 | |
parent | 5709c30083f89674b71ce8a7a5c9f81602cc97a3 (diff) | |
download | chef-zero-fa417efc4aec935b0d5c48db642a42d87b13fc9a.tar.gz |
Log debugging request and response
-rw-r--r-- | lib/chef_zero/rest_base.rb | 2 | ||||
-rw-r--r-- | lib/chef_zero/rest_router.rb | 25 |
2 files changed, 18 insertions, 9 deletions
diff --git a/lib/chef_zero/rest_base.rb b/lib/chef_zero/rest_base.rb index fe08069..f219ff1 100644 --- a/lib/chef_zero/rest_base.rb +++ b/lib/chef_zero/rest_base.rb @@ -14,8 +14,6 @@ module ChefZero end def call(request) - ChefZero::Log.debug(request) - method = request.method.downcase.to_sym if !self.respond_to?(method) accept_methods = [:get, :put, :post, :delete].select { |m| self.respond_to?(m) } diff --git a/lib/chef_zero/rest_router.rb b/lib/chef_zero/rest_router.rb index 26b9522..d8919d3 100644 --- a/lib/chef_zero/rest_router.rb +++ b/lib/chef_zero/rest_router.rb @@ -12,18 +12,29 @@ module ChefZero def call(request) begin - ChefZero::Log.debug "Request: #{request}" + ChefZero::Log.debug(request) + clean_path = "/" + request.rest_path.join("/") - routes.each do |route, endpoint| - if route.match(clean_path) - return endpoint.call(request) - end - end - not_found.call(request) + + response = find_endpoint(clean_path).call(request) + ChefZero::Log.debug([ + "", + "--- RESPONSE (#{response[0]}) ---", + response[2], + "--- END RESPONSE ---", + ].join("\n")) + return response rescue ChefZero::Log.error("#{$!.inspect}\n#{$!.backtrace.join("\n")}") [500, {"Content-Type" => "text/plain"}, "Exception raised! #{$!.inspect}\n#{$!.backtrace.join("\n")}"] end end + + private + + def find_endpoint(clean_path) + _, endpoint = routes.find { |route, endpoint| route.match(clean_path) } + endpoint || not_found + end end end |