Skip to content

Commit

Permalink
Create and show database objects in when running the app
Browse files Browse the repository at this point in the history
  • Loading branch information
maria committed Oct 30, 2014
1 parent e4ec1d9 commit fef94eb
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 37 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*schema.rb
10 changes: 4 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
FROM openshift/ruby-20-centos

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

USER ruby
ADD app.rb /tmp/app.rb
ADD sinatra_app /tmp/
WORKDIR /tmp/

EXPOSE 8080
CMD ["ruby", "/tmp/app.rb"]
CMD ["ruby", "start.rb"]
31 changes: 0 additions & 31 deletions app.rb

This file was deleted.

2 changes: 2 additions & 0 deletions sinatra_app/Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require_relative 'app'
require 'sinatra/activerecord/rake'
15 changes: 15 additions & 0 deletions sinatra_app/app.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'sinatra'
require_relative 'config/environments'
require_relative 'models'

set :bind, '0.0.0.0'
set :port, ENV["FRONTEND_SERVICE_PORT"]

get '/' do
Timestamp.create(date: Time.now, text: "This is a message from a database query. The last insertion in the database was at")
"Hello World!\n"+
# ENV values are generated during template processing
# and then passed to the container when openshift launches it.
"All the environment variables are: #{ENV.map { |k,v| "#{k}=#{v}" }.join("\n")}]\n" +
"#{Timestamp.last().text} #{Timestamp.last().date}."
end
7 changes: 7 additions & 0 deletions sinatra_app/config/database.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
development:
adapter: mysql2
database: <%= ENV["MYSQL_DATABASE"] %>
username: root
password: <%= ENV['MYSQL_ROOT_PASSWORD'] %>
host: <%= ENV["DATABASE_SERVICE_IP_ADDR"] %>
port: <%= ENV["DATABASE_SERVICE_PORT"] %>
27 changes: 27 additions & 0 deletions sinatra_app/config/environments.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require 'sinatra/activerecord'

def self.connect_to_database
begin
ActiveRecord::Base.establish_connection(
:adapter => "mysql2",
:host => "#{ENV["DATABASE_SERVICE_IP_ADDR"]}",
:port => "#{ENV["DATABASE_SERVICE_PORT"]}",
:database => "#{ENV["MYSQL_DATABASE"]}",
:password => "#{ENV["MYSQL_ROOT_PASSWORD"]}"
)

ActiveRecord::Base.connection.active?

rescue Exception
return false
end
end

configure :development do

while !self.connect_to_database
puts "Connecting to database...\n"
sleep 0.1
end
puts "Connected to database"
end
12 changes: 12 additions & 0 deletions sinatra_app/db/migrate/1414364400_create_timestamp.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class CreateTimestamp < ActiveRecord::Migration
def up
create_table :timestamps do |t|
t.date :date
t.text :text
end
end

def down
drop_table :timestamps
end
end
4 changes: 4 additions & 0 deletions sinatra_app/models.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require 'active_record'

class Timestamp < ActiveRecord::Base
end
6 changes: 6 additions & 0 deletions sinatra_app/start.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
puts "Create database..."
%x"rake db:create"
puts "Run migrations..."
%x"rake db:migrate"
puts "Run app..."
%x"ruby app.rb &>/dev/null &"

0 comments on commit fef94eb

Please sign in to comment.