diff --git a/.env b/.env new file mode 100644 index 0000000..2a8ff87 --- /dev/null +++ b/.env @@ -0,0 +1,4 @@ +MYSQL_ROOT_PASSWORD=password +MYSQL_DATABASE=testdb +MYSQL_USER=testuser +MYSQL_PASSWORD=password diff --git a/data.csv b/data.csv new file mode 100644 index 0000000..b8dac44 --- /dev/null +++ b/data.csv @@ -0,0 +1,4 @@ +name,age +Alice,30 +Bob,25 +Charlie,35 diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..4587167 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,40 @@ +version: "3.8" + +services: + db: + image: mariadb + environment: + MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD} + MYSQL_DATABASE: ${MYSQL_DATABASE} + MYSQL_USER: ${MYSQL_USER} + MYSQL_PASSWORD: ${MYSQL_PASSWORD} + networks: + - mynetwork + healthcheck: + test: ["CMD", "mysqladmin", "ping", "--silent"] + interval: 10s + retries: 5 + + filler: + build: ./filler + depends_on: + db: + condition: service_healthy + volumes: + - ./data.csv:/app/data.csv + networks: + - mynetwork + + web: + build: ./web + ports: + - "8000:8000" + depends_on: + - db + - filler + networks: + - mynetwork + +networks: + mynetwork: + driver: bridge diff --git a/filler/Dockerfile b/filler/Dockerfile new file mode 100644 index 0000000..7cfd7c7 --- /dev/null +++ b/filler/Dockerfile @@ -0,0 +1,10 @@ +FROM python:3.9-slim + +WORKDIR /app + +COPY requirements.txt ./ +RUN pip install --no-cache-dir -r requirements.txt + +COPY fill_db.py ./ + +ENTRYPOINT ["python", "fill_db.py"] diff --git a/filler/filler_db.py b/filler/filler_db.py new file mode 100644 index 0000000..c1f4b6d --- /dev/null +++ b/filler/filler_db.py @@ -0,0 +1,32 @@ +import os +import time +import pymysql +import csv + +db_config = { + 'host': 'db', + 'user': os.getenv('MYSQL_USER'), + 'password': os.getenv('MYSQL_PASSWORD'), + 'db': os.getenv('MYSQL_DATABASE'), +} + +while True: + print("Try connect to db") + try: + connection = pymysql.connect(**db_config) + break + except pymysql.MySQLError: + time.sleep(3) + +print("") + +with connection.cursor() as cursor, open('data.csv', 'r') as csv_file: + reader = csv.DictReader(csv_file) + for row in reader: + cursor.execute("INSERT INTO data (name, age) VALUES (%s, %s)", (row['name'], row['age'])) + connection.commit() + +with connection.cursor() as cursor: + cursor.execute("SELECT * FROM data") + for row in cursor.fetchall(): + print(row) diff --git a/web/Dockerfile b/web/Dockerfile new file mode 100644 index 0000000..be087eb --- /dev/null +++ b/web/Dockerfile @@ -0,0 +1,11 @@ +# ./web/Dockerfile +FROM python:3.9-slim + +WORKDIR /app + +COPY requirements.txt ./ +RUN pip install --no-cache-dir -r requirements.txt + +COPY app.py ./ + +CMD ["flask", "run", "--host=0.0.0.0", "--port=8000"] diff --git a/web/app.py b/web/app.py new file mode 100644 index 0000000..e0a2338 --- /dev/null +++ b/web/app.py @@ -0,0 +1,29 @@ +from flask import Flask, jsonify +import pymysql +import os + +app = Flask(__name__) + +def get_db_connection(): + return pymysql.connect( + host='db', + user=os.getenv('MYSQL_USER'), + password=os.getenv('MYSQL_PASSWORD'), + db=os.getenv('MYSQL_DATABASE') + ) + +@app.route('/') +def index(): + conn = get_db_connection() + with conn.cursor() as cursor: + cursor.execute("SELECT name, age FROM data") + result = cursor.fetchall() + return jsonify(result) + +@app.route('/health') +def health(): + return jsonify({"status": "OK"}) + +@app.errorhandler(404) +def not_found(e): + return jsonify({"error": "Not found"}), 404 diff --git a/web/requirements.txt b/web/requirements.txt new file mode 100644 index 0000000..c167153 --- /dev/null +++ b/web/requirements.txt @@ -0,0 +1,2 @@ +flask +pymysql