Class: Cannon::App

Inherits:
Object
  • Object
show all
Defined in:
lib/cannon/app.rb

Defined Under Namespace

Classes: ExtraRouter

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app_binding, port: nil, ip_address: nil, &block) ⇒ App

Returns a new instance of App.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/cannon/app.rb', line 10

def initialize(app_binding, port: nil, ip_address: nil, &block)
  @app_binding = app_binding
  @routes = []
  @load_environment = block
  @cache = {}

  config.port = port unless port.nil?
  config.ip_address = ip_address unless ip_address.nil?

  define_cannon_environment
  define_cannon_root
  define_cannon_mime_type
  define_cannon_config
  define_cannon_configure_method
  define_cannon_logger
  define_cannon_cache
end

Instance Attribute Details

#app_bindingObject (readonly)

Returns the value of attribute app_binding.



8
9
10
# File 'lib/cannon/app.rb', line 8

def app_binding
  @app_binding
end

#cacheObject (readonly)

Returns the value of attribute cache.



8
9
10
# File 'lib/cannon/app.rb', line 8

def cache
  @cache
end

#routesObject (readonly)

Returns the value of attribute routes.



8
9
10
# File 'lib/cannon/app.rb', line 8

def routes
  @routes
end

Instance Method Details

#configObject



89
90
91
# File 'lib/cannon/app.rb', line 89

def config
  @config ||= Config.new
end

#configure(*environments, &block) ⇒ Object



82
83
84
85
86
87
# File 'lib/cannon/app.rb', line 82

def configure(*environments, &block)
  environments.each do |environment|
    define_env_helper(environment)
    yield config if Cannon.env == environment.to_s
  end
end

#envObject



93
94
95
# File 'lib/cannon/app.rb', line 93

def env
  @env ||= detect_env
end

#listen(port: config.port, ip_address: config.ip_address, async: false) ⇒ Object

Raises:



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
66
# File 'lib/cannon/app.rb', line 34

def listen(port: config.port, ip_address: config.ip_address, async: false)
  cannon_app = self

  if ENV['CONSOLE']
    command_set = Pry::CommandSet.new {}
    Pry.start binding, commands: command_set
    exit
  end

  raise AlreadyListening, 'App is currently listening' unless @server_thread.nil?

  Cannon::Handler.define_singleton_method(:app) { cannon_app }

  $LOAD_PATH << Cannon.root
  reload_environment unless config.reload_on_request

  server_block = ->(notifier) do
    EventMachine::run {
      server = EventMachine::start_server(ip_address, port, Cannon::Handler)
      notifier << server unless notifier.nil? # notify the calling thread that the server started if async
      Cannon.logger.info "Cannon listening on port #{port}..."
    }
  end

  if async
    notification = Queue.new
    Thread.abort_on_exception = true
    @server_thread = Thread.new { server_block.call(notification) }
    notification.pop
  else
    server_block.call(nil)
  end
end

#middleware_runnerObject



97
98
99
# File 'lib/cannon/app.rb', line 97

def middleware_runner
  @middleware_runner ||= build_middleware_runner(prepared_middleware_stack)
end

#reload_environmentObject



78
79
80
# File 'lib/cannon/app.rb', line 78

def reload_environment
  @load_environment.call unless @load_environment.nil?
end

#stopObject



68
69
70
71
72
73
74
75
76
# File 'lib/cannon/app.rb', line 68

def stop
  return if @server_thread.nil?
  EventMachine::stop_event_loop
  @server_thread.join(10)
  @server_thread.kill unless @server_thread.stop?
  Thread.abort_on_exception = false
  @server_thread = nil
  Cannon.logger.info "Cannon no longer listening"
end