A simple service for sending and tracking messages with retry capabilities and audit logging.
Message Sender is a microservice designed to handle asynchronous message delivery with reliability features:
- Message queuing and scheduled processing
- Automatic retries with configurable parameters
- Audit logging of all delivery attempts
- RESTful API for message management
- Configurable worker pool for concurrent processing
The application follows a clean architecture approach with the following components:
- Core: Contains domain models and service interfaces
- Infrastructure: Implements the interfaces defined in the core
- HTTP handlers for API endpoints
- Database repositories for PostgreSQL and Redis
- Processing components (scheduler and worker pool)
- Docker and Docker Compose
- Go 1.24+ (for local development)
The easiest way to run the application is using Docker Compose:
docker compose up --build
This will start:
- PostgreSQL database
- Redis cache
- External service (simulates message delivery endpoints)
- Message Sender API
The service exposes the following REST API endpoints:
Method | Path | Description |
---|---|---|
GET | /docs |
API Documentation |
GET | /api/messages |
List all messages with pagination and filtering |
POST | /api/messages |
Create a new message to be sent |
GET | /api/messages/{messageID}/audit |
Get audit log for a specific message |
POST | /api/scheduler/start |
Start the message processing scheduler |
POST | /api/scheduler/stop |
Stop the message processing scheduler |
curl -X POST http://localhost:8080/api/messages \
-H "Content-Type: application/json" \
-d '{
"content": "Hello, this is a test message",
"phone_number": "+905555555555"
}'
curl -X GET http://localhost:8080/api/messages/1/audit
Start the scheduler with custom parameters:
curl -X POST http://localhost:8080/api/scheduler/start \
-H "Content-Type: application/json" \
-d '{
"interval": "10s",
"batch_size": 5
}'
Stop the scheduler:
curl -X POST http://localhost:8080/api/scheduler/stop
The application can be configured using environment variables:
Variable | Description | Default |
---|---|---|
DATABASE_URL | PostgreSQL connection string | postgres://postgres:postgres@localhost:5432/message_sender?sslmode=disable |
REDIS_URL | Redis connection string | redis://localhost:6379 |
SERVER_PORT | HTTP server port | 8080 |
WORKER_POOL_SIZE | Number of concurrent workers | 5 |
SCHEDULER_INTERVAL | Interval between processing batches | 5s |
SCHEDULER_BATCH_SIZE | Number of messages to process in each batch | 2 |
SUCCESS_URL | External service success endpoint | http://external-service:8081/success |
FAIL_URL | External service failure endpoint | http://external-service:8081/fail |
- Clone the repository
- Install dependencies:
go mod download
- Start the required services (PostgreSQL and Redis):
docker-compose up -d postgres redis external-service
- Run the application:
go run cmd/message-sender/main.go
For development with hot reload, you can use Air:
air
This will automatically rebuild and restart the application when files change.
.
├── cmd
│ ├── external-server # Simulated external service
│ └── message-sender # Main application entry point
├── internal
│ ├── config # Application configuration
│ ├── core # Domain models and interfaces
│ │ ├── model # Domain entities
│ │ ├── port # Interface definitions
│ │ └── service # Business logic
│ └── infrastructure # Implementation details
│ ├── db # Database connections
│ ├── http # HTTP server and handlers
│ ├── processing # Message processing components
│ └── repository # Data access implementations
├── migrations # Database migrations
└── docker-compose.yml # Docker Compose configuration