Class: UWA::Server

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/uwa/server.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeServer

Returns a new instance of Server.



13
14
15
16
17
18
19
20
21
# File 'lib/uwa/server.rb', line 13

def initialize
	@host = '0.0.0.0'
	@port = 42080
	@debug = true
	@reload = true
	@widgets = []
	@mongrel = nil
	@plugins = []
end

Instance Attribute Details

#debugObject

Returns the value of attribute debug.



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

def debug
  @debug
end

#hostObject

Returns the value of attribute host.



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

def host
  @host
end

#pluginsObject

Returns the value of attribute plugins.



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

def plugins
  @plugins
end

#portObject

Returns the value of attribute port.



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

def port
  @port
end

#reloadObject

Returns the value of attribute reload.



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

def reload
  @reload
end

Class Method Details

.method_missing(name, *args) ⇒ Object



7
8
9
# File 'lib/uwa/server.rb', line 7

def self.method_missing(name, *args)
	self.instance.send(name, *args)
end

Instance Method Details

#<<(config) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/uwa/server.rb', line 23

def <<(config)
	@plugins = UWA::Plugin.load if @plugins.empty?

	begin
		@widgets << @plugins[config[:name]].merge(config)
	rescue
		log(:warning, "Unable to configure #{config[:name].inspect} at #{config[:path].inspect}")
	else
		log(:info, "Configure #{config[:name].inspect} at #{config[:path].inspect}")
	end
end

#log(level, msg) ⇒ Object



71
72
73
74
75
76
77
78
79
# File 'lib/uwa/server.rb', line 71

def log(level, msg)
	return unless @debug
	if msg.is_a? Array
		msg.each { |l| STDERR.puts "       " + l }
	else
		STDERR.puts "#{Time.now}: #{level.to_s.upcase}: #{msg}"
	end
	STDERR.flush
end

#runObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/uwa/server.rb', line 35

def run
	begin
		$mongrel_debug_client = @debug
		@mongrel = Mongrel::HttpServer.new(@host, @port) if @mongrel.nil?
		@widgets.each do |widget|
			unless @mongrel.classifier.uris.include?(widget[:path])
				object = widget[:class].new
				object.base = widget[:base]
				object.script = widget[:script]
				object.css = widget[:css]
				widget[:params].to_a.each do |name, value|
					object.instance_variable_set(('@' + name.to_s).to_sym, value)
				end
				@mongrel.register(widget[:path], object)
				log(:info, "Register #{widget[:name].inspect} at #{widget[:path].inspect}")
			end
		end
		unless @mongrel.classifier.uris.include?('/proxy')
			@mongrel.register('/proxy', UWA::Proxy.new)
			log(:info, "Proxy at \"/proxy\"")
		end
		log(:info, "Ready to serve")
		@mongrel.run.join
	rescue Interrupt
		# Do nothing
		log(:info, "Shutdown")
	rescue Exception
		@mongrel.graceful_shutdown unless @mongrel.nil?
		log :error , $!.message
		log :error, $!.backtrace
		log :error, "Retrying in 5 seconds..."
		sleep(5)
		retry
	end
end