/
builder.rb
37 lines (36 loc) · 1.04 KB
/
builder.rb
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
module Goliath
module Rack
class Builder
# Builds the rack middleware chain for the given API
#
# @param klass [Class] The API class to build the middlewares for
# @param api [Object] The instantated API
# @return [Object] The Rack middleware chain
def self.build(klass, api)
::Rack::Builder.app do
klass.middlewares.each do |mw_klass, args, blk|
use(mw_klass, *args, &blk)
end
# If you use map you can't use run as
# the rack builder will blowup.
if klass.maps.empty?
run api
else
klass.maps.each do |(path, route_klass, blk)|
if route_klass
map(path) do
route_klass.middlewares.each do |mw_klass, args, blk|
use(mw_klass, *args, &blk)
end
run klass.new
end
else
map(path, &blk)
end
end
end
end
end
end
end
end