Created
February 17, 2017 04:41
-
-
Save joey-g/97ca71d24c657845a42683ffecf68e91 to your computer and use it in GitHub Desktop.
Modular Sinatra
This file contains hidden or 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
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