Class: Harbor::Cascade

Inherits:
Object
  • Object
show all
Defined in:
lib/harbor/cascade.rb

Instance Method Summary collapse

Constructor Details

#initialize(environment, services, application, *ports) ⇒ Cascade

Returns a new instance of Cascade.


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
# File 'lib/harbor/cascade.rb', line 4

def initialize(environment, services, application, *ports)
  unless services.is_a?(Harbor::Container)
    raise ArgumentError.new("Harbor::Cascade#initialize[services] must be a Harbor::Container")
  end

  @services = services
  @applications = []
  @public_paths = []

  begin
    @applications << application.new(services, environment)
  rescue ArgumentError => e
    raise ArgumentError.new("#{application}: #{e.message}")
  end

  @public_paths << Pathname(application.public_path) if application.respond_to?(:public_path)

  ports.each do |port|
    begin
      @applications << port.new(services, environment)
    rescue ArgumentError => e
      raise ArgumentError.new("#{port}: #{e.message}")
    end

    @public_paths << Pathname(port.public_path) if port.respond_to?(:public_path)
  end

  @public_paths << Pathname("public")
end

Instance Method Details

#call(env) ⇒ Object


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
# File 'lib/harbor/cascade.rb', line 34

def call(env)
  request = Request.new(self, env)
  response = Response.new(request)

  catch(:abort_request) do
    if file = find_public_file(request.path_info[1..-1])
      response.cache(nil, ::File.mtime(file), 86400) do
        response.stream_file(file)
      end
      return response.to_a
    end

    application, handler = nil

    @applications.each do |application|
      break if handler = application.router.match(request)
    end

    application = @applications.first unless handler
    request.application = application

    application.dispatch_request(handler, request, response)
  end

  response.to_a
end

#find_public_file(file) ⇒ Object


61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/harbor/cascade.rb', line 61

def find_public_file(file)
  result = nil

  @public_paths.each do |public_path|
    if (path = public_path + file).file?
      result = path
      break
    end
  end

  result
end