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 0000000..12cdcc0 Binary files /dev/null and b/src/requirements.txt differ