Skip to content

Instantly share code, notes, and snippets.

@joey-g
Created February 17, 2017 04:41
Show Gist options
  • Save joey-g/97ca71d24c657845a42683ffecf68e91 to your computer and use it in GitHub Desktop.
Save joey-g/97ca71d24c657845a42683ffecf68e91 to your computer and use it in GitHub Desktop.
Modular Sinatra
require 'sinatra/base'
require 'rack'
# http://recipes.sinatrarb.com/p/embed/event-machine
class WebService < Sinatra::Base
def initialize(test_obj)
super()
@test_obj = test_obj
end
get '/' do
puts @test_obj
@test_obj.inspect
end
end
class Test; end
def run_svc(port)
web_svc = WebService.new(Test.new)
dispatch = Rack::Builder.app do
map '/' do
run web_svc
end
end
server_opts = {
app: dispatch,
server: 'thin',
Host: '0.0.0.0',
Port: port,
signals: false,
}
Rack::Server.start server_opts
end
threads = []
threads << Thread.new { run_svc '4567' }
threads << Thread.new { run_svc '4568' }
threads.each(&:join)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment