Skip to content

Commit

Permalink
#253 [Devops] Setup mongo and add telegram db Fail.
Browse files Browse the repository at this point in the history
  • Loading branch information
fdhhhdjd committed Apr 12, 2023
1 parent a3ac5fb commit b1c8300
Show file tree
Hide file tree
Showing 28 changed files with 626 additions and 27 deletions.
5 changes: 4 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,8 @@
"MIDDLAWARE",
"Middlawre",
"mutilp"
]
],
"[dockerfile]": {
"editor.defaultFormatter": "ms-azuretools.vscode-docker"
}
}
34 changes: 34 additions & 0 deletions backend-manager-student/docker-compose-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#! Description: Docker Compose Dev
#!@ Created_At : 20-12-2022.
#!@ Update_At: 21-12-2022, 22-12-2022, 16-01-2023, 12-03-2023, 18-03-2023.
#!@ Updated_At: 12-04-2023

version: '3.7'

Expand Down Expand Up @@ -171,12 +172,43 @@ services:
timeout: 3s
retries: 3

###! mongo
mongodb:
container_name: mongodb
image: mongo:latest
build:
context: .
dockerfile: dockerfile.mongo
restart: unless-stopped
environment:
MONGO_INITDB_ROOT_USERNAME: '${MONGO_INITDB_ROOT_USERNAME}'
MONGO_INITDB_ROOT_PASSWORD: '${MONGO_INITDB_ROOT_PASSWORD}'
MONGO_INITDB_DATABASE: '${MONGO_INITDB_DATABASE}'
volumes:
- mongo_db_data:/data/db
ports:
- ${MONGO_INITDB_PORT}:${MONGO_INITDB_PORT}
env_file:
- .env
networks:
- libary_school
healthcheck:
test:
[
'CMD',
'mongosh --username ${MONGO_INITDB_ROOT_USERNAME} --password ${MONGO_INITDB_ROOT_PASSWORD} --eval "printjson(db.serverStatus())"',
]
interval: 10s
timeout: 3s
retries: 3

###! WebServer:NGINX ###
nginx:
image: nginx:1.21.3
container_name: nginx_libary_school
depends_on:
- postgresql
- mongodb
- redis-master
- redis-slave
- admin_api
Expand Down Expand Up @@ -206,6 +238,8 @@ networks:
volumes:
db_data:
driver: local
mongo_db_data:
driver: local
frontend-manager-student:
driver: local
redis-master:
Expand Down
36 changes: 35 additions & 1 deletion backend-manager-student/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!@ Author: Nguyễn Tiến Tài.
#! Description: Docker Compose Production
#!@ Created_At : 20-12-2022.
#!@ Created_At: 20-12-2022.
#!@ Updated_At: 12-04-2023
#!@ Update_At: 21-12-2022, 22-12-2022, 16-01-2023, 12-03-2023, 18-03-2023.

version: '3.7'
Expand Down Expand Up @@ -161,12 +162,43 @@ services:
timeout: 3s
retries: 3

###! mongo
mongodb:
container_name: mongodb
image: mongo:latest
build:
context: .
dockerfile: dockerfile.mongo
restart: unless-stopped
environment:
MONGO_INITDB_ROOT_USERNAME: '${MONGO_INITDB_ROOT_USERNAME}'
MONGO_INITDB_ROOT_PASSWORD: '${MONGO_INITDB_ROOT_PASSWORD}'
MONGO_INITDB_DATABASE: '${MONGO_INITDB_DATABASE}'
volumes:
- mongo_db_data:/data/db
ports:
- ${MONGO_INITDB_PORT}:${MONGO_INITDB_PORT}
env_file:
- .env
networks:
- libary_school
healthcheck:
test:
[
'CMD',
'mongosh --username ${MONGO_INITDB_ROOT_USERNAME} --password ${MONGO_INITDB_ROOT_PASSWORD} --eval "printjson(db.serverStatus())"',
]
interval: 10s
timeout: 3s
retries: 3

###! WebServer:NGINX ###
nginx:
image: nginx:1.21.3
container_name: nginx_libary_school
depends_on:
- postgresql
- mongodb
- redis-master
- admin_api
- user_api
Expand Down Expand Up @@ -195,6 +227,8 @@ networks:
volumes:
db_data:
driver: local
mongo_db_data:
driver: local
frontend-manager-student:
driver: local
redis-master:
Expand Down
13 changes: 13 additions & 0 deletions backend-manager-student/dockerfile.mongo
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM mongo

# copy javascript
COPY /mongo/init-mongo.js /docker-entrypoint-initdb.d/

# Public port 0 0 0 0
CMD ["mongod", "--bind_ip_all"]






12 changes: 6 additions & 6 deletions backend-manager-student/dockerfile.redis.dev
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ FROM redis AS redis-master
# Create foder
RUN \
if [ ! -d /usr/local/bin/scrip ]; then \
mkdir -p /usr/local/bin/scrip;\
mkdir -p /usr/local/bin/scrip;\
fi

# Check file Grant permission file
RUN \
if [ ! -f /usr/local/bin/scrip/log_script_redis_date.txt ]; then \
touch /usr/local/bin/scrip/log_script_redis_date.txt && \
chmod 600 /usr/local/bin/scrip/log_script_redis_date.txt;\
touch /usr/local/bin/scrip/log_script_redis_date.txt && \
chmod 600 /usr/local/bin/scrip/log_script_redis_date.txt;\
fi

# Copy code script
Expand All @@ -25,7 +25,7 @@ COPY scrip /usr/local/bin/scrip/
# Check logrotate exit and Install logrotate
RUN \
if [ ! -x "$(which logrotate)" ]; then \
apt-get update && apt-get install logrotate -y && \
chmod +x /usr/local/bin/scrip/scrip_redis.sh &&\
chmod 755 /usr/local/bin/scrip \
apt-get update && apt-get install logrotate -y && \
chmod +x /usr/local/bin/scrip/scrip_redis.sh &&\
chmod 755 /usr/local/bin/scrip \
; fi
9 changes: 9 additions & 0 deletions backend-manager-student/mongo/init-mongo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const db = db.getSiblingDB('library_school_service');
const user = db.getUser('docker');
if (!db && !user) {
db.createUser({
user: 'docker',
pwd: 'docker',
roles: [{ role: 'readWrite', db: 'library_school_service' }],
});
}
5 changes: 2 additions & 3 deletions backend-manager-student/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,11 @@
"express-rate-limit": "^6.7.0",
"express-session": "^1.17.3",
"geoip-lite": "^1.4.6",
"google-auth-library": "^8.7.0",
"helmet": "^6.0.1",
"ioredis": "^5.2.4",
"jsonwebtoken": "^8.5.1",
"knex": "^2.3.0",
"node-cron": "^3.0.2",
"node-fetch": "^3.3.0",
"nodemailer": "^6.8.0",
"nodemailer-express-handlebars": "^6.0.0",
"sonyflake": "^1.1.2",
Expand All @@ -76,7 +74,8 @@
"swagger-jsdoc": "^6.2.8",
"swagger-ui-express": "^4.6.0",
"ua-parser-js": "^1.0.33",
"crypto": "^1.0.1"
"crypto": "^1.0.1",
"mongoose": "^7.0.3"
},
"devDependencies": {
"jest": "^29.3.1"
Expand Down
18 changes: 11 additions & 7 deletions backend-manager-student/src/share/configs/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,6 @@ module.exports = {
POSTGRES_PASSWORD: process.env.POSTGRES_PASSWORD,
POSTGRES_DB: process.env.POSTGRES_DB,
POSTGRES_PORT: process.env.POSTGRES_PORT,
/**
* @author Nguyễn Tiến Tài
* @created_at 22/01/2023
* @description MAX MIN CONNECT DB
*/
POSTGRES_CONNECT_MIN: process.env.POSTGRES_CONNECT_MIN,
POSTGRES_CONNECT_MAX: process.env.POSTGRES_CONNECT_MAX,
/**
* @author Nguyễn Tiến Tài
* @created_at 22/01/2023
Expand Down Expand Up @@ -140,4 +133,15 @@ module.exports = {
*/
IPA_API_RATE_LIMIT_DURATION: process.env.IPA_API_RATE_LIMIT_DURATION,
IPA_API_RATE_LIMIT: process.env.IPA_API_RATE_LIMIT,
/**
* @author Nguyễn Tiến Tài
* @created_at 09/04/2023
* @description Configs Mongo
*/
MONGO_INIT_DB_ROOT_USERNAME: process.env.MONGO_INITDB_ROOT_USERNAME,
MONGO_INI_TDB_ROOT_PASSWORD: process.env.MONGO_INITDB_ROOT_PASSWORD,
MONGO_INIT_DB_DATABASE: process.env.MONGO_INITDB_DATABASE,
MONGO_INIT_MONGO_PORT: process.env.MONGO_INITDB_PORT,
MONGO_INIT_MONGO_HOST: process.env.MONGO_INITDB_HOST,
MONGO_INIT_DB_SOURCE: process.env.MONGO_INITDB_SOURCE,
};
27 changes: 27 additions & 0 deletions backend-manager-student/src/share/configs/configs.mongodb.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//! CONFIG
const CONFIGS = require('./config');

const DEVELOPER = {
db: {
host: CONFIGS.MONGO_INIT_MONGO_HOST,
port: CONFIGS.MONGO_INIT_MONGO_PORT,
db: CONFIGS.MONGO_INIT_DB_DATABASE,
user: CONFIGS.MONGO_INIT_DB_ROOT_USERNAME,
password: CONFIGS.MONGO_INI_TDB_ROOT_PASSWORD,
source: CONFIGS.MONGO_INIT_DB_SOURCE,
},
};
const PRODUCTION = {
db: {
host: CONFIGS.MONGO_INIT_MONGO_HOST,
port: CONFIGS.MONGO_INIT_MONGO_PORT,
db: CONFIGS.MONGO_INIT_DB_DATABASE,
user: CONFIGS.MONGO_INIT_DB_ROOT_USERNAME,
password: CONFIGS.MONGO_INI_TDB_ROOT_PASSWORD,
source: CONFIGS.MONGO_INIT_DB_SOURCE,
},
};
const config = { DEVELOPER, PRODUCTION };
const env = process.env.NODE_ENV || 'DEVELOPER';

module.exports = config[env];
29 changes: 29 additions & 0 deletions backend-manager-student/src/share/configs/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,7 @@ module.exports = {
REDIS_SERVER_ADMIN: 'admin_unErrorServer',
REDIS_SERVER_STUDENT: 'user_unErrorServer',
REDIS_SERVER_CRON: 'cron_unErrorServer',
REDIS_DB: 'db_fail',
},
/**
* @author Nguyễn Tiến Tài
Expand All @@ -498,5 +499,33 @@ module.exports = {
ADMIN: 'admin',
STUDENT: 'student',
CRON: 'cron',
DB: 'database',
},
/**
* @author Nguyễn Tiến Tài
* @created_at 10/04/2023
* @description NAME_DATABASE
*/
NAME_DATABASE: {
PG: 'PostgreSQL',
MONGO: 'MongoDB',
REDIS: 'REDIS',
},
/**
* @author Nguyễn Tiến Tài
* @created_at 10/04/2023
* @description Setup Mongo
*/
MONGO: {
POOL_SIZE: 50,
STRING_CONNECT: 'mongodb://${user}:${password}@${host}:${port}/${db}?authSource=${source}',
},
PG: {
POSTGRES_CONNECT_MIN: 2,
POSTGRES_CONNECT_MAX: 10,
ACQUIRE_TIMEOUT_MILLIS: 30000,
CREATE_TIMEOUT_MILLIS: 30000,
IDLE_TIMEOUT_MILLIS: 30000,
REAP_INTERVAL_MILLIS: 1000,
},
};
67 changes: 67 additions & 0 deletions backend-manager-student/src/share/db/init.mongodb.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
//! LIBRARY
const mongoose = require('mongoose');

//! CONFIGS
const CONSTANTS = require('../configs/constants');
const HELPERS = require('../utils/helper');
const { db: { host, port, db, user, password, source } } = require('../configs/configs.mongodb');

//! PUBSUB
const { handleException } = require('../utils/redis_pub_sub_helper');

/**
* @author Nguyễn Tiến Tài
* @created_at 10/04/2023
* @description setup nosql MongoDb
*/

const connectString = HELPERS.getURIFromTemplate(CONSTANTS.MONGO.STRING_CONNECT, {
user,
password,
host,
port,
db,
source,
});

// Singleton
class Database {
constructor() {
this.connect();
}

// Connect
// eslint-disable-next-line class-methods-use-this
connect() {
//! DEVELOPERS
// eslint-disable-next-line no-constant-condition, no-self-compare
if (1 === 1) {
mongoose.set('debug', CONSTANTS.DELETED_ENABLE);
mongoose.set('debug', { color: CONSTANTS.DELETED_ENABLE });
}

mongoose.connect(connectString, {
maxPoolSize: CONSTANTS.MONGO.POOL_SIZE,
useNewUrlParser: CONSTANTS.DELETED_ENABLE,
useUnifiedTopology: CONSTANTS.DELETED_ENABLE,
// eslint-disable-next-line no-unused-vars
}).then((_) => {
console.info('CONNECTED TO MONGODB SUCCESS !!');
}).catch((err) => {
handleException(err, CONSTANTS.NAME_SERVER.DB, CONSTANTS.NAME_DATABASE.MONGO);
console.error(err);
});
}

static getInstance() {
if (!Database.instance) {
Database.instance = new Database();
}

return Database.instance;
}
}

const instanceMongoDb = Database.getInstance();

module.exports = instanceMongoDb;
6 changes: 3 additions & 3 deletions backend-manager-student/src/share/db/init_multiple_redis.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ const CONFIGS = require('../configs/config');
* @description Connect Cache Redis Master and Slave
*/
const ConnectionRedis = (REDIS) => {
REDIS.on('connect', function () {
REDIS.on('connect', function() {
console.info(`Client connected to redis Push ${JSON.stringify(this.options.user)}`);
});
REDIS.on('ready', function () {
REDIS.on('ready', function() {
console.info(`Client connected to redis push and ready to use ${JSON.stringify(this.options.host)}...`);
});
REDIS.on('error', (error) => {
console.info(error);
});
REDIS.on('end', function () {
REDIS.on('end', function() {
console.info(`Client disconnected from redis push ${JSON.stringify(this.options.user)}`);
});
REDIS.on('SIGINT', () => {
Expand Down
Loading

0 comments on commit b1c8300

Please sign in to comment.