-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathrestore-post.js
More file actions
75 lines (63 loc) · 1.79 KB
/
restore-post.js
File metadata and controls
75 lines (63 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
const shell = require('shelljs');
const config = require('config');
const {
verifyAuthenticated
} = require('../../../cronjob/trailingTradeHelper/common');
const { slack } = require('../../../helpers');
const handleRestorePost = async (funcLogger, app) => {
const logger = funcLogger.child({
method: 'POST',
endpoint: '/restore-post'
});
app.route('/restore').post(async (req, res) => {
if (config.get('demoMode')) {
return res.send({
success: false,
status: 403,
message: 'You cannot restore database in the demo mode.',
data: {}
});
}
const authToken = req.header('X-AUTH-TOKEN');
// Verify authentication
const isAuthenticated = await verifyAuthenticated(logger, authToken);
if (isAuthenticated === false) {
logger.info('Not authenticated');
return res.send({
success: false,
status: 403,
message: 'Please authenticate first.',
data: {}
});
}
const { archive } = req.files;
const filepath = `/tmp/${archive.name}`;
archive.mv(filepath);
const result = await new Promise(resolve => {
shell.exec(
`${process.cwd()}/scripts/restore.sh ${config.get(
'mongo.host'
)} ${config.get('mongo.port')} ${filepath}`,
(code, stdout, stderr) => {
resolve({ code, stdout, stderr });
}
);
});
if (result.code !== 0) {
slack.sendMessage(`The restore has failed.`, { symbol: 'global' });
return res.send({
success: false,
status: 500,
message: 'Restore failed',
data: result
});
}
return res.send({
success: true,
status: 200,
message: 'Restore success',
data: result
});
});
};
module.exports = { handleRestorePost };