Refactor setup routes and update error messages
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -21,47 +21,6 @@ router.get('/attachments', (req, res) => {
|
|||||||
|
|
||||||
router.get('/checkupdate',authMiddleware, checkUpdates);
|
router.get('/checkupdate',authMiddleware, checkUpdates);
|
||||||
|
|
||||||
router.get('/setup', (req, res) => {
|
|
||||||
fs.readFile(path.join(__dirname, '../setup.json'), 'utf8', (err, data) => {
|
|
||||||
if (err) {
|
|
||||||
ErrorLogger.error('Error reading setup.json:', err);
|
|
||||||
return res.status(500).json({ success: false, message: 'Error reading setup.json.' });
|
|
||||||
}
|
|
||||||
|
|
||||||
const setup = JSON.parse(data);
|
|
||||||
|
|
||||||
if (Object.keys(setup).length === 0 && setup.constructor === Object) {
|
|
||||||
res.render('setup', { setup });
|
|
||||||
} else {
|
|
||||||
res.status(200).json({ message: 'CDN setup is already done.' });
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
router.post('/setup/save', (req, res) => {
|
|
||||||
const setupData = {
|
|
||||||
domain: req.body.domain ?? '',
|
|
||||||
uptime: req.body.uptime ?? ''
|
|
||||||
};
|
|
||||||
|
|
||||||
if (!areAllFieldsEmpty(req.body.ldap)) {
|
|
||||||
setupData.ldap = req.body.ldap;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!areAllFieldsEmpty(req.body.discord)) {
|
|
||||||
setupData.discord = req.body.discord;
|
|
||||||
}
|
|
||||||
|
|
||||||
fs.writeFile(path.join(__dirname, '../setup.json'), JSON.stringify(setupData, null, 2), (err) => {
|
|
||||||
if (err) {
|
|
||||||
ErrorLogger.error('Error writing to setup.json:', err);
|
|
||||||
return res.status(500).json({ success: false, message: 'Error writing to setup.json.' });
|
|
||||||
}
|
|
||||||
|
|
||||||
res.json({ success: true, message: 'Configuration data has been successfully saved.' });
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
router.get('/applyupdate',authMiddleware, async (req, res) => {
|
router.get('/applyupdate',authMiddleware, async (req, res) => {
|
||||||
const updateUrl = 'https://apollon.dinawo.fr/api/download/all';
|
const updateUrl = 'https://apollon.dinawo.fr/api/download/all';
|
||||||
const updateFolder = path.join(__dirname, '..');
|
const updateFolder = path.join(__dirname, '..');
|
||||||
@@ -73,12 +32,12 @@ router.get('/applyupdate',authMiddleware, async (req, res) => {
|
|||||||
logger.info('------After applying the update------');
|
logger.info('------After applying the update------');
|
||||||
res.json({
|
res.json({
|
||||||
success: true,
|
success: true,
|
||||||
message: 'Update applied successfully. Remember to restart the server for the update to take effect. (systemctl restart cdn).'
|
message: 'Mise à jour appliquée avec succès. Pensé à redémarrer le serveur pour que la MàJ soit prise en compte. (systemctl restart cdn).'
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
ErrorLogger.error('Error applying update:', error);
|
ErrorLogger.error('Error applying update:', error);
|
||||||
|
|
||||||
return res.status(500).json({ success: false, message: 'Error applying the update.' });
|
return res.status(500).json({ success: false, message: 'Erreur lors de l\'application de la mise à jour.' });
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -106,8 +65,54 @@ router.get('/translateAll', async (req, res) => {
|
|||||||
|
|
||||||
res.json(translatedFiles);
|
res.json(translatedFiles);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error translating all EJS files:', error.message);
|
console.error('Error translating all EJS files :', error.message);
|
||||||
res.status(500).json({ error: 'Translation error' });
|
res.status(500).json({ error: 'Translation mistake' });
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
router.get('/setup', (req, res) => {
|
||||||
|
fs.readFile(path.join(__dirname, '../setup.json'), 'utf8', (err, data) => {
|
||||||
|
if (err) {
|
||||||
|
ErrorLogger.error('Error reading setup.json:', err);
|
||||||
|
return res.status(500).json({ success: false, message: 'Error reading setup.json.' });
|
||||||
|
}
|
||||||
|
|
||||||
|
const setup = JSON.parse(data);
|
||||||
|
|
||||||
|
if (Object.keys(setup).length === 0 && setup.constructor === Object) {
|
||||||
|
res.render('setup', { setup });
|
||||||
|
} else {
|
||||||
|
res.status(200).json({ success: false, message: 'The CDN configuration is already done.' });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
router.post('/setup/save', (req, res) => {
|
||||||
|
const setupData = {
|
||||||
|
domain: req.body.domain ?? '',
|
||||||
|
uptime: req.body.uptime ?? ''
|
||||||
|
};
|
||||||
|
|
||||||
|
if (!areAllFieldsEmpty(req.body.ldap)) {
|
||||||
|
setupData.ldap = req.body.ldap;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!areAllFieldsEmpty(req.body.discord)) {
|
||||||
|
setupData.discord = req.body.discord;
|
||||||
|
}
|
||||||
|
|
||||||
|
fs.writeFile(path.join(__dirname, '../setup.json'), JSON.stringify(setupData, null, 2), (err) => {
|
||||||
|
if (err) {
|
||||||
|
ErrorLogger.error('Error writing to setup.json:', err);
|
||||||
|
return res.status(500).json({ success: false, message: 'Error writing to setup.json.' });
|
||||||
|
}
|
||||||
|
|
||||||
|
res.json({ success: true, message: 'The configuration data has been saved successfully.' });
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
function areAllFieldsEmpty(obj) {
|
||||||
|
return Object.values(obj).every(val => !val);
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = router;
|
module.exports = router;
|
||||||
|
|||||||
Reference in New Issue
Block a user