Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

140 lines (116 sloc) 3.395 kB
require 'sinatra'
require 'redis'
require 'json'
require 'mongo'
require 'mysql2'
require 'carrot'
require 'uri'
require 'pg'
get '/env' do
ENV['VCAP_SERVICES']
end
get '/' do
'hello from sinatra'
end
get '/crash' do
Process.kill("KILL", Process.pid)
end
get '/service/redis/:key' do
redis = load_redis
redis[params[:key]]
end
post '/service/redis/:key' do
redis = load_redis
redis[params[:key]] = request.env["rack.input"].read
end
post '/service/mongo/:key' do
coll = load_mongo
value = request.env["rack.input"].read
coll.insert( { '_id' => params[:key], 'data_value' => value } )
value
end
get '/service/mongo/:key' do
coll = load_mongo
coll.find('_id' => params[:key]).to_a.first['data_value']
end
not_found do
'This is nowhere to be found.'
end
post '/service/mysql/:key' do
client = load_mysql
value = request.env["rack.input"].read
result = client.query("insert into data_values (id, data_value) values('#{params[:key]}','#{value}');")
client.close
value
end
get '/service/mysql/:key' do
client = load_mysql
result = client.query("select data_value from data_values where id = '#{params[:key]}'")
value = result.first['data_value']
client.close
value
end
post '/service/postgresql/:key' do
client = load_postgresql
value = request.env["rack.input"].read
client.query("insert into data_values (id, data_value) values('#{params[:key]}','#{value}');")
client.close
value
end
get '/service/postgresql/:key' do
client = load_postgresql
value = client.query("select data_value from data_values where id = '#{params[:key]}'").first['data_value']
client.close
value
end
post '/service/rabbit/:key' do
value = request.env["rack.input"].read
client = rabbit_service
write_to_rabbit(params[:key], value, client)
value
end
get '/service/rabbit/:key' do
client = rabbit_service
read_from_rabbit(params[:key], client)
end
post '/service/rabbitmq/:key' do
value = request.env["rack.input"].read
client = rabbit_service
write_to_rabbit(params[:key], value, client)
value
end
get '/service/rabbitmq/:key' do
client = rabbit_service
read_from_rabbit(params[:key], client)
end
def load_redis
Redis.new({:host => '127.0.0.1', :port => 6379, :password => 'testpw'})
end
def load_mysql
client = Mysql2::Client.new(:host => '127.0.0.1', :username =>'testuser', :port => 3306, :password => 'testpw', :database => 'testdb')
client.query("Create table IF NOT EXISTS data_values ( id varchar(20), data_value varchar(20)); ")
client
end
def load_mongo
conn = Mongo::Connection.new('127.0.0.1', 4567)
db = conn['testdb']
coll = db['data_values'] #if db.authenticate(mongodb_service['username'], mongodb_service['password'])
end
def load_postgresql
client = PGconn.open('127.0.0.1', '8675', :dbname => 'testdb', :user => 'testuser', :password => 'testpw')
client.query("create table data_values (id varchar(20), data_value varchar(20));") if client.query("select * from information_schema.tables where table_name = 'data_values';").first.nil?
client
end
def rabbit_service
Carrot.new( :host => '127.0.0.1', :port => 12345, :user => 'testuser', :pass => 'testpass', :vhost => 'vhost' )
end
def write_to_rabbit(key, value, client)
q = client.queue(key)
q.publish(value)
end
def read_from_rabbit(key, client)
q = client.queue(key)
msg = q.pop(:ack => true)
q.ack
msg
end
Jump to Line
Something went wrong with that request. Please try again.