Skip to content

Commit

Permalink
add DockerFile for email_service
Browse files Browse the repository at this point in the history
  • Loading branch information
Nguyen-Duc-Khai committed Dec 12, 2023
1 parent 2f94f1d commit 3c36a8d
Show file tree
Hide file tree
Showing 11 changed files with 213 additions and 33 deletions.
9 changes: 9 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ services:
networks:
- node-network

email_service:
build:
context: ./email-service
dockerfile: Dockerfile
restart: unless-stopped
container_name: email_service
networks:
- node-network

prediction_service:
build:
context: ./prediction-service
Expand Down
3 changes: 2 additions & 1 deletion email-service/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
node_modules
.env
20 changes: 20 additions & 0 deletions email-service/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Use an official Node.js runtime as a parent image
FROM node:alpine

# Set the working directory to /app
WORKDIR /app

# Copy package.json and package-lock.json to the container
COPY package*.json ./

# Install pm2
RUN npm install pm2 -g

# Install app dependencies
RUN npm ci

# Copy the rest of the app source code to the container
COPY . .

# Start the app by running the "npm start" command
CMD ["npm", "start"]
16 changes: 8 additions & 8 deletions email-service/app.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
const config = require("./config.json");
const logger = require("./logger");
const config = require("./config/config");
const logger = require("./config/logger");
const amqp = require("amqplib");

const sendMail = require("./transporter");

async function consumeMessages() {
try {
const connection = await amqp.connect(config.amqp.url);
const connection = await amqp.connect(config.rabbitmq.url);

logger.info("Connect RabbitMQ successfull");

const channel = await connection.createChannel();

await channel.assertExchange(config.amqp.exchangeName, "direct");
await channel.assertExchange(config.rabbitmq.exchangeName, "direct");

const q = await channel.assertQueue(config.amqp.queueName);
const q = await channel.assertQueue(config.rabbitmq.queueName);

await channel.bindQueue(
q.queue,
config.amqp.exchangeName,
config.amqp.bindingKey
config.rabbitmq.exchangeName,
config.rabbitmq.bindingKey
);

channel.consume(q.queue, async (msg) => {
const content = JSON.parse(msg.content);

logger.info(
`Receive message from exchange [${
config.amqp.exchangeName
config.rabbitmq.exchangeName
}], data ${JSON.stringify(content)}`
);

Expand Down
15 changes: 0 additions & 15 deletions email-service/config.json

This file was deleted.

44 changes: 44 additions & 0 deletions email-service/config/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const dotenv = require("dotenv");
const path = require("path");
const Joi = require("joi");

dotenv.config({ path: path.join(__dirname, "../.env") });

const envVarsSchema = Joi.object()
.keys({
NODE_ENV: Joi.string()
.valid("production", "development", "test")
.required(),
RABBITMQ: Joi.string(),
RABBITMQ_EXCHANGE_NAME: Joi.string(),
RABBITMQ_QUEUE_NAME: Joi.string(),
RABBITMQ_BINDING_KEY: Joi.string(),
SMTP_PORT: Joi.string(),
SMTP_HOST: Joi.string(),
SMTP_USERNAME: Joi.string(),
SMTP_PASSWORD: Joi.string(),
})
.unknown();

const { value: envVars, error } = envVarsSchema
.prefs({ errors: { label: "key" } })
.validate(process.env);

if (error) {
throw new Error(`Config validation error: ${error.message}`);
}

module.exports = {
rabbitmq: {
url: envVars.RABBITMQ,
exchangeName: envVars.RABBITMQ_EXCHANGE_NAME,
queueName: envVars.RABBITMQ_QUEUE_NAME,
bindingKey: envVars.RABBITMQ_BINDING_KEY,
},
smtp: {
port: envVars.SMTP_PORT,
host: envVars.SMTP_HOST,
username: envVars.SMTP_USERNAME,
password: envVars.SMTP_PASSWORD,
},
};
File renamed without changes.
15 changes: 15 additions & 0 deletions email-service/ecosystem.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"apps": [
{
"name": "email_service",
"script": "app.js",
"instances": 1,
"autorestart": true,
"watch": false,
"time": true,
"env": {
"NODE_ENV": "production"
}
}
]
}
104 changes: 104 additions & 0 deletions email-service/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion email-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@
"main": "app.js",
"scripts": {
"smtp": "nodemon smtp_server.js",
"app": "nodemon app.js"
"app": "nodemon app.js",
"start": "pm2 start ecosystem.config.json --no-daemon"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"amqplib": "^0.10.3",
"dotenv": "^16.3.1",
"handlebars": "^4.7.8",
"joi": "^17.11.0",
"nodemailer": "^6.9.7",
"nodemon": "^3.0.2",
"smtp-server": "^3.13.0",
Expand Down
15 changes: 7 additions & 8 deletions email-service/transporter.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
const nodemailer = require("nodemailer");
const config = require("./config.json");
const logger = require("./logger");
const config = require("./config/config");
const logger = require("./config/logger");
const fs = require("fs");
const handlebars = require("handlebars");
const textToImage = require("text-to-image");

const transporter = nodemailer.createTransport(
{
host: config.smtp_server.host,
port: config.smtp_server.port,
host: config.smtp.host,
port: config.smtp.port,
secure: true,
auth: {
user: config.smtp_server.username,
pass: config.smtp_server.password,
user: config.smtp.username,
pass: config.smtp.password,
},
},
{
from: `"AQI Alert ⚠️" ${config.smtp_server.username}`,
from: `"AQI Alert ⚠️" ${config.smtp.username}`,
}
);

Expand Down

0 comments on commit 3c36a8d

Please sign in to comment.