Skip to content

Commit

Permalink
import antcat db and display sample of data in webpage
Browse files Browse the repository at this point in the history
  • Loading branch information
haydenclev committed Aug 14, 2024
1 parent 3883476 commit 873f0d2
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 15 deletions.
1 change: 1 addition & 0 deletions migration/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ COPY app.rb .
# Install the webrick gem
RUN gem install webrick
RUN gem install mysql2
RUN gem install timeout

# Expose port 8080 to be accessible from outside the container
EXPOSE 8080
Expand Down
10 changes: 10 additions & 0 deletions migration/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Temporary README to explain how to run docker in this subdirectory.

setup: unzip the db initialization file sql-data/init.sql.gz.
- it is too large for git to handle unless zipped
- make sure not to commit this change when pushing
start: docker-compose up --build -d
- d flag runs detached in background
- build processes any changes in the Dockerfile
stop: docker-compose down -v
- v flag removes volumes which is necessary when starting the next time
43 changes: 39 additions & 4 deletions migration/app.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,47 @@
require 'webrick'
require 'mysql2'
require 'timeout'

# Method to check if the database is up and running
def wait_for_db(host:, username:, password:, database:, timeout: 60, interval: 5)
start_time = Time.now

while Time.now - start_time < timeout
begin
Timeout.timeout(interval) do
client = Mysql2::Client.new(
host: host,
username: username,
password: password,
database: database
)
client.query('SELECT 1') # Simple query to check connection
client.close
return true
end
rescue Mysql2::Error => e
puts "Database connection failed: #{e.message}. Retrying..."
sleep interval
end
end

raise "Database did not become available within #{timeout} seconds."
end

# Wait for the MySQL database to be ready
wait_for_db(
host: 'db', # Docker Compose service name
username: 'root',
password: 'password',
database: 'antcat'
)

# Configure the MySQL client
client = Mysql2::Client.new(
host: 'db', # Docker Compose service name
host: 'db',
username: 'root',
password: 'password',
database: 'mydatabase'
database: 'antcat'
)

# Define the web server
Expand All @@ -15,10 +50,10 @@
# Define a response for the root path "/"
server.mount_proc '/' do |req, res|
# Query the database
results = client.query('SELECT * FROM mytable')
results = client.query('SELECT name FROM names LIMIT 100')

# Build the HTML response
html = '<html><body><h1>Data from MySQL</h1><ul>'
html = '<html><body><h1>behold.....ANTS!</h1><ul>'
results.each do |row|
html += "<li>#{row['name']}</li>"
end
Expand Down
1 change: 0 additions & 1 deletion migration/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ services:
ports:
- "3306:3306" # Port mapping
container_name: db # Name of the container
restart: unless-stopped # Restart policy
volumes:
- ./sql-data:/docker-entrypoint-initdb.d # Mount the initialization directory

Expand Down
10 changes: 0 additions & 10 deletions migration/sql-data/init.sql

This file was deleted.

Binary file added migration/sql-data/init.sql.gz
Binary file not shown.

0 comments on commit 873f0d2

Please sign in to comment.