This repository has been archived by the owner on Jan 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
/
main.rb
185 lines (159 loc) · 4.94 KB
/
main.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
require 'sinatra'
require 'redis'
require 'json'
require 'mongo'
require 'mysql2'
require 'carrot'
require 'uri'
require 'pg'
class RackServiceApp < Sinatra::Base
get '/env' do
ENV['VCAP_SERVICES']
end
get '/rack/env' do
ENV['RACK_ENV']
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
if coll.find('_id' => params[:key]).to_a.empty?
coll.insert( { '_id' => params[:key], 'data_value' => value } )
else
coll.update( { '_id' => params[:key] }, { '_id' => params[:key], 'data_value' => value } )
end
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
key = params[:key]
result = client.query("select * from data_values where id='#{key}'")
if result.count > 0
client.query("update data_values set data_value='#{value}' where id='#{key}'")
else
client.query("insert into data_values (id, data_value) values('#{key}','#{value}');")
end
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_srs_service
write_to_rabbit(params[:key], value, client)
value
end
get '/service/rabbitmq/:key' do
client = rabbit_srs_service
read_from_rabbit(params[:key], client)
end
def load_redis
redis_service = load_service('redis')
Redis.new({:host => redis_service["hostname"], :port => redis_service["port"], :password => redis_service["password"]})
end
def load_mysql
mysql_service = load_service('mysql')
client = Mysql2::Client.new(:host => mysql_service['hostname'], :username => mysql_service['user'], :port => mysql_service['port'], :password => mysql_service['password'], :database => mysql_service['name'])
result = client.query("SELECT table_name FROM information_schema.tables WHERE table_name = 'data_values'");
client.query("Create table IF NOT EXISTS data_values ( id varchar(20), data_value varchar(20)); ") if result.count != 1
client
end
def load_mongo
mongodb_service = load_service('mongodb')
conn = Mongo::Connection.new(mongodb_service['hostname'], mongodb_service['port'])
db = conn[mongodb_service['db']]
coll = db['data_values'] if db.authenticate(mongodb_service['username'], mongodb_service['password'])
end
def load_postgresql
postgresql_service = load_service('postgresql')
client = PGconn.open(postgresql_service['host'], postgresql_service['port'], :dbname => postgresql_service['name'], :user => postgresql_service['username'], :password => postgresql_service['password'])
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 load_service(service_name)
services = JSON.parse(ENV['VCAP_SERVICES'])
service = nil
services.each do |k, v|
v.each do |s|
if k.split('-')[0].downcase == service_name.downcase
service = s["credentials"]
end
end
end
service
end
def rabbit_service
service = load_service('rabbitmq')
Carrot.new( :host => service['hostname'], :port => service['port'], :user => service['user'], :pass => service['pass'], :vhost => service['vhost'] )
end
def rabbit_srs_service
service = load_service('rabbitmq')
uri = URI.parse(service['url'])
host = uri.host
port = uri.port
user = uri.user
pass = uri.password
vhost = uri.path[1..uri.path.length]
Carrot.new( :host => host, :port => port, :user => user, :pass => pass, :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
end