uwa 0.2 → 0.3

Sign up to get free protection for your applications and to get access to all the features.
data/bin/uwa CHANGED
@@ -1,58 +1,24 @@
1
1
  #!/usr/bin/ruby
2
2
  require 'rubygems'
3
3
  require 'uwa'
4
- require 'getoptlong'
5
4
 
6
- CONFIG = {
7
- :host => '0.0.0.0',
8
- :port => '42080',
9
- :dynamic => false,
10
- :debug => false,
5
+ SCRIPT =<<EOF
6
+ #!/usr/bin/ruby
7
+ require 'rubygems'
8
+ require 'uwa'
9
+
10
+ UWA::Server << {
11
+ :name => 'uwa_hello',
12
+ :path => '/hello'
11
13
  }
12
14
 
13
- OPTIONS = [
14
- [ '--help', '-h', GetoptLong::NO_ARGUMENT, 'Display this help' ],
15
- [ '--host', '-b', GetoptLong::REQUIRED_ARGUMENT, 'Set host/ip' ],
16
- [ '--port', '-p', GetoptLong::REQUIRED_ARGUMENT, 'Set port' ],
17
- [ '--dynamic', '-r', GetoptLong::NO_ARGUMENT, 'Enable dynamic mode (auto reload ressources only)' ],
18
- [ '--debug', '-d', GetoptLong::NO_ARGUMENT, 'Enable debug mode' ],
19
- ]
15
+ UWA::Server.run
16
+ EOF
20
17
 
21
- opts = GetoptLong.new(*OPTIONS.collect{|o| o[0..-2]})
22
- opts.each do |opt, arg|
23
- case opt
24
- when '--help'
25
- puts "Usage #{File.basename(__FILE__)} [OPTION]"
26
- OPTIONS.each do |opt|
27
- default = CONFIG[opt.first[2..-1].to_sym]
28
- default = default.nil? ? '' : "(default = #{default})"
29
- puts "%2s, %-15s: %s %s" % [opt[1], opt[0], opt[3], default]
30
- end
31
- exit
32
- when '--host'
33
- CONFIG[:host] = arg
34
- when '--port'
35
- CONFIG[:port] = arg
36
- when '--dynamic'
37
- CONFIG[:dynamic] = true
38
- when '--debug'
39
- CONFIG[:debug] = true
40
- $mongrel_debug_client = true
41
- end
18
+ name = ARGV.first || 'myuwa'
19
+ File.open(name, 'w') do |fd|
20
+ fd.puts SCRIPT
42
21
  end
43
22
 
44
- begin
45
- m = Mongrel::HttpServer.new(CONFIG[:host], CONFIG[:port])
46
- UWA::Plugin.load.each do |name, config|
47
- widget = config[:class].new
48
- widget.script = config[:script]
49
- widget.css = config[:css]
50
- m.register('/' + name, widget)
51
- end
52
- m.register('/proxy', UWA::Proxy.new)
53
- m.run.join
54
- rescue
55
- STDERR.puts "Error: #{$!.message}"
56
- STDERR.puts "Retrying..."
57
- retry
58
- end
23
+ puts "Your UWA server is ready\nJust edit and launch the file #{name.inspect}"
24
+
data/lib/uwa.rb CHANGED
@@ -3,3 +3,4 @@ require 'builder'
3
3
  require 'uwa/proxy'
4
4
  require 'uwa/handler'
5
5
  require 'uwa/plugin'
6
+ require 'uwa/server'
data/lib/uwa/config.rb CHANGED
@@ -1,8 +1,10 @@
1
1
  module UWA
2
2
  NAME = 'UWA'
3
- VERSION = '0.2'
3
+ VERSION = '0.3'
4
4
  DESC = 'Widget server for UWA specs by Netvibes (http://dev.netvibes.com/doc/universal_widget_api)'
5
5
  AUTHOR = 'Florent Solt'
6
6
  EMAIL = 'florent@solt.biz'
7
7
  HOMEPAGE = 'http://gnetvibes.rubyforge.org'
8
+ LICENSE = 'BSD'
9
+ COPYRIGHT = 'Copyright (C) 2007 Florent Solt'
8
10
  end
data/lib/uwa/handler.rb CHANGED
@@ -18,29 +18,29 @@ class Handler < Mongrel::HttpHandler
18
18
  @css = []
19
19
  @script = []
20
20
  @cache = {:css => nil, :script => nil}
21
- end
22
21
 
23
- def log(title, msg)
24
- return if not CONFIG[:debug]
25
- if msg.is_a? Array
26
- msg.each { |l| STDERR.puts " " + l }
27
- else
28
- STDERR.puts "#{Time.now}: #{title.to_s.upcase}: #{msg}"
29
- end
30
- STDERR.flush
22
+ @query = nil
23
+ @out = nil
24
+ @response = nil
25
+ @request = nil
31
26
  end
32
27
 
33
28
  def process(req,res)
34
- log(req.params['REQUEST_METHOD'], req.params['REQUEST_URI'])
35
- query = query_string(req.params['QUERY_STRING'])
36
- log(:QUERY, query.inspect)
37
- if req.params['REQUEST_PATH'].split('/', 3)[2].nil? and query['url'].nil?
38
- res.start(200) do |head, out|
29
+ @request = req
30
+ @response = res
31
+ UWA::Server.log(@request.params['REQUEST_METHOD'], @request.params['REQUEST_URI'])
32
+ @query = query_string(req.params['QUERY_STRING'])
33
+ UWA::Server.log(:QUERY, @query.inspect)
34
+ if @request.params['REQUEST_PATH'].split('/', 3)[2].nil? and @query['url'].nil?
35
+ @response.start(200) do |head, out|
39
36
  head["Content-Type"] = "text/html"
40
37
  xml = Builder::XmlMarkup.new(:indent => 4, :target => out)
41
38
  xml.instruct!
42
- xml.declare! :DOCTYPE, :html, :PUBLIC, '-//W3C//DTD XHTML 1.0 Strict//EN', 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'
43
- xml.html(:xmlns => 'http://www.w3.org/1999/xhtml', 'xmlns:widget' => 'http://www.netvibes.com/ns/') do |xml|
39
+ xml.declare! :DOCTYPE, :html, :PUBLIC,
40
+ '-//W3C//DTD XHTML 1.0 Strict//EN',
41
+ 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'
42
+ xml.html(:xmlns => 'http://www.w3.org/1999/xhtml',
43
+ 'xmlns:widget' => 'http://www.netvibes.com/ns/') do |xml|
44
44
  xml.head do |xml|
45
45
  xml.meta(:name => 'author', :content => @author)
46
46
  xml.meta(:name => 'description', :content => @description)
@@ -48,8 +48,10 @@ class Handler < Mongrel::HttpHandler
48
48
  xml.meta(:name => 'inline', :content => @inline.to_s)
49
49
  xml.meta(:name => 'debugMode', :content => @debugMode.to_s)
50
50
  xml.meta(:name => 'keywords', :content => @keywords)
51
- xml.link(:rel => :stylesheet, :type => 'text/css', :href => 'http://www.netvibes.com/themes/uwa/style.css')
52
- xml.script("", :type => 'text/javascript', :src => 'http://www.netvibes.com/js/UWA/load.js.php?env=Standalone')
51
+ xml.link(:rel => :stylesheet, :type => 'text/css',
52
+ :href => 'http://www.netvibes.com/themes/uwa/style.css')
53
+ xml.script("", :type => 'text/javascript',
54
+ :src => 'http://www.netvibes.com/js/UWA/load.js.php?env=Standalone')
53
55
  xml.tag!('widget:preferences') do |xml|
54
56
  @preferences.each do |pref|
55
57
  xml.preference(pref)
@@ -62,12 +64,15 @@ class Handler < Mongrel::HttpHandler
62
64
  url = 'http://' + req.params['HTTP_HOST'] + req.params['REQUEST_PATH'] + '/'
63
65
  xml << "widget.remoteUrl = '#{url}'\n"
64
66
  ['Text', 'Json', 'Xml', 'Feed'].each do |m|
65
- xml << "widget.remote#{m} = function(uri, cb) { UWA.Data.get#{m}('#{url}' + uri, cb); };\n"
67
+ xml << "widget.remote#{m} = function(uri, cb) {
68
+ UWA.Data.get#{m}(widget.remoteUrl + uri, cb); };\n"
66
69
  end
67
70
  script.each do |s|
68
71
  xml << "\n#{s}\n"
69
72
  end
70
- xml << "if (document.location && document.location.hostname == 'localhost') { UWA.proxies.ajax = '/proxy'; }\n"
73
+ xml << "if (document.location && document.location.hostname == 'localhost') {
74
+ UWA.proxies.ajax = '/proxy';
75
+ UWA.Data.useJsonRequest = false; }\n"
71
76
  end
72
77
  end
73
78
  xml.body do |xml|
@@ -77,14 +82,16 @@ class Handler < Mongrel::HttpHandler
77
82
  xml.target!
78
83
  end
79
84
  else
80
- method = req.params['REQUEST_PATH'].split('/', 3)[2].to_sym
85
+ method = @request.params['REQUEST_PATH'].split('/', 3)[2].to_sym
81
86
  if self.respond_to?(method)
82
87
  res.start(200) do |head, out|
83
88
  begin
84
- self.send(method, query, head, out)
89
+ @headers = head
90
+ @out = out
91
+ self.send(method)
85
92
  rescue Exception
86
- log(:error, "#{$!.message} <#{$!.class.name}>")
87
- log(:error, $!.backtrace)
93
+ UWA::Server.log(:error, "#{$!.message} <#{$!.class.name}>")
94
+ UWA::Server.log(:error, $!.backtrace)
88
95
  end
89
96
  end
90
97
  else
@@ -95,15 +102,21 @@ class Handler < Mongrel::HttpHandler
95
102
  end
96
103
  end
97
104
 
105
+ protected
106
+
107
+ def <<(buf)
108
+ @out.write(buf)
109
+ end
110
+
98
111
  def script
99
- if @cache[:script].to_a.empty? or CONFIG[:dynamic]
112
+ if @cache[:script].to_a.empty? or UWA::Server.reload
100
113
  @cache[:script] = @script.collect { |s| File.read(s) }
101
114
  end
102
115
  @cache[:script].to_a
103
116
  end
104
117
 
105
118
  def css
106
- if @cache[:css].to_a.empty? or CONFIG[:dynamic]
119
+ if @cache[:css].to_a.empty? or UWA::Server.reload
107
120
  @cache[:css] = @css.collect { |s| File.read(s) }
108
121
  end
109
122
  @cache[:css].to_a
data/lib/uwa/proxy.rb CHANGED
@@ -6,22 +6,28 @@ module UWA
6
6
  class Proxy < Mongrel::HttpHandler
7
7
 
8
8
  def process(req,res)
9
- query = CGI::parse(req.params['QUERY_STRING'])
10
- url = URI.parse(query['url'].to_a.first)
11
- if req.params['REMOTE_ADDR'] == '127.0.0.1'
12
- res.start(200) do |head, out|
13
- head["Content-Type"] = "text/html"
9
+ query = CGI::parse(req.params['QUERY_STRING'].to_s)
10
+ url = query['url'].to_a.first
11
+ if url.nil?
12
+ UWA::Server.log :ignore, req.params['REQUEST_URI']
13
+ else
14
+ url = URI.parse(query['url'].to_a.first.to_s.strip)
15
+ if req.params['REMOTE_ADDR'] == '127.0.0.1'
16
+ res.start(200) do |head, out|
17
+ head["Content-Type"] = "text/xml"
14
18
  unless url.nil?
15
- STDERR.puts "#{Time.now}: PROXY: #{url}" if CONFIG[:debug]
19
+ UWA::Server.log :proxy, url
16
20
  res = Net::HTTP.start(url.host, url.port) do |http|
17
- http.get(url.path)
21
+ http.get(url.request_uri)
18
22
  end
19
23
  out.write(res.body)
20
24
  end
25
+ end
26
+ else
27
+ UWA::Server.log :denied, "#{url} from #{req.params['REMOTE_ADDR']}"
21
28
  end
22
- else
23
- STDERR.puts "#{Time.now}: DENIED: #{url} from #{req.params['REMOTE_ADDR']}" if CONFIG[:debug]
24
29
  end
25
30
  end
31
+
26
32
  end
27
33
  end
data/lib/uwa/server.rb ADDED
@@ -0,0 +1,68 @@
1
+ require 'singleton'
2
+
3
+ module UWA
4
+ class Server
5
+ include Singleton
6
+
7
+ def self.method_missing(name, *args)
8
+ self.instance.send(name, *args)
9
+ end
10
+
11
+ attr_accessor :host, :port, :debug, :reload, :plugins
12
+
13
+ def initialize
14
+ @host = '0.0.0.0'
15
+ @port = 42080
16
+ @debug = true
17
+ @reload = true
18
+ @widgets = []
19
+ @plugins = UWA::Plugin.load
20
+ end
21
+
22
+ def <<(config)
23
+ begin
24
+ @widgets << @plugins[config[:name]].merge(config)
25
+ rescue
26
+ log(:warning, "Unable to register #{config[:name].inspect} at #{config[:path].inspect}")
27
+ else
28
+ log(:info, "Register #{config[:name].inspect} at #{config[:path].inspect}")
29
+ end
30
+ end
31
+
32
+ def run
33
+ m = nil
34
+ begin
35
+ $mongrel_debug_client = @debug
36
+ m = Mongrel::HttpServer.new(@host, @port) if m.nil?
37
+ m.register('/', UWA::Proxy.new)
38
+ @widgets.each do |widget|
39
+ object = widget[:class].new
40
+ object.script = widget[:script]
41
+ object.css = widget[:css]
42
+ widget[:params].to_a.each do |name, value|
43
+ object.instance_variable_set(('@' + name.to_s).to_sym, value)
44
+ end
45
+ m.register(widget[:path], object)
46
+ end
47
+ m.run.join
48
+ rescue
49
+ m.graceful_shutdown unless m.nil?
50
+ log :error , $!.message
51
+ log :error, $!.backtrace
52
+ log :error, "Retrying in 5 seconds..."
53
+ sleep(5)
54
+ retry
55
+ end
56
+ end
57
+
58
+ def log(level, msg)
59
+ return unless @debug
60
+ if msg.is_a? Array
61
+ msg.each { |l| STDERR.puts " " + l }
62
+ else
63
+ STDERR.puts "#{Time.now}: #{level.to_s.upcase}: #{msg}"
64
+ end
65
+ STDERR.flush
66
+ end
67
+ end
68
+ end
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.2
3
3
  specification_version: 1
4
4
  name: uwa
5
5
  version: !ruby/object:Gem::Version
6
- version: "0.2"
7
- date: 2007-03-28 00:00:00 +02:00
6
+ version: "0.3"
7
+ date: 2007-03-29 00:00:00 +02:00
8
8
  summary: Widget server for UWA specs by Netvibes (http://dev.netvibes.com/doc/universal_widget_api)
9
9
  require_paths:
10
10
  - lib
@@ -29,6 +29,7 @@ post_install_message:
29
29
  authors:
30
30
  - Florent Solt
31
31
  files:
32
+ - lib/uwa/server.rb
32
33
  - lib/uwa/handler.rb
33
34
  - lib/uwa/config.rb
34
35
  - lib/uwa/plugin.rb