Sha256: 04065c45fa372624a388d2d24778c610cf84762893ee15526ebec14011b253e6

Contents?: true

Size: 1.64 KB

Versions: 1

Compression:

Stored size: 1.64 KB

Contents

require 'glue/autoreload'
require 'nitro/adapter/webrick'

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?
	#++

	setting :address, :default => '127.0.0.1', :doc => 'The server listening address'

	# The server listening port.

	setting :port, :default => 9999, :doc => 'The server listening port'

	# The map.
	
	setting :map, :default => { '/' => SimpleController }, :doc => 'The server map'

	# The name of the application.

	attr_accessor :name
	
	# The sitemap. Defines how controller objects are published.
	
	attr_accessor :map

	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

	def root=(controller)
		@map['/'] = controller
	end
	
	def root
		Mounter.new(self)
	end

	# Helper method.
	
	def self.run(controller = nil)
		server = Server.new
		server.start(controller)
	end
	
end

end

# Alias for the Server class.

App = Nitro::Server unless Object.const_defined?(:App)

# * George Moschovitis <gm@navel.gr>

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
nitro-0.20.0 lib/nitro/server.rb