Skip to content

Commit

Permalink
Changed API a bit, simpler now
Browse files Browse the repository at this point in the history
  • Loading branch information
Elad Meidar committed Feb 19, 2010
1 parent feca6d5 commit cdaaf58
Show file tree
Hide file tree
Showing 7 changed files with 383 additions and 101 deletions.
16 changes: 16 additions & 0 deletions lib/sinatra_ext.rb
@@ -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
55 changes: 36 additions & 19 deletions lib/sinatra_webservice.rb
@@ -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
45 changes: 35 additions & 10 deletions lib/web_service.rb
@@ -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

0 comments on commit cdaaf58

Please sign in to comment.