Permalink
Browse files

Changed API a bit, simpler now

  • Loading branch information...
Elad Meidar
Elad Meidar committed Feb 19, 2010
1 parent feca6d5 commit cdaaf58a8fc5517b6a35f9cd98239cae49421d2e
Showing with 383 additions and 101 deletions.
  1. +16 −0 lib/sinatra_ext.rb
  2. +36 −19 lib/sinatra_webservice.rb
  3. +35 −10 lib/web_service.rb
  4. +273 −0 sinatra.log
  5. +3 −2 test/helper.rb
  6. +20 −31 test/test_sinatra_fake_webservice.rb
  7. +0 −39 test/test_web_service.rb
View
@@ -0,0 +1,16 @@
+# Disabling the capture of the Errno::EADDRINUSE exception in #run! so we can make WebService catch it.
+# class Sinatra::Base
+# def self.run!(options={})
+# attempts = 0
+# set options
+# handler = detect_rack_handler
+# handler_name = handler.name.gsub(/.*::/, '')
+# handler.run self, :Host => bind, :Port => port do |server|
+# trap(:INT) do
+# ## Use thins' hard #stop! if available, otherwise just #stop
+# server.respond_to?(:stop!) ? server.stop! : server.stop
+# end
+# set :running, true
+# end
+# end
+# end
View
@@ -1,36 +1,53 @@
class SinatraWebService
- attr_reader :services
-
- def initialize
-
- end
- def running?
- false
+ attr_accessor :host, :port
+
+ class SinatraStem < Sinatra::Base
+ get '/' do
+ "Hello! i am lindesy lohan!"
+ end
end
- def initialize
- @services = []
+
+ def initialize(options = {})
+ @host = options[:host] ||= 'localhost'
+ @port = options[:port] ||= 4567
end
- def services
- @services.dup.freeze
+ def running?
+ false
end
def run!
- @services.each do |service|
- puts "== Loading service #{service.name}"
- service.run
+ @port = find_free_port
+
+ app = Thread.new("running_app") do
+ SinatraStem.run! :post => @host, :port => @port.to_i
+ sleep 1
end
end
- def add_service(service = nil)
- raise "Tried to add an empty service" if service.nil?
- raise "Only WebService Objects are allowed" if !(service.is_a?(WebService))
-
- @services << service
+ def find_free_port
+ found = false
+ attempts = 0
+ while !found and attempts < 10
+ puts "\n== Trying port #{@port}"
+ begin
+ res = Net::HTTP.start(host,port) do |http|
+ http.get('/')
+ end
+ attempts += 1
+ @port = @port.succ
+ rescue Errno::ECONNREFUSED
+ return @port
+ end
+ end
+ end
+
+ def method_missing(method, *args, &block)
+ SinatraStem.send(method, *args, &block) unless method.to_s == "proxy"
end
end
View
@@ -1,35 +1,60 @@
+require 'net/http'
class WebService
- class App < Sinatra::Base
+ class SinatraStem < Sinatra::Base
get '/' do
"Hello! i am lindesy lohan!"
end
end
- attr_accessor :port, :host, :name
+ attr_accessor :port, :host, :name, :sinatra_app
# Initialize a new face service
def initialize(options = {})
@name = options[:name] ||= "App"
@port = options[:port] ||= "4567"
@host = options[:host] ||= "localhost"
+ @sinatra_app = Sinatra::Base.new
+ end
+
+ def proxy
+ @sinatra_app
end
def run
+
+ @port = find_free_port
+
app = Thread.new("running_#{self.name}") do
- silence_stream(STDOUT) do
- App.run! :host => @host, :port => @port
+ puts "\n == Starting webservice '#{@name}' on port #{@port}"
+ proxy.run! :post => @host, :port => @port.to_i
+ sleep 1
+ end
+ end
+
+ def find_free_port
+ found = false
+ attempts = 0
+ while !found and attempts < 10
+ puts "\n== Trying port #{@port}"
+ begin
+ res = Net::HTTP.start(host,port) do |http|
+ http.get('/')
+ end
+ attempts += 1
+ @port = @port.succ
+ rescue Errno::ECONNREFUSED
+ return @port
end
end
- sleep 1
+ end
+
+ def proxy
+ @sinatra_app
end
def method_missing(method, *args, &block)
- if App.respond_to?(method)
- App.send(method, args, block)
- else
- super
- end
+ proxy.send(method, *args, &block) unless method.to_s == "proxy"
end
end
Oops, something went wrong.

0 comments on commit cdaaf58

Please sign in to comment.