-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
70 lines (65 loc) · 1.76 KB
/
app.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
require 'sinatra'
require 'pg'
require 'json'
require 'redis'
class CheckApp < Sinatra::Base
get '/' do
content_type :json
{
message: 'Welcome to the Sinatra App!',
routes: {
check_db: '/check/db',
check_redis: '/check/redis'
}
}.to_json
end
get '/check' do
content_type :json
{
message: 'Welcome to the Sinatra App!',
routes: {
check_db: '/check/db',
check_redis: '/check/redis'
}
}.to_json
end
get '/check/db' do
begin
connection = PG.connect(
dbname: ENV['POSTGRESQL_DATABASE'],
user: ENV['POSTGRESQL_USERNAME'],
password: ENV['POSTGRESQL_PASSWORD'],
host: ENV['POSTGRESQL_ADDRESS'],
port: ENV['POSTGRESQL_PORT'] || 5432,
connect_timeout: ENV['POSTGRESQL_TIMEOUT'] || 5
)
connection.exec("SELECT 1")
connection.close
status 200
content_type :json
{ status: 'success', message: 'Connection to PostgreSQL is successful' }.to_json
rescue PG::Error => e
status 500
content_type :json
error_response = { status: 'error', message: e.message }
error_response[:env] = ENV.to_hash if params['secret'] == ENV['ENV_SECRET']
error_response.to_json
end
end
get '/check/redis' do
begin
redis = Redis.new(url: ENV['REDIS_URL'])
redis.ping
status 200
content_type :json
{ status: 'success', message: 'Connection to Redis is successful' }.to_json
rescue Redis::BaseError => e
status 500
content_type :json
error_response = { status: 'error', message: e.message }
error_response[:env] = ENV.to_hash if params['secret'] == ENV['ENV_SECRET']
error_response.to_json
end
end
run! if app_file == $0
end