Class: Noder::HTTP::Server
- Inherits:
-
Object
- Object
- Noder::HTTP::Server
- Includes:
- Events::EventEmitter
- Defined in:
- lib/noder/http/server.rb
Instance Attribute Summary collapse
-
#options ⇒ Object
Returns the value of attribute options.
Instance Method Summary collapse
- #close ⇒ Object
-
#initialize(options = {}, &block) ⇒ Server
constructor
A new instance of Server.
- #listen(port = nil, address = nil, options = {}, &block) ⇒ Object
- #on(event, callback = nil, &block) ⇒ Object
Methods included from Events::EventEmitter
#emit, #event_stack, #event_stacks, #listener_count, #listeners, #max_listener_counts, #remove_all_listeners, #remove_listener, #set_max_listeners
Constructor Details
#initialize(options = {}, &block) ⇒ Server
Returns a new instance of Server.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/noder/http/server.rb', line 10 def initialize(={}, &block) defaults = { address: '0.0.0.0', port: 8000, app: nil, environment: 'development', threadpool_size: 20, enable_ssl: false, ssl_key: nil, ssl_cert: nil } @options = defaults.merge() # The 'close' event is emitted as EM is stopped, so we need to handle the callbacks outside of # the EM event loop with Events::EventNode instead of Events::EMEventNode set_node_class_for_event(Events::EventNode, 'close') push_default_callbacks on('request', &block) if block end |
Instance Attribute Details
#options ⇒ Object
Returns the value of attribute options.
8 9 10 |
# File 'lib/noder/http/server.rb', line 8 def @options end |
Instance Method Details
#close ⇒ Object
64 65 66 67 68 |
# File 'lib/noder/http/server.rb', line 64 def close Noder.logger.info 'Stopping Noder...' emit('close') EM.stop end |
#listen(port = nil, address = nil, options = {}, &block) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/noder/http/server.rb', line 29 def listen(port=nil, address=nil, ={}, &block) @options.merge!() @options[:port] = port if port @options[:address] = address if address EM.threadpool_size = @options[:threadpool_size] EM.epoll EM.synchrony do trap('INT') { close } trap('TERM') { close } # Listeners::NotFound should run after all other listeners, so we'll add it here add_listener('request', Listeners::NotFound) Noder.logger.info "Running Noder at #{@options[:address]}:#{@options[:port]}..." emit('start') connection_settings = Noder::Utils.slice_hash(@options, [:enable_ssl, :ssl_key, :ssl_cert]) EM.start_server(@options[:address], @options[:port], Noder::HTTP::Connection, block, connection_settings) do |connection| connection.request_stack = event_stack('request') connection.app = @options[:app] connection.environment = @options[:environment] end end end |
#on(event, callback = nil, &block) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/noder/http/server.rb', line 52 def on(event, callback=nil, &block) callback ||= block case event when 'request' super('request', callback, argument_keys: [:request, :response]) when 'close' super('close', callback) else super(event, callback) end end |