86 lines
2.6 KiB
JavaScript
86 lines
2.6 KiB
JavaScript
const checkForServerConnection = async () => {
|
|
const pathToIpConfig = path.join(__dirname, '..', '..', 'ipConfig.json');
|
|
await lockFile.lock(pathToIpConfig);
|
|
try {
|
|
await decryptFileInPlace(pathToIpConfig);
|
|
const ipConfig = await fs.readFile(pathToIpConfig, 'utf-8');
|
|
const { ip } = JSON.parse(ipConfig);
|
|
const response = await fetch(`http://${ip}:5000/heartbeat`);
|
|
if (response.ok) {
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
} catch (error) {
|
|
console.error("Error:", error);
|
|
return false;
|
|
} finally {
|
|
await lockFile.unlock(pathToIpConfig);
|
|
}
|
|
};const express = require('express');
|
|
const crypto = require('crypto');
|
|
const { v4: uuidv4 } = require('uuid');
|
|
|
|
const {httpStatus} = require("../helpers/httpResponses");
|
|
|
|
const router = express.Router();
|
|
|
|
function validateBody(req, res, next) {
|
|
const apiKey = req.headers['authorization'];
|
|
const adminDB = req.app.get('adminDB');
|
|
const {id} = adminDB.readFile();
|
|
|
|
if(apiKey !== id){
|
|
return res.status(httpStatus.UNAUTHORIZED).json(
|
|
{message: "You are not authorized as an admin."})
|
|
}
|
|
|
|
next();
|
|
}
|
|
|
|
router.get('/reset_server', validateBody, (req, res) => {
|
|
const usersDB = req.app.get('usersDB');
|
|
const departmentsDB = req.app.get('departmentsDB');
|
|
const backupSchemesDB = req.app.get('backupSchemesDB');
|
|
const ceoDB = req.app.get('ceoDB');
|
|
|
|
usersDB.writeFile([]);
|
|
departmentsDB.writeFile({});
|
|
backupSchemesDB.writeFile({});
|
|
ceoDB.writeFile({
|
|
id: uuidv4(),
|
|
name: "CEO",
|
|
email: "ceo@yourfirm.com",
|
|
password: crypto.randomBytes(16).toString('hex'),
|
|
department: "CEO"
|
|
});
|
|
|
|
const ceoDepartment = {
|
|
name: "CEO",
|
|
key: crypto.randomBytes(32).toString('hex')
|
|
|
|
}
|
|
|
|
const departmentsJson = departmentsDB.readFile();
|
|
departmentsJson[1] = ceoDepartment;
|
|
departmentsDB.writeFile(departmentsJson);
|
|
|
|
res.status(httpStatus.OK).json({ message: "Resetting the server..." });
|
|
});
|
|
|
|
router.get('/ceo', validateBody, (req, res) => {
|
|
const ceoDB = req.app.get('ceoDB');
|
|
res.status(httpStatus.OK).json({ message: "Retrieving CEO info...", data: ceoDB.readFile() }) // Corrected to readFile
|
|
});
|
|
|
|
router.get('/users', validateBody, (req, res) => {
|
|
const usersDB = req.app.get('usersDB');
|
|
res.status(httpStatus.OK).json({ message: "Retrieving users info...", data: usersDB.readFile() }) // Corrected to readFile
|
|
});
|
|
|
|
router.use((req, res) => {
|
|
return res.status(httpStatus.NOT_FOUND).json({message: "Endpoint not found"});
|
|
})
|
|
|
|
module.exports = router;
|