Harbor Documentation

Harbor::Cascade

Public Class Methods

new(environment, services, application, *ports)

      # File lib/harbor/cascade.rb, line 4
 4:     def initialize(environment, services, application, *ports)
 5:       unless services.is_a?(Harbor::Container)
 6:         raise ArgumentError.new("Harbor::Cascade#initialize[services] must be a Harbor::Container")
 7:       end
 8: 
 9:       @services = services
10:       @applications = []
11:       @public_paths = []
12: 
13:       begin
14:         @applications << application.new(services, environment)
15:       rescue ArgumentError => e
16:         raise ArgumentError.new("#{application}: #{e.message}")
17:       end
18: 
19:       @public_paths << Pathname(application.public_path) if application.respond_to?(:public_path)
20: 
21:       ports.each do |port|
22:         begin
23:           @applications << port.new(services, environment)
24:         rescue ArgumentError => e
25:           raise ArgumentError.new("#{port}: #{e.message}")
26:         end
27: 
28:         @public_paths << Pathname(port.public_path) if port.respond_to?(:public_path)
29:       end
30: 
31:       @public_paths << Pathname("public")
32:     end

Public Instance Methods

call(env)

      # File lib/harbor/cascade.rb, line 34
34:     def call(env)
35:       request = Request.new(self, env)
36:       response = Response.new(request)
37: 
38:       catch(:abort_request) do
39:         if file = find_public_file(request.path_info[1..-1])
40:           response.cache(nil, ::File.mtime(file), 86400) do
41:             response.stream_file(file)
42:           end
43:           return response.to_a
44:         end
45: 
46:         application, handler = nil
47: 
48:         @applications.each do |application|
49:           break if handler = application.router.match(request)
50:         end
51: 
52:         application = @applications.first unless handler
53:         request.application = application
54: 
55:         application.dispatch_request(handler, request, response)
56:       end
57: 
58:       response.to_a
59:     end

find_public_file(file)

      # File lib/harbor/cascade.rb, line 61
61:     def find_public_file(file)
62:       result = nil
63: 
64:       @public_paths.each do |public_path|
65:         if (path = public_path + file).file?
66:           result = path
67:           break
68:         end
69:       end
70: 
71:       result
72:     end