From 9081853a6fea2c81ee19523d7105f4ff804b61de Mon Sep 17 00:00:00 2001 From: Kwstas Drakontidis Date: Mon, 9 Jun 2025 08:53:29 +0200 Subject: [PATCH] General refactor --- app/routes/private_routes.js | 5 +++++ app/routes/public_routes.js | 10 +--------- app/views/about.ejs | 2 +- app/views/add.ejs | 6 +++--- app/views/auth.ejs | 33 --------------------------------- app/views/edit.ejs | 6 +++--- app/views/error.ejs | 5 ++++- app/views/index.ejs | 5 ++++- samples/app.conf.sample | 3 ++- samples/map.conf.sample | 3 ++- server.js | 5 ++--- src/javascript/add.js | 4 ++-- src/javascript/edit.js | 14 +++++++------- src/javascript/error.js | 4 ++-- src/javascript/index.js | 10 +++++----- 15 files changed, 43 insertions(+), 72 deletions(-) delete mode 100644 app/views/auth.ejs diff --git a/app/routes/private_routes.js b/app/routes/private_routes.js index e2159a4..3925ba9 100644 --- a/app/routes/private_routes.js +++ b/app/routes/private_routes.js @@ -15,6 +15,7 @@ function errorPageRenderer(res, emoji, message) { res.render("error", { page_title: app_conf.name, program: app_conf.program, + app_url: app_conf.app_url, css_path: path.join(app_conf.paths.css.web, "stylesheet.css"), js_path: path.join(app_conf.paths.js.web, "error.js"), error_emoji: emoji, @@ -124,6 +125,7 @@ router.get("/add", async (req, res) => { res.render("add", { page_title: app_conf.name, program: app_conf.program, + app_url: app_conf.app_url, css_path: path.join(app_conf.paths.css.web, "stylesheet.css"), js_path: path.join(app_conf.paths.js.web, "add.js"), chartjs: app_conf.paths.chartjs.web + "/chart.umd.js", @@ -143,6 +145,7 @@ router.get("/edit", async (req, res) => { res.render("edit", { page_title: app_conf.name, program: app_conf.program, + app_url: app_conf.app_url, css_path: path.join(app_conf.paths.css.web, "stylesheet.css"), js_path: path.join(app_conf.paths.js.web, "edit.js"), chartjs: app_conf.paths.chartjs.web + "/chart.umd.js", @@ -203,6 +206,7 @@ router.get("/", async (req, res) => { res.render("index", { page_title: app_conf.name, program: app_conf.program, + app_url: app_conf.app_url, css_path: path.join(app_conf.paths.css.web, "stylesheet.css"), js_path: path.join(app_conf.paths.js.web, "index.js"), map_name_msg: (selected_map_name != "" ? "Using: " + selected_map_name : ""), @@ -222,6 +226,7 @@ router.get("/about", (req, res) => { res.render("about", { page_title: app_conf.name, program: app_conf.program, + app_url: app_conf.app_url, css_path: path.join(app_conf.paths.css.web, "stylesheet.css"), version: app_conf.version }); diff --git a/app/routes/public_routes.js b/app/routes/public_routes.js index ae5a7d9..ad716ec 100644 --- a/app/routes/public_routes.js +++ b/app/routes/public_routes.js @@ -12,15 +12,7 @@ router.use(app_conf.paths.chartjs.web, express.static(path.join(path.dirname(req * Main endpoint - might implement authentication step here... */ router.get("/", (req, res) => { - res.render("auth", { - page_title: app_conf.name, - - program: app_conf.program, - css_path: path.join(app_conf.paths.css.web, "stylesheet.css"), - protocol: app_conf.protocol, - host: app_conf.host, - port: app_conf.port - }); + res.redirect(`${app_conf.protocol}://${app_conf.host}:${app_conf.port}${app_conf.app_url}`); }); module.exports = router; \ No newline at end of file diff --git a/app/views/about.ejs b/app/views/about.ejs index c020fa0..0da6861 100644 --- a/app/views/about.ejs +++ b/app/views/about.ejs @@ -30,7 +30,7 @@ style="font-size: 150%;">GitHub
- 🢀 Return + 🢀 Return diff --git a/app/views/add.ejs b/app/views/add.ejs index 71ff5b0..f285c58 100644 --- a/app/views/add.ejs +++ b/app/views/add.ejs @@ -16,7 +16,7 @@ @@ -55,12 +55,12 @@
- 🢀 Return + 🢀 Return
-
About
+
About
diff --git a/app/views/auth.ejs b/app/views/auth.ejs deleted file mode 100644 index 7ee1d1d..0000000 --- a/app/views/auth.ejs +++ /dev/null @@ -1,33 +0,0 @@ - - - - - - <%= page_title %> - - - - - - - - - - -
- -
-
-
- - Enter -
-
- - - \ No newline at end of file diff --git a/app/views/edit.ejs b/app/views/edit.ejs index 152a679..99504bb 100644 --- a/app/views/edit.ejs +++ b/app/views/edit.ejs @@ -16,7 +16,7 @@ @@ -60,12 +60,12 @@
- 🢀 Return + 🢀 Return
-
About
+
About
diff --git a/app/views/error.ejs b/app/views/error.ejs index 285751c..cb2fbe5 100644 --- a/app/views/error.ejs +++ b/app/views/error.ejs @@ -14,6 +14,9 @@ rel="stylesheet"> + @@ -38,7 +41,7 @@ <%= error_message %>
-
🢀 Return + 🢀 Return diff --git a/app/views/index.ejs b/app/views/index.ejs index f06213e..6c922bb 100644 --- a/app/views/index.ejs +++ b/app/views/index.ejs @@ -14,6 +14,9 @@ rel="stylesheet"> + @@ -47,7 +50,7 @@
-
About
+
About
diff --git a/samples/app.conf.sample b/samples/app.conf.sample index 6264e4b..5e0ffd5 100644 --- a/samples/app.conf.sample +++ b/samples/app.conf.sample @@ -4,7 +4,8 @@ module.exports = { program: "Throttle & Motor Mapping", host: "localhost", protocol: "http", - port: 8080, + port: 8081, + app_url: "/app", username: "admin", password: "password", logger: { diff --git a/samples/map.conf.sample b/samples/map.conf.sample index b6a842d..1c4aa3a 100644 --- a/samples/map.conf.sample +++ b/samples/map.conf.sample @@ -1,7 +1,8 @@ module.exports = { prefix: "map", + extension: "csv", steps: 10, max_rpm: 6500, max_throttle: 1, - selected_map_path: "app/storage/selected" + map_storage_path: "/app/storage" } \ No newline at end of file diff --git a/server.js b/server.js index d0fd5f4..b432c7f 100644 --- a/server.js +++ b/server.js @@ -25,7 +25,7 @@ app.use(fileUpload({ app.use("/", public_routes); -app.use("/app", basicAuth({ +app.use(`${app_conf.app_url}`, basicAuth({ users: { [app_conf.username]: app_conf.password }, @@ -39,8 +39,7 @@ app.use("/app", basicAuth({ app.set("view engine", "ejs"); app.set("views", path.join(__dirname, app_conf.views_directory)); const server = app.listen(app_conf.port, (error) => { - logger.info("Starting Database Editor."); - + logger.info(`Starting ${app_conf.name} ${app_conf.program}.`); fs.rm(path.join(__dirname, app_conf.temp_directory, "/."), { recursive: true }, (error) => { if (error == null) { diff --git a/src/javascript/add.js b/src/javascript/add.js index 1af7cf6..36706f9 100644 --- a/src/javascript/add.js +++ b/src/javascript/add.js @@ -59,7 +59,7 @@ setEventListeners(commit, async () => { } try { - const response = await fetch("/app/add", { + const response = await fetch(`${app_url}/add`, { method: "POST", body: JSON.stringify(req_body), headers: { @@ -69,7 +69,7 @@ setEventListeners(commit, async () => { if (!response.ok) { throw new Error(`Response status: ${response.status}`); } else { - window.location.href = "/app"; + window.location.href = app_url; } } catch (error) { alert(`Add unsuccessful. ${error.message}`); diff --git a/src/javascript/edit.js b/src/javascript/edit.js index 01f7976..b27a04b 100644 --- a/src/javascript/edit.js +++ b/src/javascript/edit.js @@ -32,13 +32,13 @@ setEventListeners(enable, async () => { const map_filename = (new URLSearchParams(window.location.search)).get("element"); try { - const response = await fetch(`/app/enable?element=${map_filename}`, { + const response = await fetch(`${app_url}/enable?element=${map_filename}`, { method: "GET" }); if (!response.ok) { throw new Error(`Response status: ${response.status}`); } else { - window.location.href = "/app"; + window.location.href = app_url; } } catch (error) { alert(`Could not enable map. ${error.message}`); @@ -49,13 +49,13 @@ setEventListeners(remove, async () => { const map_filename = (new URLSearchParams(window.location.search)).get("element"); try { - const response = await fetch(`/app/remove?element=${map_filename}`, { + const response = await fetch(`${app_url}/remove?element=${map_filename}`, { method: "DELETE" }); if (!response.ok) { throw new Error(`Response status: ${response.status}`); } else { - window.location.href = "/app"; + window.location.href = app_url; } } catch (error) { alert(`Remove unsuccessful. ${error.message}`); @@ -100,7 +100,7 @@ setEventListeners(commit, async () => { } try { - const response = await fetch(`/app/edit?element=${map_filename}`, { + const response = await fetch(`${app_url}/edit?element=${map_filename}`, { method: "POST", body: JSON.stringify(req_body), headers: { @@ -110,7 +110,7 @@ setEventListeners(commit, async () => { if (!response.ok) { throw new Error(`Response status: ${response.status}`); } else { - window.location.href = "/app"; + window.location.href = app_url; } } catch (error) { alert(`Edit unsuccessful. ${error.message}`); @@ -123,7 +123,7 @@ setEventListeners(commit, async () => { setEventListeners(download, () => { const map_filename = (new URLSearchParams(window.location.search)).get("element"); - window.location.href = `/app/download?element=${map_filename}`; + window.location.href = `${app_url}/download?element=${map_filename}`; }); var parsed_map = JSON.parse(map); diff --git a/src/javascript/error.js b/src/javascript/error.js index 95b39a1..0609e40 100644 --- a/src/javascript/error.js +++ b/src/javascript/error.js @@ -18,7 +18,7 @@ function setEventListeners(element, cb) { } setEventListeners(add, () => { - window.location.href = "/app/add"; + window.location.href = `${app_url}/add`; }); setEventListeners(upload, async () => { @@ -27,7 +27,7 @@ setEventListeners(upload, async () => { const formData = new FormData(); formData.append('file', file); - fetch('/app/upload', { + fetch(`${app_url}/upload`, { method: 'POST', body: formData }).then((response) => { diff --git a/src/javascript/index.js b/src/javascript/index.js index 18cec0c..a405184 100644 --- a/src/javascript/index.js +++ b/src/javascript/index.js @@ -22,13 +22,13 @@ function setEventListeners(element, cb) { setEventListeners(disable, async () => { try { - const response = await fetch(`/app/disable`, { + const response = await fetch(`${app_url}/disable`, { method: "GET" }); if (!response.ok) { throw new Error(`Response status: ${response.status}`); } else { - window.location.href = "/app"; + window.location.href = app_url; } } catch (error) { alert(`Could not disable map. ${error.message}`); @@ -41,7 +41,7 @@ setEventListeners(upload, async () => { const formData = new FormData(); formData.append('file', file); - fetch('/app/upload', { + fetch(`${app_url}/upload`, { method: 'POST', body: formData }).then((response) => { @@ -58,7 +58,7 @@ setEventListeners(upload, async () => { }); setEventListeners(add, () => { - window.location.href = "/app/add"; + window.location.href = `${app_url}/add`; }); setEventListeners(logout, () => { @@ -69,6 +69,6 @@ const map_rows = map_table.rows; for (let i = 1; i < map_rows.length; i++) { setEventListeners(map_rows[i], () => { - window.location.href = `/app/edit?element=${map_rows[i].cells[0].innerText}`; + window.location.href = `${app_url}/edit?element=${map_rows[i].cells[0].innerText}`; }); } \ No newline at end of file