add bash scripts
This commit is contained in:
parent
01b63c6b6d
commit
80d8c8d28a
16
app.js
16
app.js
@ -67,14 +67,14 @@ app.get('/twitter-card.svg', (req, res) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Schedule cleanup job
|
// Schedule cleanup job
|
||||||
cron.schedule('*/10 * * * *', async () => {
|
// cron.schedule('*/10 * * * *', async () => {
|
||||||
try {
|
// try {
|
||||||
await MessageService.cleanup();
|
// await MessageService.cleanup();
|
||||||
console.log('Daily cleanup completed');
|
// console.log('Daily cleanup completed');
|
||||||
} catch (error) {
|
// } catch (error) {
|
||||||
console.error('Cleanup failed:', error);
|
// console.error('Cleanup failed:', error);
|
||||||
}
|
// }
|
||||||
});
|
// });
|
||||||
|
|
||||||
app.use(express.static(path.join(__dirname, 'client/build')));
|
app.use(express.static(path.join(__dirname, 'client/build')));
|
||||||
|
|
||||||
|
|||||||
2
scripts/.crons
Normal file
2
scripts/.crons
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
0 * * * * /root/temp_mail/scripts/delete_messages_24h.sh >> /var/log/temp_mail/cleanup.log 2>&1
|
||||||
|
0 0 * * * /root/temp_mail/scripts/delete_emails_14d.sh >> /var/log/temp_mail/cleanup.log 2>&1
|
||||||
50
scripts/delete_emails_14d.sh
Normal file
50
scripts/delete_emails_14d.sh
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
set -e # Exit on any error
|
||||||
|
|
||||||
|
# Get the directory of the script
|
||||||
|
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
||||||
|
|
||||||
|
# Find node executable
|
||||||
|
node=$(which node)
|
||||||
|
|
||||||
|
# Get database credentials from config
|
||||||
|
db_user=$($node -e "console.log(require('${SCRIPT_DIR}/../src/config/database.js').development.connection.user);")
|
||||||
|
if [ -z "$db_user" ]; then
|
||||||
|
echo "Error: Could not get database user"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
db_pass=$($node -e "console.log(require('${SCRIPT_DIR}/../src/config/database.js').development.connection.password);")
|
||||||
|
if [ -z "$db_pass" ]; then
|
||||||
|
echo "Error: Could not get database password"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
db_name=$($node -e "console.log(require('${SCRIPT_DIR}/../src/config/database.js').development.connection.database);")
|
||||||
|
if [ -z "$db_name" ]; then
|
||||||
|
echo "Error: Could not get database name"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Execute MySQL commands
|
||||||
|
mysql -u"$db_user" -p"$db_pass" "$db_name" <<EOF
|
||||||
|
START TRANSACTION;
|
||||||
|
|
||||||
|
# Delete messages first due to foreign key constraint
|
||||||
|
DELETE m FROM messages m
|
||||||
|
INNER JOIN temp_emails e ON m.temp_email_id = e.id
|
||||||
|
WHERE DATE(e.expires_at) = CURDATE();
|
||||||
|
|
||||||
|
# Then delete the expired emails
|
||||||
|
DELETE FROM temp_emails
|
||||||
|
WHERE DATE(expires_at) = CURDATE();
|
||||||
|
|
||||||
|
COMMIT;
|
||||||
|
EOF
|
||||||
|
|
||||||
|
if [ $? -eq 0 ]; then
|
||||||
|
echo "Successfully cleaned up expired emails and messages"
|
||||||
|
else
|
||||||
|
echo "Error occurred while cleaning up expired emails and messages"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
40
scripts/delete_messages_24h.sh
Normal file
40
scripts/delete_messages_24h.sh
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
set -e # Exit on any error
|
||||||
|
|
||||||
|
# Get the directory of the script
|
||||||
|
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
||||||
|
|
||||||
|
# Find node executable
|
||||||
|
node=$(which node)
|
||||||
|
|
||||||
|
# Get database credentials from config
|
||||||
|
db_user=$($node -e "console.log(require('${SCRIPT_DIR}/../src/config/database.js').development.connection.user);")
|
||||||
|
if [ -z "$db_user" ]; then
|
||||||
|
echo "Error: Could not get database user"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
db_pass=$($node -e "console.log(require('${SCRIPT_DIR}/../src/config/database.js').development.connection.password);")
|
||||||
|
if [ -z "$db_pass" ]; then
|
||||||
|
echo "Error: Could not get database password"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
db_name=$($node -e "console.log(require('${SCRIPT_DIR}/../src/config/database.js').development.connection.database);")
|
||||||
|
if [ -z "$db_name" ]; then
|
||||||
|
echo "Error: Could not get database name"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
mysql -u"$db_user" -p"$db_pass" "$db_name" <<EOF
|
||||||
|
SELECT * FROM messages
|
||||||
|
WHERE created_at < DATE_SUB(NOW(), INTERVAL 24 HOUR);
|
||||||
|
EOF
|
||||||
|
|
||||||
|
if [ $? -eq 0 ]; then
|
||||||
|
echo "Successfully cleaned up expired emails and messages"
|
||||||
|
else
|
||||||
|
echo "Error occurred while cleaning up expired emails and messages"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
@ -57,9 +57,9 @@ router.use(authenticateToken);
|
|||||||
* - temp_email_id
|
* - temp_email_id
|
||||||
* properties:
|
* properties:
|
||||||
* temp_email_id:
|
* temp_email_id:
|
||||||
* type: integer
|
* type: integer | string
|
||||||
* description: ID of the temporary email
|
* description: ID of the temporary email or email address
|
||||||
* example: 123
|
* example: 123 or test@example.com
|
||||||
* responses:
|
* responses:
|
||||||
* 200:
|
* 200:
|
||||||
* description: List of messages successfully retrieved
|
* description: List of messages successfully retrieved
|
||||||
@ -127,11 +127,31 @@ router.use(authenticateToken);
|
|||||||
* type: string
|
* type: string
|
||||||
*/
|
*/
|
||||||
router.post('/list', async (req, res) => {
|
router.post('/list', async (req, res) => {
|
||||||
if (!req.body.temp_email_id) {
|
const { temp_email_id } = req.body;
|
||||||
|
|
||||||
|
if (!temp_email_id) {
|
||||||
return res.status(400).json({ error: 'temp_email_id is required' });
|
return res.status(400).json({ error: 'temp_email_id is required' });
|
||||||
}
|
}
|
||||||
const messages = await Message.query().where('temp_email_id', req.body.temp_email_id);
|
|
||||||
res.json(messages);
|
const isInteger = Number.isInteger(Number(temp_email_id));
|
||||||
|
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
||||||
|
const isEmail = typeof temp_email_id === 'string' && emailRegex.test(temp_email_id);
|
||||||
|
|
||||||
|
if (!isInteger && !isEmail) {
|
||||||
|
return res.status(400).json({ error: 'temp_email_id must be either an integer or a valid email address' });
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isInteger) {
|
||||||
|
const messages = await Message.query().where('temp_email_id', temp_email_id);
|
||||||
|
res.json(messages);
|
||||||
|
} else {
|
||||||
|
const tempEmail = await TempEmail.query().where('email', temp_email_id).first();
|
||||||
|
if (!tempEmail) {
|
||||||
|
return res.status(404).json({ error: 'Temporary email not found' });
|
||||||
|
}
|
||||||
|
const messages = await Message.query().where('temp_email_id', tempEmail.id);
|
||||||
|
res.json(messages);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
router.post('/read/:id', async (req, res) => {
|
router.post('/read/:id', async (req, res) => {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user