forked from openshift/ruby-hello-world
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
68 lines (60 loc) · 1.21 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
require 'sinatra'
require_relative 'config/database'
require_relative 'models'
set :bind, '0.0.0.0'
set :port, 8080
configure do
puts "Running app file"
puts "Create database..."
%x"rake db:create"
puts "Run migrations..."
%x"rake db:migrate"
puts "Run app..."
if ENV['RACK_ENV']=="production"
while !self.connect_to_database_prod
puts "Connecting to production database...\n"
sleep 0.1
end
else
while !self.connect_to_database_test
puts "Connecting to test database...\n"
sleep 0.1
end
end
puts "Connected to database"
end
get '/' do
File.read('main.html')
end
get '/keys' do
a={}
KeyPair.all.each do |v|
a[v.key]=v.value
end
a.to_s
end
get '/keys/:id' do
if KeyPair.exists?(params[:id])
KeyPair.find(params[:id]).value
else
not_found "Key not found"
end
end
post '/keys/:id' do
if KeyPair.exists?(params[:id])
KeyPair.update(params['id'], value: params['value'])
"Key updated"
else
KeyPair.create(key:params[:id],value:params['value']).save
"Key created"
end
end
delete '/keys/:id' do
if KeyPair.exists?(params[:id])
v=KeyPair.find(params[:id])
v.destroy
"Key deleted"
else
"Key not found"
end
end