From 38834761bb68493922c717545f687f7f0ed80756 Mon Sep 17 00:00:00 2001 From: Hayden Date: Tue, 13 Aug 2024 16:31:41 -0700 Subject: [PATCH] add basic ruby and sql files --- migration/Dockerfile | 19 ++++++++++++++++++ migration/app.rb | 37 ++++++++++++++++++++++++++++++++++++ migration/docker-compose.yml | 23 ++++++++++++++++++++++ migration/sql-data/init.sql | 10 ++++++++++ 4 files changed, 89 insertions(+) create mode 100644 migration/Dockerfile create mode 100644 migration/app.rb create mode 100644 migration/docker-compose.yml create mode 100644 migration/sql-data/init.sql diff --git a/migration/Dockerfile b/migration/Dockerfile new file mode 100644 index 000000000..600115063 --- /dev/null +++ b/migration/Dockerfile @@ -0,0 +1,19 @@ +# Use the official Ruby image from Docker Hub +FROM ruby:latest + +# Set the working directory inside the container +WORKDIR /usr/src/app + +# Copy the Ruby script into the container +COPY app.rb . + +# Install the webrick gem +RUN gem install webrick +RUN gem install mysql2 + +# Expose port 8080 to be accessible from outside the container +EXPOSE 8080 + +# Define the command to run the script +CMD ["ruby", "app.rb"] + diff --git a/migration/app.rb b/migration/app.rb new file mode 100644 index 000000000..352896c51 --- /dev/null +++ b/migration/app.rb @@ -0,0 +1,37 @@ +require 'webrick' +require 'mysql2' + +# Configure the MySQL client +client = Mysql2::Client.new( + host: 'db', # Docker Compose service name + username: 'root', + password: 'password', + database: 'mydatabase' +) + +# Define the web server +server = WEBrick::HTTPServer.new(Port: 8080) + +# Define a response for the root path "/" +server.mount_proc '/' do |req, res| + # Query the database + results = client.query('SELECT * FROM mytable') + + # Build the HTML response + html = '

Data from MySQL

' + + res.body = html +end + +# Trap interrupts (Ctrl+C) to gracefully shut down the server +trap 'INT' do + server.shutdown +end + +# Start the server +server.start + diff --git a/migration/docker-compose.yml b/migration/docker-compose.yml new file mode 100644 index 000000000..fc1bb5a87 --- /dev/null +++ b/migration/docker-compose.yml @@ -0,0 +1,23 @@ + +services: + db: # The name of your service + image: mysql/mysql-server:latest # The image to use + environment: # Environment variables + - MYSQL_ROOT_HOST=% + - MYSQL_ROOT_PASSWORD=password + 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 + + app: + depends_on: + - db + build: + context: . + dockerfile: Dockerfile + ports: + - "8080:8080" # Map port 8080 of the container to port 8080 on the host + container_name: ruby-hello-world diff --git a/migration/sql-data/init.sql b/migration/sql-data/init.sql new file mode 100644 index 000000000..956063b4f --- /dev/null +++ b/migration/sql-data/init.sql @@ -0,0 +1,10 @@ +CREATE DATABASE IF NOT EXISTS mydatabase; +USE mydatabase; + +CREATE TABLE IF NOT EXISTS mytable ( + id INT AUTO_INCREMENT PRIMARY KEY, + name VARCHAR(255) NOT NULL +); + +INSERT INTO mytable (name) VALUES ('Hello, World!'); +