summaryrefslogtreecommitdiff
path: root/lib/chef_zero/rest_router.rb
blob: a93af8bc71e0a62450599175dbd5eba94004ead9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
require 'pp'

module ChefZero
  class RestRouter
    def initialize(routes)
      @routes = routes.map do |route, endpoint|
        if route =~ /\*\*$/
          pattern = Regexp.new("^#{route[0..-3].gsub('*', '[^/]*')}")
        else
          pattern = Regexp.new("^#{route.gsub('*', '[^/]*')}$")
        end
        [ pattern, endpoint ]
      end
    end

    attr_reader :routes
    attr_accessor :not_found

    def call(request)
      log_request(request)

      clean_path = "/" + request.rest_path.join("/")

      find_endpoint(clean_path).call(request).tap do |response|
        log_response(response)
      end
    rescue => ex
      exception = "#{ex.inspect}\n#{ex.backtrace.join("\n")}"

      ChefZero::Log.error(exception)
      [ 500, { "Content-Type" => "text/plain" }, "Exception raised! #{exception}" ]
    end

    private

      def find_endpoint(clean_path)
        _, endpoint = routes.find { |route, endpoint| route.match(clean_path) }
        endpoint || not_found
      end

      def log_request(request)
        ChefZero::Log.debug do
          "#{request.method} /#{request.rest_path.join("/")}".tap do |msg|
            next unless request.method =~ /^(POST|PUT)$/

            if request.body.nil? || request.body.empty?
              msg << " (no body)"
            else
              msg << [
                "",
                "--- #{request.method} BODY ---",
                request.body.chomp,
                "--- END #{request.method} BODY ---"
              ].join("\n")
            end
          end
        end

        ChefZero::Log.debug { request.pretty_inspect }
      end

      def log_response(response)
        ChefZero::Log.debug {
          [ "",
            "--- RESPONSE (#{response[0]}) ---",
            response[2].chomp,
            "--- END RESPONSE ---",
          ].join("\n")
        }
      end
  end
end