-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Microservice and boilerplate for mailer
- Loading branch information
1 parent
36ff6f0
commit 2e5a9e9
Showing
9 changed files
with
2,014 additions
and
1,707 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules | ||
build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
module.exports = { | ||
root: true, | ||
extends: ['custom'], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'], | ||
}, | ||
}; |
Oops, something went wrong.