Written in Python 3.7
# Install pipenv
pip install pipenv
# Install the dependencies
pipenv install
https://docs.docker.com/install/
You will need to register a Docker account, to download Docker.
./scripts/dev.sh
On the first run, it will be a bit slow, as the script creates a Docker
container for a testing PostgreSQL
database.
./scripts/test_dev.sh
Edit the pipenv run pytest
line inside ./scripts/test_dev.sh
to run the test you want. An example is commented out in the file.
Requires the
dev
instance from part 1 to be running
./scripts/setup_dev_db.sh
In case you would like to get requirements.txt
, run:
./scripts/lock_pipenv.sh
And the 2 requirements.txt
for test
and main
will be created in folder requirements
.
Before committing your changes, it is recommended to run:
black .
which will format the code styles all Python files
An access token is a short-life token used for sending authorized requests.
A refresh token is a long-life token used to generate new access tokens. In this application, refresh tokens don't expire.
- The client sends a request with fields
email
andpassword
in the request's body. - The server returns a response, with
user
andaccess_token
in the body and a cookie. - The client stores both
access_token
to its browser. - The client uses the cookie to authorize the user's requests.
- When the cookie expires, the client sends the cookie to endpoint
/refresh
to get a new cookie. - Replace the expired
access_token
with the freshly retrieved one in the browser. - Repeat from step 4.
Both app.py
and app_socketio.py
need to be run to fully achieve all functionalities.
The commands to run them are located in variable ExecStart
in deployment/ora.service and deployment/ora_socketio.service
.nginx.conf
is the NginX
configuration used in the DigitalOcean
server.
*.service
files are the systemctl
services to auto-run the application.
- Database: PostgreSQL
- Database ORM: Gino
- Database Schema Migration: Alembic
- Chat: python-socketio is used for chat events (such as sending/receiving chat messages, updating online statuses, etc.)
- Monitoring: Prometheus for storing metrics of requests, which could be used by other visualization tools to see requests count, latency, error rates, etc.
- Error logging: Sentry
- Sending emails (for new incoming chats, visitors replying a message, etc.): SendGrid
- Metadata of chat rooms and online users are stored in Redis
- Redis is also used as a message queue for email sending tasks.
- Celery: manage workers and allow workers to take tasks from
Redis
and execute them. - Flower: A monitoring tool for
Celery
, to track and manage running tasks and failures.
1. Chat - ora_backend/views/chat_socketio.py
- For each connection, the user's information is stored in the SocketIO's session. It should be changed so that the user's information is stored in a centralized
Redis
instance, as the session is only available separatedly to each machine.- Be noted that, for
app_socketio.py
, as eachgunicorn
worker actually doesn't share memory, they won't be able to access others' sessions. Link to discussion
- Be noted that, for
- Some chat events may not work properly (such as events for changing chat priority, transfering ownership of the chat room, etc.) as this project was done without being properly tested.
2. Auto-assign volunteers on new chats - ora_backend/utils/assign.py
- Sometimes, the auto-assign functionality doesn't seem to email/assign the correct volunteers
-
app.py
is the REST API server used mostly for authentication, bookmarking visitors, fetching chat history, etc. -
app_socketio.py
is the SocketIO server to send/receive SocketIO events with front-end for chat functionalities.
It is similar to package.json
, which includes the dependencies of the project.
The config files (including for NginX
and systemctl
) used in the DigitalOcean
server.
The requirements.txt
files generated by Pipenv
from running ./scripts/lock_pipenv.sh
It is the main source code of the project.
- ora_backend/__init__.py: Setting up the application
- ora_backend/models.py: The Database models of the application.
- ora_backend/schemas.py: The schemas used for input serialization and de-serialization on requests.
The configurations of the application and connection to Database.
The background running tasks of the application.
The HTML templates used to send emails.
The logic/execution of all the REST endpoints and SocketIO events.
Declaration of the tasks that will be ran by Celery
.