Class: Hokuto::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/hokuto/server.rb

Constant Summary collapse

Jetty =
org.eclipse.jetty.server.Server

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Server

initialize the server.



16
17
18
19
20
21
22
23
# File 'lib/hokuto/server.rb', line 16

def initialize(options = {})
  @http_port = options[:port].to_i
  @https_port = options[:https_port].to_i if options[:https_port]
  @jetty = Jetty.new
  @applications = {}
  @handler_collection = ContextHandlerCollection.new
  jetty.handler = @handler_collection
end

Instance Attribute Details

#applicationsObject (readonly)

Returns the value of attribute applications.



12
13
14
# File 'lib/hokuto/server.rb', line 12

def applications
  @applications
end

#certsObject (readonly)

Returns the value of attribute certs.



12
13
14
# File 'lib/hokuto/server.rb', line 12

def certs
  @certs
end

#handler_collectionObject (readonly)

Returns the value of attribute handler_collection.



12
13
14
# File 'lib/hokuto/server.rb', line 12

def handler_collection
  @handler_collection
end

#http_portObject (readonly)

Returns the value of attribute http_port.



11
12
13
# File 'lib/hokuto/server.rb', line 11

def http_port
  @http_port
end

#https_portObject (readonly)

Returns the value of attribute https_port.



11
12
13
# File 'lib/hokuto/server.rb', line 11

def https_port
  @https_port
end

#jettyObject (readonly)

Returns the value of attribute jetty.



12
13
14
# File 'lib/hokuto/server.rb', line 12

def jetty
  @jetty
end

Instance Method Details

#add(application) ⇒ Object

register the application.



26
27
28
29
30
31
32
# File 'lib/hokuto/server.rb', line 26

def add(application)
  previous_application = applications.delete application.context_root
  handler_collection.remove_handler previous_application.context if previous_application

  handler_collection.add_handler application.context
  applications[application.context_root] = application
end

#runObject

booting the server.



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/hokuto/server.rb', line 35

def run
  [:INT, :TERM, :ABRT].each{|signal|Signal.trap(signal, ->{stop})}

  connector = SelectChannelConnector.new
  connector.port = http_port
  connector.confidential_port = https_port if https_port

  jetty.add_connector connector
  jetty.start

  jetty.join
end

#stopObject

shutting down the server.



49
50
51
# File 'lib/hokuto/server.rb', line 49

def stop
  jetty.stop
end