A simple HTTP REST api:
- Sends emails using multiple providers (Sendgrid & Mailgun)
- Provides failover if one of the provider fails
- node 8
- npm 5
- Refer to swagger
- 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
- The api exposes some simple metrics at
GET /metrics
for number of emails sent or failed - e.g.
{
email: {
sent: 5,
fail: 1
}
}
Path | Method |
---|---|
POST | |
/health-check | GET |
/metrics | GET |
├── src - service source files
| ├── schemas - schema validators
| ├── email - email routes and external provider services
├── test - (using mocha/shouldjs/nock)
├── integration
├── unit
- 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
- App config is at
src/config.js
- Set Environment variables for mail provider api keys:
- MAIL_GUN_API_KEY
- SEND_GRID_API_KEY
curl -X POST \
http://localhost:3000/email \
-d '{
"to": ["[email protected]"],
"cc": ["[email protected]"],
"subject": "test-email",
"body": "test email content"
}'
- 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