lib/nitro/server.rb in nitro-0.20.0 vs lib/nitro/server.rb in nitro-0.21.0
- old
+ new
@@ -1,90 +1,127 @@
require 'glue/autoreload'
-require 'nitro/adapter/webrick'
+require 'nitro/server/runner'
+
module Nitro
class Server
- # :nodoc: all
- # A Helper class used for CherryPy-style publishing.
-
- class Mounter
- def initialize(parent, base = '')
- @parent, @base = parent, base
- end
-
- def method_missing(sym, *args)
- sym = sym.to_s
- if sym =~ /=$/
- @parent.map["#@base/#{sym.gsub(/=/, '')}"] = args.first
- else
- Mounter.new(@parent, "#@base/#{sym}")
- end
- end
- end
-
- # The server listening address.
- #--
- # 0.0.0.0 may be a better default?
- #++
+ # The server listening address.
+ #--
+ # 0.0.0.0 may be a better default?
+ #++
- setting :address, :default => '127.0.0.1', :doc => 'The server listening address'
+ setting :address, :default => '127.0.0.1', :doc => 'The server listening address'
- # The server listening port.
+ # The server listening port.
- setting :port, :default => 9999, :doc => 'The server listening port'
+ setting :port, :default => 9999, :doc => 'The server listening port'
- # The map.
-
- setting :map, :default => { '/' => SimpleController }, :doc => 'The server map'
+ # The map.
+
+ setting :map, :default => { '/' => SimpleController }, :doc => 'The server map'
- # The name of the application.
+ # The public files root directory.
+
+ setting :public_root, :default => 'public', :doc => 'The public files root directory'
- attr_accessor :name
-
- # The sitemap. Defines how controller objects are published.
-
- attr_accessor :map
+ # The access log, can be a path to a file or an IO object.
+
+ setting :access_log, :default => nil, :doc => 'The access log'
- def initialize(name = 'Nitro')
- @name = name
- @map = self.class.map.dup
- end
-
- # Start the server.
-
- def start(controller = nil)
- self.map['/'] = controller if controller
-
- $DBG = true
- autoreload(3)
- Rendering.reload = :full
-
- dispatcher = Dispatcher.new
- dispatcher.publish(self.map)
- conf = {
- :dispatcher => dispatcher
- }
-
- Webrick.start(Conf.new(conf))
- end
+ # The name of the application.
- def root=(controller)
- @map['/'] = controller
- end
-
- def root
- Mounter.new(self)
- end
+ attr_accessor :name
+
+ # The sitemap. Defines how controller objects are published.
+
+ attr_accessor :map
- # Helper method.
-
- def self.run(controller = nil)
- server = Server.new
- server.start(controller)
- end
-
+ # The public files root directory.
+
+ attr_accessor :public_root
+
+ # The server listening address.
+
+ attr_accessor :address
+
+ # The server listening port.
+
+ attr_accessor :port
+
+ # The dispatcher
+
+ attr_accessor :dispatcher
+
+ # Additional server options. Useful to pass options to
+ # Webrick for example.
+
+ attr_accessor :options
+
+ 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 = options
+ end
+
+ # Return the dispatcher.
+
+ def dispatcher
+ unless @dispatcher
+ @dispatcher = Dispatcher.new(self.map)
+ end
+ @dispatcher
+ end
+
+ # Start the server.
+
+ def start(controller = nil)
+ @map['/'] = controller if controller
+ @dispatcher = Dispatcher.new(@map)
+ end
+
+ def root=(controller)
+ @map['/'] = controller
+ end
+
+ def root
+ Mounter.new(self)
+ end
+
+ # Helper method.
+
+ def self.run(controller = nil)
+ runner = Runner.new
+ runner.setup_options
+ runner.setup_mode
+ runner.daemonize if runner.daemon
+ server = Server.new
+ server.start(controller)
+ runner.invoke(server)
+ end
+
+ # A Helper class used for CherryPy-style publishing.
+
+ class Mounter # :nodoc: all
+ def initialize(parent, base = '')
+ @parent, @base = parent, base
+ end
+
+ def method_missing(sym, *args)
+ sym = sym.to_s
+ if sym =~ /=$/
+ @parent.map["#@base/#{sym.gsub(/=/, '')}"] = args.first
+ else
+ Mounter.new(@parent, "#@base/#{sym}")
+ end
+ end
+ end
+
+
end
end
# Alias for the Server class.