Class: Heroku::Forward::Proxy::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/heroku/forward/proxy/server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(backend, options = {}) ⇒ Server

Returns a new instance of Server.



9
10
11
12
13
14
# File 'lib/heroku/forward/proxy/server.rb', line 9

def initialize(backend, options = {})
  @host = options[:host] || '0.0.0.0'
  @port = options[:port] || 3000
  @retries = options[:retries] || 10
  @backend = backend
end

Instance Attribute Details

#backendObject (readonly)

Returns the value of attribute backend.



6
7
8
# File 'lib/heroku/forward/proxy/server.rb', line 6

def backend
  @backend
end

#hostObject (readonly)

Returns the value of attribute host.



6
7
8
# File 'lib/heroku/forward/proxy/server.rb', line 6

def host
  @host
end

#loggerObject

Returns the value of attribute logger.



7
8
9
# File 'lib/heroku/forward/proxy/server.rb', line 7

def logger
  @logger
end

#portObject (readonly)

Returns the value of attribute port.



6
7
8
# File 'lib/heroku/forward/proxy/server.rb', line 6

def port
  @port
end

#retriesObject (readonly)

Returns the value of attribute retries.



6
7
8
# File 'lib/heroku/forward/proxy/server.rb', line 6

def retries
  @retries
end

#startObject (readonly)

Returns the value of attribute start.



6
7
8
# File 'lib/heroku/forward/proxy/server.rb', line 6

def start
  @start
end

Instance Method Details

#forward!(options = {}) ⇒ Object



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
# File 'lib/heroku/forward/proxy/server.rb', line 24

def forward!(options = {})

  @start = Time.now

  logger.info "Launching Backend ..." if logger

  backend.spawn!

  if options[:delay] && (delay = options[:delay].to_i) > 0
    logger.info "Waiting #{delay}s to Launch Proxy Server ..." if logger
    sleep delay
  end

  logger.info "Launching Proxy Server at #{host}:#{port} ..." if logger

  s = self
  ::Proxy.start({ :host => host, :port => port, :debug => false }) do |conn|
    if @start
      EM.next_tick do
        s.send(:connect, conn)
      end
    else
      s.send(:connect, conn)
    end

    conn.on_connect do
      s.on_connect
    end

    conn.on_data do |data|
      data
    end

    conn.on_response do |backend, resp|
      resp
    end

    conn.on_finish do
    end
  end

end

#on_connect(&callback) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/heroku/forward/proxy/server.rb', line 16

def on_connect(&callback)
  if block_given?
    @on_connect = callback
  elsif @on_connect
    @on_connect.call
  end
end

#stop!Object



67
68
69
70
71
72
# File 'lib/heroku/forward/proxy/server.rb', line 67

def stop!
  logger.info "Terminating Proxy Server" if logger
  EventMachine.stop
  logger.info "Terminating Web Server" if logger
  backend.terminate!
end