forked from openshift/ruby-hello-world
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create and show database objects in when running the app
- Loading branch information
Showing
10 changed files
with
78 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
*schema.rb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
require_relative 'app' | ||
require 'sinatra/activerecord/rake' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
require 'active_record' | ||
|
||
class Timestamp < ActiveRecord::Base | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 &" |