Skip to content

Commit fef94eb

Browse files
committed
Create and show database objects in when running the app
1 parent e4ec1d9 commit fef94eb

10 files changed

+78
-37
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*schema.rb

Dockerfile

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
FROM openshift/ruby-20-centos
22

3-
# Install mysql client and gems to connect app to database.
4-
USER root
5-
RUN yum install -y mysql && gem install sinatra activerecord mysql2 --no-ri --no-rdoc
3+
RUN gem install sinatra sinatra-activerecord mysql2 --no-ri --no-rdoc
64

7-
USER ruby
8-
ADD app.rb /tmp/app.rb
5+
ADD sinatra_app /tmp/
6+
WORKDIR /tmp/
97

108
EXPOSE 8080
11-
CMD ["ruby", "/tmp/app.rb"]
9+
CMD ["ruby", "start.rb"]

app.rb

-31
This file was deleted.

sinatra_app/Rakefile

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
require_relative 'app'
2+
require 'sinatra/activerecord/rake'

sinatra_app/app.rb

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
require 'sinatra'
2+
require_relative 'config/environments'
3+
require_relative 'models'
4+
5+
set :bind, '0.0.0.0'
6+
set :port, ENV["FRONTEND_SERVICE_PORT"]
7+
8+
get '/' do
9+
Timestamp.create(date: Time.now, text: "This is a message from a database query. The last insertion in the database was at")
10+
"Hello World!\n"+
11+
# ENV values are generated during template processing
12+
# and then passed to the container when openshift launches it.
13+
"All the environment variables are: #{ENV.map { |k,v| "#{k}=#{v}" }.join("\n")}]\n" +
14+
"#{Timestamp.last().text} #{Timestamp.last().date}."
15+
end

sinatra_app/config/database.yml

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
development:
2+
adapter: mysql2
3+
database: <%= ENV["MYSQL_DATABASE"] %>
4+
username: root
5+
password: <%= ENV['MYSQL_ROOT_PASSWORD'] %>
6+
host: <%= ENV["DATABASE_SERVICE_IP_ADDR"] %>
7+
port: <%= ENV["DATABASE_SERVICE_PORT"] %>

sinatra_app/config/environments.rb

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
require 'sinatra/activerecord'
2+
3+
def self.connect_to_database
4+
begin
5+
ActiveRecord::Base.establish_connection(
6+
:adapter => "mysql2",
7+
:host => "#{ENV["DATABASE_SERVICE_IP_ADDR"]}",
8+
:port => "#{ENV["DATABASE_SERVICE_PORT"]}",
9+
:database => "#{ENV["MYSQL_DATABASE"]}",
10+
:password => "#{ENV["MYSQL_ROOT_PASSWORD"]}"
11+
)
12+
13+
ActiveRecord::Base.connection.active?
14+
15+
rescue Exception
16+
return false
17+
end
18+
end
19+
20+
configure :development do
21+
22+
while !self.connect_to_database
23+
puts "Connecting to database...\n"
24+
sleep 0.1
25+
end
26+
puts "Connected to database"
27+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class CreateTimestamp < ActiveRecord::Migration
2+
def up
3+
create_table :timestamps do |t|
4+
t.date :date
5+
t.text :text
6+
end
7+
end
8+
9+
def down
10+
drop_table :timestamps
11+
end
12+
end

sinatra_app/models.rb

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
require 'active_record'
2+
3+
class Timestamp < ActiveRecord::Base
4+
end

sinatra_app/start.rb

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
puts "Create database..."
2+
%x"rake db:create"
3+
puts "Run migrations..."
4+
%x"rake db:migrate"
5+
puts "Run app..."
6+
%x"ruby app.rb &>/dev/null &"

0 commit comments

Comments
 (0)