Email Service
Intro
A simple HTTP REST api:
- Sends emails using multiple providers (Sendgrid & Mailgun)
- Provides failover if one of the provider fails
Environment
- node 8
- npm 5
Schema
- Refer to swagger
Failover Approach
- The config has a default provider preference order set e.g. ['mail-gun', 'send-mail']. so in this case
mail-gun
is the preferred provider - The Email sender loops on the available providers in order of preference and tries to send email sender
- If there is a failure while sending the email it updates the current provider to be of lower priority provider-preference
- It then tries to send the email with next provider
- The approach can be improved further to switch back to the the original preferred provider after some time to retry
Metrics
- The api exposes some simple metrics at
GET /metrics
for number of emails sent or failed - e.g.
{
email: {
sent: 5,
fail: 1
}
}
Routes
Path | Method |
---|---|
POST | |
/health-check | GET |
/metrics | GET |
Project Structure
├── src - service source files
| ├── schemas - schema validators
| ├── email - email routes and external provider services
├── test - (using mocha/shouldjs/nock)
├── integration
├── unit
Quick Setup
- Clone the repo
npm i
to install node packages- Run
MAIL_GUN_API_KEY=<>
SEND_GRID_API_KEY=<>
npm start
to start the app - View logs at
tail -f email-service.log
- Run
npm test
to run specs - Run
npm run lint
to run es6 linter
Config
- App config is at
src/config.js
- Set Environment variables for mail provider api keys:
- MAIL_GUN_API_KEY
- SEND_GRID_API_KEY
Sample Request
curl -X POST \
http://localhost:3000/email \
-d '{
"to": ["john.doe@gmail.com"],
"cc": ["john.doe@yahoo.com"],
"subject": "test-email",
"body": "test email content"
}'
Pending
- can store email sent details in database to track and show
- log metrics - num of emails sent by provider, num of failures by provider
- handling more request validation scenarios for different providers
- more integration tests can be added to cover scenarios