A simple email service
- docker and docker compose (lastest version) Docker & Docker-Compose
- Mailgun and Sendgrid accounts
- git clone https://github.com/asjadsaboor/sm-email-service.git and go to project folder.
- create .env file in config directory
npm install
(install packages locally, since we don't mount node_modules in container)npm run build:docker
npm run start:docker
A collection of useful commands which might be helpful
npm run logs
npm run lint
npm run lint:fix
npm run format:check
npm run format:fix:all
npm run test
npm run apidocs
- uncomment following line from docker-compose.yml
#- --inspect-brk=0.0.0.0
- create launch.json in .vscode folder, and copy following code
{
"version": "0.2.0",
"configurations": [{
"type": "node",
"request": "attach",
"name": "Docker: Attach to Node",
"port": 9229,
"address": "localhost",
"localRoot": "${workspaceFolder}/src",
"remoteRoot": "/api/src",
"protocol": "inspector"
}]
}
NODE_ENV=local
PORT=4001
API_KEY_SENDGRID=XXX
API_KEY_MAILGUN=XXX
MAILGUN_DOMAIN=xxx
http://sm-email-service-2058030217.us-east-1.elb.amazonaws.com/api/docs/
http://sm-email-service-2058030217.us-east-1.elb.amazonaws.com/api/v1/
POST email/send
Body
{
"to":["asjadsaboor@gmail.com"],
"cc":["asjadsaboor1@yopmail.com"],
"bcc":["asjadsaboor2@yopmail.com"],
"subject": "my subject",
"body":"my email body"
}
CURL
curl -X POST \
http://sm-email-service-2058030217.us-east-1.elb.amazonaws.com/api/v1/email/send \
-H 'Content-Type: application/json' \
-H 'cache-control: no-cache' \
-d '{
"to":["asjadsaboor@gmail.com"],
"cc":["asjadsaboor1@yopmail.com"],
"bcc":["asjadsaboor2@yopmail.com"],
"subject": "from em service1 aws",
"body":"from em service1 aws"
}'
API is deployed on AWS Elastic Container service and attached to elastic load balancer.
Codepipeline is used for automated deployment.
- Implement a email service using 2 service providers, such that if one goes down that service can quickly switch to the other
When a user call REST API to send emails, first the request is send using the primary server.If the request of primary server fails, a key is set in in-memmory cache to deactivate primary server for few minutes and secondary email service will be used till the primaryserver key expires.
What will happen if both providers are down? A rare possibility, but in this case all request can be send to a SQS and a lambda can be attached to SQS to retry these requests.
Note: SQS is not yet implemented.
- Implement logger and push logs to cloudwatch or similar service.
- Make tslint a little more strict and run lint/test in CI/CD.
- Implement API rate limiting
- Add integration tests
- Implement Queue based solution for eg( SQS and lambda ), so that we can push requests to queue in case all email providers are down.
- attach SSL to aws elastic load balancer and redirect http to https.
- Enable auto scaling in ECS to handle heavy traffic.
- Whitelabel / authenticate sender domain.