Skip to content

Commit

Permalink
Update server.js
Browse files Browse the repository at this point in the history
  • Loading branch information
maedi committed Dec 1, 2020
1 parent 0754fb0 commit 4b3c304
Showing 1 changed file with 45 additions and 49 deletions.
94 changes: 45 additions & 49 deletions lib/web/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ const express = require('express')
const path = require('path')
const vm = require("vm");
const fs = require('fs');

const app = express();

// Config.
const port = 3001;
const db_path = 'db.js';

Expand All @@ -18,105 +19,100 @@ app.get('/', (req, res) => {
})

////
// READ.
// READ
////

var load_db = () => {

const data = fs.readFileSync(db_path);
const script = new vm.Script(data);
script.runInThisContext();

return JSON.parse(db);

}

////
// WRITE
////

var save_db = (db) => {
// Save JS file to disk.
try {
fs.writeFileSync(db_path, 'var db = ' + JSON.stringify(JSON.stringify(db)) + ';');
}
catch (err) {
console.log("ERROR: save_db() failed to save.");
console.error(err);
}

}

////
// DELETE.
////

// Handle executions delete request.
app.post('/executions/delete', (req, res) => {
// Handle controls delete request.
app.post('/controls/delete', (req, res) => {

// Get reflection ID.
// Get execution ID that controls share.
var exe_id = req.body.exe_id;
var number = req.body.number;
console.log(number);

// Get database.
db = load_db();

for (let [index, reflection] of db.reflections.entries()) {
if (reflection.e == exe_id && reflection.n == number) {
console.log("DELETED:")
console.log(db.reflections[index]);
db.reflections.splice(index, 1);
// Delete controls.
for (let [index, control] of db.controls.entries()) {
if (control.exe_id == exe_id) {
console.log("DELETE CONTROL:");
console.log(db.controls[index]);
db.controls.splice(index, 1);
}
if (reflection.b != null && reflection.b == exe_id && reflection.n == number) {
console.log("DELETED:")
console.log(db.reflections[index]);
db.reflections.splice(index, 1);
if (control.base_id != null && control.base_id == exe_id) {
console.log("DELETE:");
console.log(db.controls[index]);
db.controls.splice(index, 1);
}
}

// Save database.
try {
fs.writeFileSync(db_path, 'var db = ' + JSON.stringify(JSON.stringify(db)) + ';');
}
catch (err) {
console.error(err)
}
save_db(db)

res.status(200).send({ message: 'ok' });

})

// Handle reflections delete request.
app.post('/reflections/delete', (req, res) => {
// Handle control delete request.
app.post('/control/delete', (req, res) => {

// Get reflection ID.
// Get control ID.
ref_id = req.body.ref_id;

// Get database.
db = load_db();

// Delete reflection.
for (let [index, reflection] of db.reflections.entries()) {
if (reflection.r == ref_id) {
console.log("DELETED:")
console.log(db.reflections[index]);
db.reflections.splice(index, 1);
// Delete control.
for (let [index, control] of db.controls.entries()) {
if (control.r == ref_id) {
console.log("DELETE CONTROL:")
console.log(db.controls[index]);
db.controls.splice(index, 1);
break;
}
}

// Save database.
try {
fs.writeFileSync(db_path, 'var db = ' + JSON.stringify(JSON.stringify(db)) + ';');
}
catch (err) {
console.error(err)
}
save_db(db)

res.status(200).send({ message: 'ok' });

})

////
// UPDATE.
// SERVE
////

// Handle reflections keep request.
app.post('/reflections/keep', (req, res) => {
console.log(req.body);
})

// Listen for requests.
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})

//fs.readFile('student.json', (err, data) => {
// if (err) throw err;
// let student = JSON.parse(data);
// console.log(student);
//});

0 comments on commit 4b3c304

Please sign in to comment.