Class: Nitro::Server

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

Defined Under Namespace

Classes: Mounter

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = 'Nitro', options = {}) ⇒ Server

Returns a new instance of Server.



74
75
76
77
78
79
80
81
82
# File 'lib/nitro/server.rb', line 74

def initialize(name = 'Nitro', options = {})
  @name = name
  @map = self.class.map.dup
  @address, @port = self.class.address, self.class.port
  @public_root = self.class.public_root
  @access_log = self.class.access_log
  @options = self.class.options.dup
  @options.update(options)
end

Instance Attribute Details

#access_logObject

The access_log for this server, used by Webrick.



72
73
74
# File 'lib/nitro/server.rb', line 72

def access_log
  @access_log
end

#addressObject

The server listening address.



55
56
57
# File 'lib/nitro/server.rb', line 55

def address
  @address
end

#dispatcherObject

Return the dispatcher.



63
64
65
# File 'lib/nitro/server.rb', line 63

def dispatcher
  @dispatcher
end

#mapObject

The sitemap. Defines how controller objects are published.



47
48
49
# File 'lib/nitro/server.rb', line 47

def map
  @map
end

#nameObject

The name of the application.



43
44
45
# File 'lib/nitro/server.rb', line 43

def name
  @name
end

#optionsObject

Additional server options. Useful to pass options to Webrick for example.



68
69
70
# File 'lib/nitro/server.rb', line 68

def options
  @options
end

#portObject

The server listening port.



59
60
61
# File 'lib/nitro/server.rb', line 59

def port
  @port
end

#public_rootObject

The public files root directory.



51
52
53
# File 'lib/nitro/server.rb', line 51

def public_root
  @public_root
end

Class Method Details

.map(ctrl_map = nil) ⇒ Object Also known as: map=

Helper to set the default controller map for a Server.



115
116
117
118
119
120
121
122
# File 'lib/nitro/server.rb', line 115

def map(ctrl_map = nil)
  unless ctrl_map
    Server.controller_map
  else
    Server.controller_map ||= {}
    Server.controller_map.update(ctrl_map)
  end
end

.run(options = {}) ⇒ Object

Helper method.

Available options:

:dispatcher, :controller

Altetnatively you can pass a single Controller class instead of the options hash.



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/nitro/server.rb', line 134

def run(options = {})
  unless options.is_a?(Hash)
    options = { :controller => options }
  end

  runner = Runner.new
  runner.setup_options
  runner.setup_mode
  runner.daemonize if runner.daemon  

  Global.setup
  Session.setup
  Part.setup
      
  server = Server.new
  server.start(options)
   
  runner.invoke(server) unless $NITRO_NO_INVOKE
  
  return server
end

Instance Method Details

#rootObject



107
108
109
# File 'lib/nitro/server.rb', line 107

def root
  Mounter.new(self)
end

#root=(controller) ⇒ Object



103
104
105
# File 'lib/nitro/server.rb', line 103

def root=(controller)
  @map['/'] = controller
end

#start(options = {}) ⇒ Object

Start the server.



95
96
97
98
99
100
101
# File 'lib/nitro/server.rb', line 95

def start(options = {})    
  @map['/'] = options[:controller] if options[:controller]
  @dispatcher = options[:dispatcher] || Dispatcher.new(@map)
  @dispatcher.server = self
  
  return self
end