4.3 KiB
4.3 KiB
Notification Service
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.
Features
- Multiple Brokers: Support for both Redis and RabbitMQ as message brokers
- Multiple Notification Channels: SMS and Email notification support
- Containerized: Ready to deploy with Docker
- Extensible Architecture: Easy to add new notification channels or message brokers
Architecture
The service follows a clean architecture pattern with the following components:
- Broker: Handles message subscription from different sources (Redis/RabbitMQ)
- Notifier: Implements different notification channels (SMS/Email)
- 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:
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
Local Development
-
Clone the repository
git clone https://github.com/JscorpTech/notification.git cd notification -
Install dependencies
go mod download -
Build and run the application
go build -o notification ./cmd/main.go ./notification
Docker Deployment
Build and run using Docker:
docker build -t notification-service .
docker run -p 8080:8080 --env-file .env notification-service
Or using Docker Compose:
docker-compose up -d
Usage
Message Format
The service expects messages in the following JSON format:
{
"type": "email",
"message": "Subject: Welcome\r\n\r\nHello, welcome to our service.",
"to": ["user@example.com"]
}
For SMS notifications:
{
"type": "sms",
"message": "Your verification code is 1234",
"to": ["+998901234567"]
}
Sending Messages
Using Redis
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
from kombu import Connection, Exchange, Producer
rabbit_url = 'amqp://guest:guest@127.0.0.1:5672/'
connection = Connection(rabbit_url)
channel = connection.channel()
exchange = Exchange('notification', type='direct')
producer = Producer(channel, exchange=exchange, routing_key="notification")
message = {
'type': 'sms',
'message': "Your verification code is 1234",
'to': ["+998901234567"]
}
producer.publish(message)
Adding New Notification Channels
- Create a new notifier implementation in
internal/notifier/ - Implement the
domain.NotifierPortinterface - Add the new notifier type to the
Handlerfunction ininternal/consumer/notification.go
License
MIT License - See LICENSE file for details.
Contributing
- Fork the repository
- Create a feature branch (
git checkout -b feature/my-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin feature/my-feature) - Create a new Pull Request