Added comments
This commit is contained in:
@@ -11,6 +11,7 @@ const map = require("../utils/map.util.js");
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
// Gets an Express response object an emoji code and a string message and renders as error message
|
||||
function errorPageRenderer(res, emoji, message) {
|
||||
res.render("error", {
|
||||
page_title: app_conf.name,
|
||||
@@ -23,6 +24,7 @@ function errorPageRenderer(res, emoji, message) {
|
||||
})
|
||||
}
|
||||
|
||||
// Disable map endpoint
|
||||
router.get("/disable", async (req, res) => {
|
||||
logger.info(`${req.method}: "${req.url}" => ${req.get("User-Agent")}`);
|
||||
|
||||
@@ -31,6 +33,7 @@ router.get("/disable", async (req, res) => {
|
||||
res.status(200).send();
|
||||
});
|
||||
|
||||
// Enable map endpoint
|
||||
router.get("/enable", async (req, res) => {
|
||||
logger.info(`${req.method}: "${req.url}" => ${req.get("User-Agent")}`);
|
||||
|
||||
@@ -45,17 +48,20 @@ router.get("/enable", async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
// Remove map endpoint
|
||||
router.delete("/remove", async (req, res) => {
|
||||
logger.info(`${req.method}: "${req.url}" => ${req.get("User-Agent")}`);
|
||||
|
||||
// Get map name
|
||||
var element = req.query["element"];
|
||||
if (element) {
|
||||
try {
|
||||
// Delete map file and check if the map was selected - to disable it
|
||||
fs.unlinkSync(path.join(path.dirname(require.main.filename), map_conf.map_storage_path, element));
|
||||
if (fs.existsSync(path.join(path.dirname(require.main.filename), map_conf.map_storage_path, "selected"))) {
|
||||
selected_map_name = fs.readFileSync(path.join(path.dirname(require.main.filename), map_conf.map_storage_path, "selected"), "utf-8");
|
||||
if (selected_map_name == element) {
|
||||
fs.unlinkSync(path.join(path.dirname(require.main.filename), map_conf.map_storage_path, "selected"));
|
||||
fs.writeFileSync(path.join(path.dirname(require.main.filename), map_conf.map_storage_path, "selected"), "");
|
||||
}
|
||||
}
|
||||
res.status(200).send();
|
||||
@@ -68,6 +74,7 @@ router.delete("/remove", async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
// Upload map file
|
||||
router.post("/upload", async (req, res) => {
|
||||
logger.info(`${req.method}: "${req.url}" => ${req.get("User-Agent")}`);
|
||||
|
||||
@@ -87,6 +94,7 @@ router.post("/upload", async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
// Download map
|
||||
router.get("/download", async (req, res) => {
|
||||
logger.info(`${req.method}: "${req.url}" => ${req.get("User-Agent")}`);
|
||||
|
||||
@@ -97,9 +105,11 @@ router.get("/download", async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
// Add new map
|
||||
router.post("/add", async (req, res) => {
|
||||
logger.info(`${req.method}: "${req.url}" => ${req.get("User-Agent")}`);
|
||||
|
||||
// Get new map data and write it to csv
|
||||
if (req.body) {
|
||||
await map.addMap(`${map_conf.prefix}_${uuid.v4()}.${map_conf.extension}`, req.body["meta"], req.body["throttle"], req.body["motor"]);
|
||||
|
||||
@@ -109,11 +119,14 @@ router.post("/add", async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
// Edit map data
|
||||
router.post("/edit", async (req, res) => {
|
||||
logger.info(`${req.method}: "${req.url}" => ${req.get("User-Agent")}`);
|
||||
|
||||
// Check if a map name and data have been received
|
||||
if (req.query["element"] && req.body) {
|
||||
try {
|
||||
// Update the map file
|
||||
await map.updateMap(req.query["element"], req.body["meta"], req.body["throttle"], req.body["motor"]);
|
||||
|
||||
res.status(200).send();
|
||||
@@ -126,6 +139,7 @@ router.post("/edit", async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
// Add map page
|
||||
router.get("/add", async (req, res) => {
|
||||
logger.info(`${req.method}: "${req.url}" => ${req.get("User-Agent")}`);
|
||||
|
||||
@@ -142,6 +156,7 @@ router.get("/add", async (req, res) => {
|
||||
});
|
||||
});
|
||||
|
||||
// Edit map page
|
||||
router.get("/edit", async (req, res) => {
|
||||
logger.info(`${req.method}: "${req.url}" => ${req.get("User-Agent")}`);
|
||||
|
||||
@@ -174,6 +189,7 @@ router.get("/edit", async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
// Main page
|
||||
router.get("/", async (req, res) => {
|
||||
logger.info(`${req.method}: "${req.url}" => ${req.get("User-Agent")}`);
|
||||
|
||||
@@ -181,10 +197,12 @@ router.get("/", async (req, res) => {
|
||||
const maps = await map.getAvailableMaps();
|
||||
var selected_map_name = "";
|
||||
|
||||
// Check if a map has been selected to show it
|
||||
if (fs.existsSync(path.join(path.dirname(require.main.filename), map_conf.map_storage_path, "selected"))) {
|
||||
selected_map_name = fs.readFileSync(path.join(path.dirname(require.main.filename), map_conf.map_storage_path, "selected"), "utf-8");
|
||||
}
|
||||
|
||||
// Construct the maps table
|
||||
var table_data = "";
|
||||
if (Object.keys(maps).length) {
|
||||
for (m of Object.keys(maps)) {
|
||||
@@ -229,6 +247,7 @@ router.get("/", async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
// About page
|
||||
router.get("/about", (req, res) => {
|
||||
res.render("about", {
|
||||
page_title: app_conf.name,
|
||||
|
||||
@@ -9,7 +9,8 @@ router.use(app_conf.paths.js.web, express.static(path.join(path.dirname(require.
|
||||
router.use(app_conf.paths.chartjs.web, express.static(path.join(path.dirname(require.main.filename), app_conf.paths.chartjs.src)));
|
||||
|
||||
/*
|
||||
* Main endpoint - might implement authentication step here...
|
||||
* Main endpoint - authentication happens here
|
||||
* Rewritting the whole url to fix logout bug
|
||||
*/
|
||||
router.get("/", (req, res) => {
|
||||
res.redirect(`${app_conf.protocol}://${app_conf.host}:${app_conf.port}${app_conf.app_url}`);
|
||||
|
||||
Reference in New Issue
Block a user