diff --git a/README.MD b/README.MD index 7ff57b8..a3a2871 100644 --- a/README.MD +++ b/README.MD @@ -1,155 +1,170 @@ # Notification Service -A microservice for handling and delivering notifications through various channels like SMS and email using RabbitMQ as a message broker. - -## 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. +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 -- Message consumption from RabbitMQ -- Support for multiple notification channels (SMS, email) -- Extensible architecture for adding new notification types -- Asynchronous notification handling +- **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 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 -- **Infrastructure Layer**: Implements the ports with concrete adapters -- **RabbitMQ**: Used as a message broker for consuming notification requests +- **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 -## Installation +## Prerequisites -### Prerequisites - -- Go 1.x+ -- RabbitMQ server - -### Setup - -1. Clone the repository: -```bash -git clone https://github.com/JscorpTech/notification.git -cd notification -``` - -2. Install dependencies: -```bash -go mod download -``` - -3. Build the application: -```bash -go build -o notification-service ./cmd/main.go -``` +- Go 1.24 or higher +- Redis (for Redis broker) +- RabbitMQ (for RabbitMQ broker) +- Docker (optional, for containerized deployment) ## Configuration -Configure your RabbitMQ connection and other settings in the appropriate configuration files. +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 + +### Local Development + +1. Clone the repository + ```bash + git clone https://github.com/JscorpTech/notification.git + cd notification + ``` + +2. Install dependencies + ```bash + go mod download + ``` + +3. Build and run the application + ```bash + go build -o notification ./cmd/main.go + ./notification + ``` + +### Docker Deployment + +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 -### Running the service +### Message Format -```bash -./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: +The service expects messages in the following JSON format: ```json { "type": "email", - "message": "Hello, this is a test notification.", + "message": "Subject: Welcome\r\n\r\nHello, welcome to our service.", "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 from kombu import Connection, Exchange, Producer -# RabbitMQ ulanishi rabbit_url = 'amqp://guest:guest@127.0.0.1:5672/' connection = Connection(rabbit_url) channel = connection.channel() exchange = Exchange('notification', type='direct') - -# Producer yaratish 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 = { - 'type': 'email', - 'message': "Subject: test\r\n\r\nclasscom.uz sayti va mobil ilovasiga ro'yxatdan o'tishingiz uchun tasdiqlash kodi: 1234", - 'to': ["JscorpTech@gmail.com", "admin@jscorp.uz"] + 'type': 'sms', + 'message': "Your verification code is 1234", + 'to': ["+998901234567"] } - -# Xabarni JSON formatga o‘tkazib, Redis listga push qilish -r.rpush('notification', json.dumps(message)) - -print("Message pushed to Redis list!") - +producer.publish(message) ``` -Available notification types: -- `email`: For email notifications -- `sms`: For SMS notifications +## Adding New Notification Channels -## 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` -``` -notification/ -├── cmd/ -│ └── 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 -``` +## License + +MIT License - See [LICENSE](LICENSE) file for details. ## Contributing 1. Fork the repository -2. Create your feature branch: `git checkout -b feature/my-new-feature` -3. Commit your changes: `git commit -am 'Add some feature'` -4. Push to the branch: `git push origin feature/my-new-feature` -5. Submit a pull request - -## License - -[Add your license here] - -## Contact - -JscorpTech - [GitHub](https://github.com/JscorpTech) +2. Create a feature branch (`git checkout -b feature/my-feature`) +3. Commit your changes (`git commit -am 'Add some feature'`) +4. Push to the branch (`git push origin feature/my-feature`) +5. Create a new Pull Request \ No newline at end of file