Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/favourdaniel/messaging-system
https://github.com/favourdaniel/messaging-system
Last synced: 21 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/favourdaniel/messaging-system
- Owner: FavourDaniel
- Created: 2024-07-12T14:51:01.000Z (4 months ago)
- Default Branch: main
- Last Pushed: 2024-07-13T19:17:32.000Z (4 months ago)
- Last Synced: 2024-07-14T17:23:23.321Z (4 months ago)
- Language: Python
- Size: 36.1 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: READMe.md
Awesome Lists containing this project
README
# Messaging System with RabbitMQ/Celery and Python Application behind Nginx
This deploys a Python application behind Nginx that interacts with RabbitMQ/Celery for email sending and logging functionality. The below architecture diagram visualizes the process:
![app-architecture](https://github.com/user-attachments/assets/7c964cc8-27ed-47b0-9119-cde2c41d5ec8)## Set up RabbitMQ Locally
- Install RabbitMQ:
```
brew install rabbitmq ##for macOS users
```
- Start RabbitMQ:
```
# starts a local RabbitMQ node
brew services start rabbitmq
```
- Access RabbitMQ on http://localhost:15672/The default login credential for both the username and password is "guest".
## Set up Celery
Celery is an asynchronous task queue/job queue system for Python, designed to handle real-time operations. It can be used to schedule tasks that run in the background, allowing for concurrency and parallel execution of code.
Celery is a Python package so it will be installed with pip. This will be done in a virtual environment.
- Create and activate a Python virtual environment
```
python3 -m venv myenv
source myenv/bin/activate
```- Install Celery and other requirements from the requirements.txt file
```
pip install -r requirements.txt
```## Setup and Start the Python application
The provided Python application creates a simple web application that does two things:1. Sends Emails: It allows you to trigger the sending of an email by visiting a specific URL in your web browser. The email sending itself happens in the background through Celery. Celery is configured to use RabbitMQ as its message broker (where it keeps track of tasks). It creates an email message, connects to a Gmail SMTP server, logs in, and sends the email.
2. Logs Time: It also has a feature to log the current time to a file when you visit another specific URL.
### Email setup
You can use an existing email address or create a new one. Configure 2FA for whichever.
In the email settings, search "App Password" and create an App password. Be sure to copy the password somewhere.A `.env.template` file has been provided in this repo. Change the name to `.env` and replace the email and app password env variables.
## Set Up Log Directory
The time logs should be logged at `/var/log/messaging_system.log`. This path has already been set in the app to be created if it doesn't exist but you can configure it manually using the below commands:
```
sudo touch /var/log/messaging_system.log
sudo chmod a+rw /var/log/messaging_system.log
```
The code has already been written to do this, but you can run these commands if you want a manual approach.### Install Nginx
Install Nginx on your local and replace the default `nginx.conf` file with the one provided in this repo. This allows Nginx to route requests from localhost:5000 where the flask app will be running.### Start the Application
- In your terminal, run:
```
python app.py --port 5000
```- In another terminal, run:
```
celery -A app.celery worker --loglevel=info
```- Access the app at `localhost`
- In another tab, load `http://localhost:5000/?talktome=true` to generate time logs- In another tab, load `http://localhost:5000/[email protected]`
- Check RabbitMQ to be sure it was queued successfully
After some seconds, it should change to ready
If you check the terminal where Celery is running, you should see the below:
This indicates that the email-sending task was received, processed by a specific Celery worker (ForkPoolWorker-8), and completed successfully.
- Check your email address for the mail sent
## Setup Ngrok
- Install ngrok via Homebrew with the following command:
```
brew install ngrok/ngrok/ngrok
```
- Connect your account
Next, connect your ngrok agent to your ngrok account. If you haven't already, sign up for an ngrok account. Copy your ngrok authtoken from your ngrok dashboard.
Run the following command in your terminal to install the authtoken and connect the ngrok agent to your account.```
ngrok config add-authtoken
```
- Create a static domainYou can create a free one on your ngrok dashboard under the setup and installation tab.
Copy and paste command displayed
```
ngrok http --domain= 80
```- Copy and open the URL in your browser
- Click on the `Visit Site` button to access your website
- Access the time logs using the url
- Send an email
- You can also access your Ngrok dashboard at `localhost:4040`