first version
This commit is contained in:
parent
0c40a056bf
commit
3401cf6fd8
4
.env
Normal file
4
.env
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
MYSQL_ROOT_PASSWORD=password
|
||||||
|
MYSQL_DATABASE=testdb
|
||||||
|
MYSQL_USER=testuser
|
||||||
|
MYSQL_PASSWORD=password
|
4
data.csv
Normal file
4
data.csv
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
name,age
|
||||||
|
Alice,30
|
||||||
|
Bob,25
|
||||||
|
Charlie,35
|
|
40
docker-compose.yml
Normal file
40
docker-compose.yml
Normal file
|
@ -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
|
10
filler/Dockerfile
Normal file
10
filler/Dockerfile
Normal file
|
@ -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"]
|
32
filler/filler_db.py
Normal file
32
filler/filler_db.py
Normal file
|
@ -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)
|
11
web/Dockerfile
Normal file
11
web/Dockerfile
Normal file
|
@ -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"]
|
29
web/app.py
Normal file
29
web/app.py
Normal file
|
@ -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
|
2
web/requirements.txt
Normal file
2
web/requirements.txt
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
flask
|
||||||
|
pymysql
|
Loading…
Reference in a new issue