require 'role' require 'sinatra/base' require 'db_crap' class RailsRole < Role PORT = 9300 role 'rails' def initialize(manifest) super init_links 'db_write', @manifest['db_write'] init_links 'db_read', @manifest['db_read'] init_links 'db_peer', @manifest['db_peer'] end def start # @manifest['db'].each do |db_addr| # @db_sockets[db_addr] = DemoSocket.connect(db_addr, DbRole::PORT) #end start_server end def stop @http_server.shutdown # @db_sockets.each { |a,s| s.close } end alias :enable :start alias :disable :stop private def start_server my_app = MyApp.new(@active_links, @manifest) trap('INT') do @http_server.shutdown if @http_server exit end Thread.new { Rack::Handler::WEBrick.run(my_app, :Port => PORT.to_s) do |server| @http_server = server end } end class MyApp < Sinatra::Base def initialize(active_links, manifest) @active_links = active_links @manifest = manifest super() end set :public, File.dirname(__FILE__) + '/../public' set :views, File.dirname(__FILE__) + '/../views' get '/one' do @list = list erb :one end get '/two' do @list = list erb :two end post '/' do write request.params['key'], request.params['value'] redirect request.params['return_to'] end get '/ping' do 'pong' end private include DbCrap end end