blob: f2770d35502016dd0e0e1f6387cfe02df0147013 (
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
|
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)
begin
ChefZero::Log.debug(request)
ChefZero::Log.debug(request.body) if request.body
clean_path = "/" + request.rest_path.join("/")
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
|