diff --git a/docker-compose.prod.yml b/docker-compose.prod.yml index 117521b..0474d9d 100644 --- a/docker-compose.prod.yml +++ b/docker-compose.prod.yml @@ -1,6 +1,6 @@ services: db: - image: postgres:15.2 + image: postgres:16.1 container_name: db env_file: - .env diff --git a/docker-compose.yml b/docker-compose.yml index bf71e1f..47de016 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,6 +1,6 @@ services: db: - image: postgres:15.2 + image: postgres:16.1 container_name: db env_file: - .env @@ -16,30 +16,44 @@ services: timeout: 20s retries: 5 + redis: + image: redis:7.2.3 + container_name: redis + healthcheck: + test: ["CMD", "redis-cli", "ping"] + interval: 10s + timeout: 5s + retries: 3 + flask: build: ./flask container_name: flask env_file: - .env + ports: + - "5000:5000" depends_on: + # waiting for the upstream containers to be ready + redis: + condition: service_healthy db: - # waiting for the database to be ready condition: service_healthy + volumes: - ./flask:/app - nginx: - image: nginx:latest - container_name: nginx - ports: - - "80:80" - - "443:443" - volumes: - - ./nginx/conf.d:/etc/nginx/conf.d - - ./nginx/nginx.conf:/etc/nginx/nginx.conf - - ./nginx/ssl-certs:/etc/nginx/ssl-certs - depends_on: - - flask + # nginx: + # image: nginx:latest + # container_name: nginx + # ports: + # - "80:80" + # - "443:443" + # volumes: + # - ./nginx/conf.d:/etc/nginx/conf.d + # - ./nginx/nginx.conf:/etc/nginx/nginx.conf + # - ./nginx/ssl-certs:/etc/nginx/ssl-certs + # depends_on: + # - flask # to persist container volumes volumes: diff --git a/flask/Dockerfile b/flask/Dockerfile index 3453168..fc50a5b 100644 --- a/flask/Dockerfile +++ b/flask/Dockerfile @@ -18,4 +18,7 @@ USER flaskuser # CMD python application.py # CMD python application.py run --debug --host 0.0.0.0 -CMD gunicorn --access-logfile '-' --workers 2 --threads 3 --bind flask:5000 application:application +# without Flask-SocketIO +# CMD gunicorn --access-logfile '-' --workers 1 --threads 4 --bind flask:5000 application:application +# with Flask-SocketIO +CMD gunicorn --access-logfile '-' --worker-class eventlet --workers 1 --threads 4 --bind flask:5000 application:application diff --git a/flask/app/__init__.py b/flask/app/__init__.py index 3510a32..c21658d 100644 --- a/flask/app/__init__.py +++ b/flask/app/__init__.py @@ -1,9 +1,11 @@ from flask import Flask from flask_sqlalchemy import SQLAlchemy from flask_cors import CORS +from flask_socketio import SocketIO import os db = SQLAlchemy() +socketio = SocketIO() def create_app(): @@ -20,8 +22,10 @@ def create_app(): port=os.environ['RDS_PORT'], database=os.environ['RDS_DB_NAME'], ) + app.config['SECRET_KEY'] = os.environ['SECRET_KEY'] db.init_app(app) + socketio.init_app(app) # importing and registering routes with their url prefix from .views.main import main_bp diff --git a/flask/app/views/__init__.py b/flask/app/views/__init__.py index e69de29..5ca8160 100644 --- a/flask/app/views/__init__.py +++ b/flask/app/views/__init__.py @@ -0,0 +1 @@ +from . import sockets \ No newline at end of file diff --git a/flask/app/views/sockets.py b/flask/app/views/sockets.py new file mode 100644 index 0000000..05119ad --- /dev/null +++ b/flask/app/views/sockets.py @@ -0,0 +1,15 @@ +from flask_socketio import emit, send +from app import socketio + +@socketio.on('message') +def handle_message(message): + print('Received message:', message) + emit('response', {'data': 'Server received your message'}) + +@socketio.on('json') +def handle_json(json): + print('received json: ' + str(json)) + +@socketio.on('my event') +def handle_my_custom_event(json): + print('received my_event: ' + str(json)) \ No newline at end of file diff --git a/flask/requirements.txt b/flask/requirements.txt index 9b6db3b..7b97f30 100644 --- a/flask/requirements.txt +++ b/flask/requirements.txt @@ -1,17 +1,26 @@ +bidict==0.22.1 click==8.1.7 +dnspython==2.4.2 +eventlet==0.34.2 Flask==2.2.3 Flask-Cors==3.0.10 +Flask-SocketIO==5.3.6 Flask-SQLAlchemy==3.0.3 greenlet==2.0.2 gunicorn==21.2.0 +h11==0.14.0 importlib-metadata==7.0.1 itsdangerous==2.1.2 Jinja2==3.1.2 MarkupSafe==2.1.3 packaging==23.2 psycopg2-binary==2.9.6 +python-engineio==4.8.2 +python-socketio==5.11.0 +simple-websocket==1.0.0 six==1.16.0 SQLAlchemy==2.0.9 typing_extensions==4.9.0 Werkzeug==2.2.3 +wsproto==1.2.0 zipp==3.15.0 \ No newline at end of file diff --git a/nginx/conf.d/server.conf b/nginx/conf.d/server.conf index 96f84d2..081fda6 100644 --- a/nginx/conf.d/server.conf +++ b/nginx/conf.d/server.conf @@ -20,4 +20,4 @@ server { proxy_set_header X-Forwarded-Host $host; proxy_set_header X-Forwarded-Prefix /; } -} \ No newline at end of file +}