feat: Yangilangan README, ko'p kanalli va brokerli xabar yetkazib beruvchi xizmat uchun.

This commit is contained in:
A'zamov Samandar
2025-04-25 11:38:53 +05:00
parent 130f03f973
commit bd8fba1d88

209
README.MD
View File

@@ -1,155 +1,170 @@
# Notification Service # Notification Service
A microservice for handling and delivering notifications through various channels like SMS and email using RabbitMQ as a message broker. A flexible notification service that supports multiple message brokers (Redis, RabbitMQ) and notification channels (SMS, Email). This service is designed to handle asynchronous notification delivery in your applications.
## Overview
This notification service is designed as a standalone microservice that consumes notification requests from a RabbitMQ queue and routes them to the appropriate notification provider based on the notification type. Currently, it supports SMS and email notifications.
## Features ## Features
- Message consumption from RabbitMQ - **Multiple Brokers**: Support for both Redis and RabbitMQ as message brokers
- Support for multiple notification channels (SMS, email) - **Multiple Notification Channels**: SMS and Email notification support
- Extensible architecture for adding new notification types - **Containerized**: Ready to deploy with Docker
- Asynchronous notification handling - **Extensible Architecture**: Easy to add new notification channels or message brokers
## Architecture ## Architecture
The notification service follows a clean architecture approach: The service follows a clean architecture pattern with the following components:
- **Domain Layer**: Contains core business logic and port interfaces - **Broker**: Handles message subscription from different sources (Redis/RabbitMQ)
- **Infrastructure Layer**: Implements the ports with concrete adapters - **Notifier**: Implements different notification channels (SMS/Email)
- **RabbitMQ**: Used as a message broker for consuming notification requests - **Services**: Contains the business logic for each notification channel
- **Domain**: Defines interfaces and data models
## Prerequisites
- Go 1.24 or higher
- Redis (for Redis broker)
- RabbitMQ (for RabbitMQ broker)
- Docker (optional, for containerized deployment)
## Configuration
Copy the provided `.env.example` to `.env` and update with your configuration:
```bash
cp .env.example .env
```
### Environment Variables
| Variable | Description | Example |
|----------|-------------|---------|
| BROKER | Message broker to use (redis or rabbitmq) | redis |
| TOPIC | Topic/queue name for notifications | notification |
| REDIS_ADDRESS | Redis server address | 127.0.0.1:6379 |
| REDIS_PASSWORD | Redis password (if any) | |
| REDIS_DB | Redis database number | 0 |
| RABBITMQ_URL | RabbitMQ connection URL | amqp://guest:guest@localhost:5672/ |
| ESKIZ_DOMAIN | Eskiz SMS API domain | https://notify.eskiz.uz/api |
| ESKIZ_USER | Eskiz SMS API username | admin@example.com |
| ESKIZ_PASSWORD | Eskiz SMS API password | password |
| ESKIZ_FROM | Eskiz SMS sender ID | 4546 |
| MAIL_DOMAIN | SMTP server domain | smtp.gmail.com |
| MAIL_USER | SMTP username | notification@example.com |
| MAIL_PASSWORD | SMTP password | yourpassword |
| MAIL_PORT | SMTP port | 587 |
## Installation ## Installation
### Prerequisites ### Local Development
- Go 1.x+ 1. Clone the repository
- RabbitMQ server
### Setup
1. Clone the repository:
```bash ```bash
git clone https://github.com/JscorpTech/notification.git git clone https://github.com/JscorpTech/notification.git
cd notification cd notification
``` ```
2. Install dependencies: 2. Install dependencies
```bash ```bash
go mod download go mod download
``` ```
3. Build the application: 3. Build and run the application
```bash ```bash
go build -o notification-service ./cmd/main.go go build -o notification ./cmd/main.go
./notification
``` ```
## Configuration ### Docker Deployment
Configure your RabbitMQ connection and other settings in the appropriate configuration files. Build and run using Docker:
```bash
docker build -t notification-service .
docker run -p 8080:8080 --env-file .env notification-service
```
Or using Docker Compose:
```bash
docker-compose up -d
```
## Usage ## Usage
### Running the service ### Message Format
```bash The service expects messages in the following JSON format:
./notification-service
```
This will start the notification consumer that listens for incoming notification requests.
### Sending a notification
Notifications should be published to the RabbitMQ exchange with the following JSON format:
```json ```json
{ {
"type": "email", "type": "email",
"message": "Hello, this is a test notification.", "message": "Subject: Welcome\r\n\r\nHello, welcome to our service.",
"to": ["user@example.com"] "to": ["user@example.com"]
} }
``` ```
Python example rabbitmq broker For SMS notifications:
```json
{
"type": "sms",
"message": "Your verification code is 1234",
"to": ["+998901234567"]
}
```
### Sending Messages
#### Using Redis
```python
import redis
import json
r = redis.StrictRedis(host='127.0.0.1', port=6379, db=0)
message = {
'type': 'email',
'message': "Subject: Welcome\r\n\r\nWelcome to our service!",
'to': ["user@example.com"]
}
r.rpush('notification', json.dumps(message))
```
#### Using RabbitMQ
```python ```python
from kombu import Connection, Exchange, Producer from kombu import Connection, Exchange, Producer
# RabbitMQ ulanishi
rabbit_url = 'amqp://guest:guest@127.0.0.1:5672/' rabbit_url = 'amqp://guest:guest@127.0.0.1:5672/'
connection = Connection(rabbit_url) connection = Connection(rabbit_url)
channel = connection.channel() channel = connection.channel()
exchange = Exchange('notification', type='direct') exchange = Exchange('notification', type='direct')
# Producer yaratish
producer = Producer(channel, exchange=exchange, routing_key="notification") producer = Producer(channel, exchange=exchange, routing_key="notification")
# Xabar yuborish
message = {'type': 'sms', 'message': "classcom.uz sayti va mobil ilovasiga ro'yxatdan o'tishingingiz uchun tasdiqlash kodi: 1234", "to": ["+998888112309", "+998943990509"]}
producer.publish(message)
print("Message sent to all workers!")
```
Python example redis broker
```python
import redis
import json
# Redis ulanishi
r = redis.StrictRedis(host='127.0.0.1', port=6379, db=0)
# Xabar tayyorlash
message = { message = {
'type': 'email', 'type': 'sms',
'message': "Subject: test\r\n\r\nclasscom.uz sayti va mobil ilovasiga ro'yxatdan o'tishingiz uchun tasdiqlash kodi: 1234", 'message': "Your verification code is 1234",
'to': ["JscorpTech@gmail.com", "admin@jscorp.uz"] 'to': ["+998901234567"]
} }
producer.publish(message)
# Xabarni JSON formatga otkazib, Redis listga push qilish
r.rpush('notification', json.dumps(message))
print("Message pushed to Redis list!")
``` ```
Available notification types: ## Adding New Notification Channels
- `email`: For email notifications
- `sms`: For SMS notifications
## Project Structure 1. Create a new notifier implementation in `internal/notifier/`
2. Implement the `domain.NotifierPort` interface
3. Add the new notifier type to the `Handler` function in `internal/consumer/notification.go`
``` ## License
notification/
├── cmd/ MIT License - See [LICENSE](LICENSE) file for details.
│ └── main.go # Entry point
├── internal/
│ ├── domain/
│ │ └── ports.go # Interfaces
│ ├── notifier/
│ │ ├── email.go # Email notification implementation
│ │ └── sms.go # SMS notification implementation
│ ├── rabbitmq/
│ │ └── connection.go # RabbitMQ connection handling
│ └── consumer/
│ └── consumer.go # Implementation of the notification consumer
└── README.md
```
## Contributing ## Contributing
1. Fork the repository 1. Fork the repository
2. Create your feature branch: `git checkout -b feature/my-new-feature` 2. Create a feature branch (`git checkout -b feature/my-feature`)
3. Commit your changes: `git commit -am 'Add some feature'` 3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch: `git push origin feature/my-new-feature` 4. Push to the branch (`git push origin feature/my-feature`)
5. Submit a pull request 5. Create a new Pull Request
## License
[Add your license here]
## Contact
JscorpTech - [GitHub](https://github.com/JscorpTech)