Skip to content

Commit

Permalink
Merge pull request #157 from fdhhhdjd/developer
Browse files Browse the repository at this point in the history
#130 [Devops] Setup Limit Request API
  • Loading branch information
fdhhhdjd authored Feb 28, 2023
2 parents 0a841e4 + 0241f59 commit d9316d3
Showing 4 changed files with 35 additions and 8 deletions.
7 changes: 7 additions & 0 deletions backend-manager-student/src/share/configs/config.js
Original file line number Diff line number Diff line change
@@ -125,4 +125,11 @@ module.exports = {
TYPE_PEM: process.env.TYPE_PEM,
MODULUSLENGTH: process.env.MODULUSLENGTH,
GENERAL_KEY_RANDOM: process.env.GENERAL_KEY_RANDOM,
/**
* @author Nguyễn Tiến Tài
* @created_at 28/02/2023
* @description Limit request
*/
IPA_API_RATE_LIMIT_DURATION: process.env.IPA_API_RATE_LIMIT_DURATION,
IPA_API_RATE_LIMIT: process.env.IPA_API_RATE_LIMIT,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const rateLimit = require('express-rate-limit');
const CONFIGS = require('../configs/config');
const { returnReasons } = require('./handle_error');

/**
* @author Nguyễn Tiến Tài
* @created_at 28/02/2023
* @description Limit request
* @function accessStudentMiddleware
*/

module.exports = rateLimit({
windowMs: CONFIGS.IPA_API_RATE_LIMIT_DURATION * 1000, // in milliseconds
max: CONFIGS.IPA_API_RATE_LIMIT,
message: {
status: 429,
message: returnReasons('429'),
},
standardHeaders: true,
});
Original file line number Diff line number Diff line change
@@ -390,13 +390,15 @@ const userController = {
domain: req.headers[CONSTANTS.HEADER_HEADER_FORWARDED_HOST]?.split(':')[0] || '',
maxAge: CONSTANTS._1_MONTH,
});

// Time create token
const access_time = new Date();
let data_device_update = {
last_access_time: access_time,
refresh_token,
public_key: publicKeyString,
};

// Update Device
await HELPER.handleRequest(
user_device_model.updateDevice(data_device_update, result[0].user_id),
@@ -415,12 +417,6 @@ const userController = {
},
});
} else {
MEMORY_CACHE.setBlackListCache(
CONSTANTS.KEY_BACK_LIST,
result[0].user_id,
refresh_token_cookie,
CONSTANTS._20_DAY_S_REDIS,
);
// Remove cookie
res.clearCookie(CONFIGS.KEY_COOKIE);

@@ -436,12 +432,15 @@ const userController = {
});
}
}

// Call Data Fail
if (err) {
return res.status(500).json({
status: 500,
message: returnReasons('500'),
});
}

return res.status(400).json({
status: 400,
message: returnReasons('400'),
Original file line number Diff line number Diff line change
@@ -7,21 +7,22 @@ const userPrivateRouter = require('./users/user.private.route');

//! SHARE MIDDLEWARE
const accessStudentMiddleware = require('../../../share/middleware/access.student.middleware');
const rate_limit_middleware = require('../../../share/middleware/rate_limit.middleware');

/**
* @author Nguyễn Tiến Tài
* @created_at 17/12/2022
* @update_at 11/01/2023
* @description Route Users middleware
*/
router.use('/api/v1/user/private', accessStudentMiddleware, userPrivateRouter);
router.use('/api/v1/user/private', rate_limit_middleware, accessStudentMiddleware, userPrivateRouter);

/**
* @author Nguyễn Tiến Tài
* @created_at 23/02/2023
* @description Route Users not middleware
*/
router.use('/api/v1/user', userRouter);
router.use('/api/v1/user', rate_limit_middleware, userRouter);

router.get('/check-status', (req, res) =>
res.status(200).json({

0 comments on commit d9316d3

Please sign in to comment.