forked from eladmeidar/sinatra_fake_webservice
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Elad Meidar
committed
Feb 19, 2010
1 parent
feca6d5
commit cdaaf58
Showing
7 changed files
with
383 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.