diff --git a/.gitignore b/.gitignore index 789ca21..0729006 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ *pip-selfcheck.json *Python *__pycache__/ +chat-env diff --git a/Dockerfile b/Dockerfile new file mode 100755 index 0000000..d17c938 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,8 @@ +FROM python:3.9 +WORKDIR /app +COPY app.py /app/ + +COPY requirements.txt /app/ +RUN pip install -r requirements.txt + +CMD cd /app/ && gunicorn --worker-class=gevent -b 0.0.0.0:8000 -t 99999 app:app diff --git a/README.md b/README.md index c82f890..c3f2cd3 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,20 @@ +- [Chat](#chat) + - [Installation](#installation) + - [Running the app](#running-the-app) + - [Container based](#container-based) + - [Dev way](#dev-way) + +# Chat A primitive chat app created to experiment with Flask, Redis, Gevent & Server-Sent Events. -## installation +## Installation pip install flask redis gevent gunicorn -## running the app +## Running the app +### Container based + docker-compose up +### Dev way start the redis server redis-server - gunicorn --debug --worker-class=gevent -t 99999 app:app + REDIS_SERVER_NAME=localhost gunicorn --debug --worker-class=gevent -t 99999 app:app + diff --git a/app.py b/app.py index de0c2fa..736bc0f 100644 --- a/app.py +++ b/app.py @@ -3,11 +3,13 @@ import flask import redis +import os app = flask.Flask(__name__) -app.secret_key = 'asdf' -red = redis.StrictRedis() - +app.secret_key = 'docker-powered-chat' +red = redis.StrictRedis(host=os.environ["REDIS_SERVER_NAME"]) +print("Connected servers so far", red.get("CONNECTED_SERVERS")) +red.incr("CONNECTED_SERVERS") def event_stream(): pubsub = red.pubsub() diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100755 index 0000000..d14e0bc --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,17 @@ +version: '2' + +services: + chat_web: + image: redis_chat:0.0.1 + build: + context: . + ports: + - 8000:8000 + depends_on: + - chat_redis_server + environment: + - REDIS_SERVER_NAME=chat_redis_server + chat_redis_server: + image: redis:5 + ports: + - 6379:6379 diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..6e66a2c --- /dev/null +++ b/requirements.txt @@ -0,0 +1,12 @@ +click==7.1.2 +Flask==1.1.2 +gevent==20.12.1 +greenlet==1.0.0 +gunicorn==20.0.4 +itsdangerous==1.1.0 +Jinja2==2.11.2 +MarkupSafe==1.1.1 +redis==3.5.3 +Werkzeug==1.0.1 +zope.event==4.5.0 +zope.interface==5.2.0