From e1e2593fa59c72f14fcbba44f018b4e08e7e8551 Mon Sep 17 00:00:00 2001 From: Kirill Date: Mon, 5 May 2025 13:44:59 +0300 Subject: [PATCH] demo for compose --- .gitignore | 2 ++ docker-compose.yaml | 28 +++++++++++++++++++++++++ src/Dockerfile | 8 ++++++++ src/main.py | 48 +++++++++++++++++++++++++++++++++++++++++++ src/requirements.txt | Bin 0 -> 304 bytes 5 files changed, 86 insertions(+) create mode 100644 docker-compose.yaml create mode 100644 src/Dockerfile create mode 100644 src/main.py create mode 100644 src/requirements.txt diff --git a/.gitignore b/.gitignore index 0dbf2f2..28f4438 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ +.idea +postgres_data # ---> Python # Byte-compiled / optimized / DLL files __pycache__/ diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 0000000..13da89c --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,28 @@ +services: + db: + image: postgres:15-alpine + env_file: + - ./.env + ports: + - "5432:5432" + volumes: + - ./postgres_data:/var/lib/postgresql/data + container_name: postgres_db + restart: always + healthcheck: + test: [ "CMD-SHELL", "pg_isready", "-d", "db_prod" ] + interval: 30s + timeout: 60s + retries: 5 + start_period: 80s + web: + ports: + - "80:8086" + build: + context: ./src + env_file: + - ./.env + depends_on: + db: + condition: service_healthy + restart: always \ No newline at end of file diff --git a/src/Dockerfile b/src/Dockerfile new file mode 100644 index 0000000..a5804e9 --- /dev/null +++ b/src/Dockerfile @@ -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"] \ No newline at end of file diff --git a/src/main.py b/src/main.py new file mode 100644 index 0000000..6235426 --- /dev/null +++ b/src/main.py @@ -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 """

Hello, World!


ТЫК""" + + +@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}
Обратно""" + + +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) diff --git a/src/requirements.txt b/src/requirements.txt new file mode 100644 index 0000000000000000000000000000000000000000..12cdcc0075c85e257de6f2345364be800f0c3321 GIT binary patch literal 304 zcmXw!u?~Vj5Jaao@mCBS#$aJ}sj-XfQ$E!1YN63cDzS)`G-}i(&TJ-2} z!H5mN91E;j8RnRwHeQp9mF$a`iFs1bxe5=K6dCD~wKBz)yuve07il^=w|J49$$K=G z?h)9Li=$E%l{?$;Mx`@4`NWJp`y&$q1|0al(n}svX&Yyo40L|;8c$V|e3YO|^OSS6 QEjoPo4^&Mqy0|cxKc}}Vz5oCK literal 0 HcmV?d00001