demo for compose

This commit is contained in:
2025-05-05 13:44:59 +03:00
parent c3e28c48d0
commit e1e2593fa5
5 changed files with 86 additions and 0 deletions

8
src/Dockerfile Normal file
View File

@@ -0,0 +1,8 @@
FROM python:3.12
WORKDIR /app
COPY main.py .
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
CMD ["python3", "main.py"]

48
src/main.py Normal file
View File

@@ -0,0 +1,48 @@
import os
import psycopg2
from flask import Flask
# Подключение к базе данных postgres
conn = psycopg2.connect(
dbname=os.getenv("POSTGRES_DB"),
user=os.getenv("POSTGRES_USER"),
password=os.getenv("POSTGRES_PASSWORD"),
host="db",
port="5432"
)
app = Flask(__name__)
@app.route("/")
def hello_world():
return """<p>Hello, World!</p><br><a href="/db">ТЫК</a>"""
@app.route("/db")
def db():
cur = conn.cursor()
cur.execute('''UPDATE mytable SET visitors_count = visitors_count + 1''')
conn.commit()
cur.execute("SELECT visitors_count from mytable")
result = cur.fetchone()
value = -1
if result:
value = result[0]
return f"""DB:{value}<br><a href="/">Обратно</a>"""
if __name__ == "__main__":
cur = conn.cursor()
cur.execute('''CREATE TABLE IF NOT EXISTS mytable (
id SERIAL PRIMARY KEY,
visitors_count INTEGER NOT NULL DEFAULT 0)''')
cur.execute('''INSERT INTO mytable (id, visitors_count)
VALUES (1, 0)
ON CONFLICT (id) DO NOTHING''')
conn.commit()
app.run(host="0.0.0.0", port=8086)

BIN
src/requirements.txt Normal file

Binary file not shown.