Skip to content

Latest commit

 

History

History
153 lines (113 loc) · 4.48 KB

README.md

File metadata and controls

153 lines (113 loc) · 4.48 KB

Message Sender

A simple service for sending and tracking messages with retry capabilities and audit logging.

Overview

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

Architecture

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)

Prerequisites

  • Docker and Docker Compose
  • Go 1.24+ (for local development)

Getting Started

Running with Docker Compose

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

API Endpoints

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

Creating a Message

curl -X POST http://localhost:8080/api/messages \
  -H "Content-Type: application/json" \
  -d '{
    "content": "Hello, this is a test message",
    "phone_number": "+905555555555"
  }'

Viewing Message Audit Log

curl -X GET http://localhost:8080/api/messages/1/audit

Controlling the Scheduler

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

Configuration

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

Development

Local Setup

  1. Clone the repository
  2. Install dependencies: go mod download
  3. Start the required services (PostgreSQL and Redis): docker-compose up -d postgres redis external-service
  4. Run the application: go run cmd/message-sender/main.go

Hot Reload (Development)

For development with hot reload, you can use Air:

air

This will automatically rebuild and restart the application when files change.

Project Structure

.
├── 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

License

MIT License