A production-grade unified messaging service that supports SMS, MMS, and Email messaging with conversation management, built with Go 1.24 and clean architecture principles.
- Unified Messaging API: Send SMS, MMS, and Email messages through a single API
- Conversation Management: Automatic grouping of messages into conversations
- Data Persistence: PostgreSQL database with proper indexing and constraints
- Webhook Support: Handle incoming messages from external providers
- Error Handling: Retry logic with exponential backoff for provider errors (500, 429)
- Production-Ready: Dockerized with multi-stage builds, health checks, and security
- API Documentation: Interactive Swagger/OpenAPI documentation served by the main application
- Clean Architecture: Separation of concerns with dependency injection
- Comprehensive Testing: Unit, integration, and API tests
- HTTP Error Handling: Robust retry logic for provider errors (500, 429, etc.)
- Go 1.24+
- Docker and Docker Compose
- Make (optional, for convenience)
-
Clone and setup:
git clone <repository-url> cd messaging-service make setup
-
Generate Swagger documentation:
make swagger
-
Start the application:
make run
-
Access the API:
- API: http://localhost:8080/api
- Swagger Docs: http://localhost:8080/swagger/index.html
- Health Check: http://localhost:8080/health
-
Start the full stack (database + application):
make docker-up
-
Access the application:
- API: http://localhost:8080/api
- Swagger Docs: http://localhost:8080/swagger/index.html
- Health Check: http://localhost:8080/health
-
Stop the stack:
make docker-down
The API documentation is automatically generated and served by the main application:
- Swagger UI: http://localhost:8080/swagger/index.html
- OpenAPI JSON: http://localhost:8080/swagger/doc.json
- OpenAPI YAML: http://localhost:8080/swagger/doc.yaml
| Method | Endpoint | Description |
|---|---|---|
POST |
/api/messages/message |
Send SMS/MMS message |
POST |
/api/messages/email |
Send email message |
POST |
/api/webhooks/message |
Handle incoming SMS/MMS |
POST |
/api/webhooks/email |
Handle incoming email |
GET |
/api/conversations |
List conversations by query - query params required |
GET |
/api/conversations/:id/messages |
Get messages in conversation |
GET |
/health |
Health check endpoint |
CREATE TABLE conversations (
id SERIAL PRIMARY KEY,
participant1 VARCHAR(255) NOT NULL,
participant2 VARCHAR(255) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);CREATE TABLE messages (
id SERIAL PRIMARY KEY,
conversation_id INTEGER REFERENCES conversations(id),
from_address VARCHAR(255) NOT NULL,
to_address VARCHAR(255) NOT NULL,
message_type VARCHAR(10) NOT NULL,
body TEXT NOT NULL,
attachments JSONB,
provider_message_id VARCHAR(255),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);make test# Unit tests only
go test ./internal/... -v
# Integration tests only
go test ./tests/... -v
# API tests
./bin/test.sh| Command | Description |
|---|---|
make docker-build |
Build Docker image |
make docker-up |
Start the full stack (database + app) |
make docker-down |
Stop the full stack |
make docker-logs |
View logs |
The application uses environment variables for configuration. See CONFIGURATION.md for all available options.
# Server Configuration
SERVER_PORT=8080
SERVER_READ_TIMEOUT=30s
SERVER_WRITE_TIMEOUT=30s
SERVER_IDLE_TIMEOUT=60s
# Database Configuration
DATABASE_HOST=localhost
DATABASE_PORT=5432
DATABASE_NAME=messaging_service
DATABASE_USER=messaging_user
DATABASE_PASSWORD=messaging_password
DATABASE_MAX_OPEN_CONNS=25
DATABASE_MAX_IDLE_CONNS=5
DATABASE_CONN_MAX_LIFETIME=5mβββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Client Applications β
β βββββββββββββββ βββββββββββββββ βββββββββββββββ β
β β Web App β β Mobile App β β External β β
β β β β β β Services β β
β βββββββββββββββ βββββββββββββββ βββββββββββββββ β
βββββββββββββββββββββββ¬ββββββββββββββββββββββββββββββββββββββββββββ
β HTTP/HTTPS
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Messaging Service API β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β Middleware Layer β β
β β βββββββββββββββ βββββββββββββββ βββββββββββββββ β β
β β β Request ID β β Logging β β Metrics β β β
β β β β β β β β β β
β β βββββββββββββββ βββββββββββββββ βββββββββββββββ β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β HTTP Handlers β β
β β βββββββββββββββ βββββββββββββββ βββββββββββββββ β β
β β β SMS/MMS β β Email β β Webhooks β β β
β β β Handler β β Handler β β Handler β β β
β β βββββββββββββββ βββββββββββββββ βββββββββββββββ β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
βββββββββββββββββββββββ¬ββββββββββββββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Business Logic Layer β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β Service Layer β β
β β βββββββββββββββ βββββββββββββββ βββββββββββββββ β β
β β β Messaging β βConversation β β Validation β β β
β β β Service β β Service β β Logic β β β
β β βββββββββββββββ βββββββββββββββ βββββββββββββββ β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
βββββββββββββββββββββββ¬ββββββββββββββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Data Access Layer β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β Repository Layer β β
β β βββββββββββββββ βββββββββββββββ βββββββββββββββ β β
β β β Message β βConversation β β Database β β β
β β β Repository β β Repository β β Connection β β β
β β βββββββββββββββ βββββββββββββββ βββββββββββββββ β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
βββββββββββββββββββββββ¬ββββββββββββββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β External Services β
β βββββββββββββββ βββββββββββββββ βββββββββββββββ β
β β SMS/MMS β β Email β β Database β β
β β Provider β β Provider β β (PostgreSQL)β β
β β β β β β β β
β βββββββββββββββ βββββββββββββββ βββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
1. Client Request
β
2. Middleware Processing
βββ Request ID Generation
βββ Structured Logging
βββ Metrics Collection
β
3. Route Matching
β
4. Handler Processing
βββ Request Validation
βββ Business Logic
βββ Response Formatting
β
5. Service Layer
βββ Business Rules
βββ Data Validation
βββ External Calls
β
6. Repository Layer
βββ Database Operations
βββ Data Persistence
β
7. External Providers
βββ SMS/MMS Delivery
βββ Email Delivery
βββ Webhook Processing
Client β POST /api/messages/message
β
Handler.SendSMS() β Validate Request
β
Service.SendSMS() β Business Logic
β
Provider.SendSMS() β External SMS Service
β
Repository.Create() β Save to Database
β
Response β Success/Error
External Service β POST /api/webhooks/message
β
Handler.HandleInboundSMS() β Parse Webhook
β
Service.HandleInboundSMS() β Process Message
β
Repository.Create() β Save to Database
β
Response β Acknowledgment
messaging-service/
βββ cmd/
β βββ server/
β βββ main.go # Application entry point
βββ internal/
β βββ app/ # Application lifecycle
β βββ config/ # Configuration management
β βββ container/ # Dependency injection
β βββ domain/ # Domain models and interfaces
β βββ handler/ # HTTP handlers
β βββ logger/ # Structured logging
β βββ middleware/ # HTTP middleware
β βββ provider/ # External service providers
β βββ repository/ # Data access layer
β βββ router/ # HTTP routing
β βββ service/ # Business logic
β βββ telemetry/ # OpenTelemetry setup
βββ tests/ # Integration tests
βββ docs/ # Generated Swagger docs
βββ init.sql/ # Database schema
βββ bin/ # Scripts
βββ Dockerfile # Multi-stage Docker build
βββ docker-compose.yml # Development environment
βββ Makefile # Build and deployment commands
βββ README.md # This file
| Command | Description |
|---|---|
make setup |
Initialize project dependencies |
make run |
Start the messaging service |
make test |
Run all tests |
make swagger |
Generate Swagger documentation |
make docs |
Generate Swagger documentation |
make help |
Show all available commands |
# Start production stack
make docker-up
# View logs
make docker-logs
# Stop stack
make docker-down# Build the application
go build -o messaging-service ./cmd/server
# Set environment variables
export DATABASE_HOST=your-db-host
export DATABASE_PORT=5432
# ... other environment variables
# Run the application
./messaging-servicecurl http://localhost:8080/healthThe application includes database connection monitoring and will log connection status on startup.
- Non-root user in Docker containers
- Input validation on all endpoints
- SQL injection protection through parameterized queries
- Boilerplate for Authorization through api-key Authorization header
- Request ID tracking with
X-Request-IDheader - Structured logging with request details and timing
- OpenTelemetry metrics with Prometheus exporter
- Request/response metrics including duration and size
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new functionality
- Ensure all tests pass
- Submit a pull request
This project is licensed under the Apache 2.0 License - see the LICENSE file for details.