Skip to content

Commit

Permalink
add basic ruby and sql files
Browse files Browse the repository at this point in the history
  • Loading branch information
haydenclev committed Aug 13, 2024
1 parent 6e476ac commit 3883476
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 0 deletions.
19 changes: 19 additions & 0 deletions migration/Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]

37 changes: 37 additions & 0 deletions migration/app.rb
Original file line number Diff line number Diff line change
@@ -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 = '<html><body><h1>Data from MySQL</h1><ul>'
results.each do |row|
html += "<li>#{row['name']}</li>"
end
html += '</ul></body></html>'

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

23 changes: 23 additions & 0 deletions migration/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions migration/sql-data/init.sql
Original file line number Diff line number Diff line change
@@ -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!');

0 comments on commit 3883476

Please sign in to comment.