Skip to content

Commit

Permalink
feat: Microservice and boilerplate for mailer
Browse files Browse the repository at this point in the history
  • Loading branch information
Abhinavmohanan authored and alllenshibu committed Feb 15, 2024
1 parent 36ff6f0 commit 2e5a9e9
Show file tree
Hide file tree
Showing 9 changed files with 2,014 additions and 1,707 deletions.
2 changes: 2 additions & 0 deletions apps/core-mailer/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
build
4 changes: 4 additions & 0 deletions apps/core-mailer/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
root: true,
extends: ['custom'],
};
21 changes: 21 additions & 0 deletions apps/core-mailer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
FROM node:16-alpine AS builder
RUN apk update && apk add --no-cache libc6-compat
RUN corepack enable && corepack prepare [email protected] --activate

WORKDIR /app

RUN npm install turbo

COPY ../../. .

RUN pnpm install

RUN pnpm run build --filter=core-mailer...

FROM node:18-alpine

COPY --from=builder /app/apps/core-mailer/dist .

ENV PORT=80

CMD ["app.js"]
47 changes: 47 additions & 0 deletions apps/core-mailer/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"name": "core-mailer",
"version": "1.0.0",
"description": "",
"main": "build/index.js",
"scripts": {
"build": "npm run clean & webpack --config webpack.config.js",
"clean": "rimraf dist & rimraf build",
"dev": "concurrently \"npx tsc --watch\" \"nodemon -q build/index.js\"",
"start": "node dist/app.js",
"lint": "eslint --ext .ts src"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.20.2",
"cors": "^2.8.5",
"dotenv": "^16.0.3",
"express": "^4.18.2",
"nodemailer": "^6.9.9"
},
"devDependencies": {
"@types/nodemailer": "^6.4.14",
"@types/chai": "^4.3.5",
"@types/express": "^4.17.17",
"@types/node": "^20.10.5",
"@types/pg": "^8.10.1",
"@typescript-eslint/eslint-plugin": "^6.7.3",
"concurrently": "^8.0.1",
"eslint": "^8.56.0",
"eslint-config-custom": "*",
"eslint-config-standard-with-typescript": "^43.0.0",
"eslint-plugin-import": "^2.27.5",
"eslint-plugin-n": "^16.5.0",
"eslint-plugin-prettier": "^5.0.0",
"eslint-plugin-promise": "^6.1.1",
"nodemon": "^3.0.1",
"prettier": "3.1.1",
"rimraf": "^5.0.1",
"ts-loader": "^9.5.1",
"tsconfig": "workspace:*",
"typescript": "^5.0.4",
"webpack": "^5.89.0",
"webpack-cli": "^5.1.4"
}
}
28 changes: 28 additions & 0 deletions apps/core-mailer/src/controller/mail.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { createTransport } from 'nodemailer';

const transporter = createTransport({
service: 'Gmail',
host: 'smtp.gmail.com',
port: 465,
secure: true,
auth: {
user: process.env.EMAIL_ADDRESS,
pass: process.env.EMAIL_PASSWORD,
},
});

export const sendMail = async (email: string, subject: string, text: string) => {
try {
const mailOptions = {
from: process.env.EMAIL_ADDRESS,
to: email,
subject: subject,
text: text,
};

const info = await transporter.sendMail(mailOptions);
console.log('Message sent: %s', info.messageId);
} catch (error) {
console.error(error);
}
};
47 changes: 47 additions & 0 deletions apps/core-mailer/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import express, { Request, Response } from 'express';
import dotenv from 'dotenv';
import { sendMail } from './controller/mail';

const bodyParser = require('body-parser');
const cors = require('cors');
dotenv.config();

const port = process.env.PORT || 80;

const app = express();
app.use(
cors({
origin: '*',
}),
);
app.use(bodyParser.json());

app.get('/health', (req: Request, res: Response) => {
const healthcheck: any = {
resource: 'Techno Mailer Server',
uptime: process.uptime(),
responseTime: process.hrtime(),
message: 'OK',
timestamp: Date.now(),
};
try {
res.send(healthcheck);
} catch (error) {
healthcheck.message = error;
res.status(503).send();
}
});

app.post('/send-mail', async (req: Request, res: Response) => {
const { email, subject, text } = req.body;
try {
await sendMail(email, subject, text);
res.status(200).send('Email sent successfully');
} catch (error) {
res.status(500).send('Email failed to send');
}
});

app.listen(port, () => {
console.log(`Server is running at http://localhost:${port}`);
});
9 changes: 9 additions & 0 deletions apps/core-mailer/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "tsconfig/tsconfig.json",
"compilerOptions": {
"outDir": "./build/",
"esModuleInterop": true
},
"include": ["src/**/*"],
"exclude": ["node_modules"]
}
23 changes: 23 additions & 0 deletions apps/core-mailer/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const path = require('path');

module.exports = {
target: 'node',
entry: './src/index.ts',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'app.js',
},
mode: 'production',
module: {
rules: [
{
test: /\.tsx?$/,
use: 'ts-loader',
exclude: /node_modules/,
},
],
},
resolve: {
extensions: ['.tsx', '.ts', '.js'],
},
};
Loading

0 comments on commit 2e5a9e9

Please sign in to comment.