From ec7d8c2133b6fbef78c1796610c480ac5af77e47 Mon Sep 17 00:00:00 2001 From: Giorgio Ravera Date: Tue, 6 Jan 2026 21:56:13 +0100 Subject: [PATCH] separated js functions --- backend/main.py | 5 ---- backend/routes/hosts.py | 5 ++++ backend/routes/login.py | 10 +++++++ frontend/hosts.html | 3 ++- frontend/{app.js => js/hosts.js} | 46 -------------------------------- frontend/js/login.js | 27 +++++++++++++++++++ frontend/js/session.js | 21 +++++++++++++++ frontend/login.html | 2 +- 8 files changed, 66 insertions(+), 53 deletions(-) rename frontend/{app.js => js/hosts.js} (87%) create mode 100644 frontend/js/login.js create mode 100644 frontend/js/session.js diff --git a/backend/main.py b/backend/main.py index 1f4908f..2644991 100644 --- a/backend/main.py +++ b/backend/main.py @@ -82,8 +82,3 @@ async def session_middleware(request: Request, call_next): @app.get("/") def home(request: Request): return FileResponse(os.path.join(FRONTEND_DIR, "hosts.html")) - -# Serve app.js -@app.get("/app.js") -def js(): - return FileResponse(os.path.join(FRONTEND_DIR, "app.js")) diff --git a/backend/routes/hosts.py b/backend/routes/hosts.py index ecbebf6..caccc3a 100644 --- a/backend/routes/hosts.py +++ b/backend/routes/hosts.py @@ -33,6 +33,11 @@ def hosts(request: Request): def css_hosts(): return FileResponse(os.path.join(FRONTEND_DIR, "css/hosts.css")) +# Serve hosts.js +@router.get("/js/hosts.js") +def css_hosts(): + return FileResponse(os.path.join(FRONTEND_DIR, "js/hosts.js")) + # --------------------------------------------------------- # API ENDPOINTS # --------------------------------------------------------- diff --git a/backend/routes/login.py b/backend/routes/login.py index 7b6a6be..91a43db 100644 --- a/backend/routes/login.py +++ b/backend/routes/login.py @@ -43,6 +43,16 @@ def login_page(request: Request): def css_login(): return FileResponse(os.path.join(FRONTEND_DIR, "css/login.css")) +# Serve login.js +@router.get("/js/login.js") +def css_login(): + return FileResponse(os.path.join(FRONTEND_DIR, "js/login.js")) + +# Serve session.js +@router.get("/js/session.js") +def css_login(): + return FileResponse(os.path.join(FRONTEND_DIR, "js/session.js")) + # --------------------------------------------------------- # API ENDPOINTS # --------------------------------------------------------- diff --git a/frontend/hosts.html b/frontend/hosts.html index b74b830..3a19e12 100644 --- a/frontend/hosts.html +++ b/frontend/hosts.html @@ -91,7 +91,8 @@ - + + diff --git a/frontend/app.js b/frontend/js/hosts.js similarity index 87% rename from frontend/app.js rename to frontend/js/hosts.js index f8984fa..9c2e47e 100644 --- a/frontend/app.js +++ b/frontend/js/hosts.js @@ -266,46 +266,6 @@ function resetSorting() { arrows.forEach(a => a.textContent = ""); } -// ----------------------------- -// Login function -// ----------------------------- -async function handleLogin(e) { - e.preventDefault(); - - const user = document.getElementById("username").value.trim(); - const pass = document.getElementById("password").value; - - const res = await fetch("/api/login", { - method: "POST", - headers: { "Content-Type": "application/json" }, - credentials: "include", - body: JSON.stringify({ - username: user, - password: pass - }) - }); - - const data = await res.json(); - - if (data.status === "ok") { - window.location.href = "/hosts"; - } else { - document.getElementById("loginError").textContent = data.error; - } -} - -// ----------------------------- -// Logout function -// ----------------------------- -async function handleLogout() { - await fetch("/api/logout", { - method: "POST", - credentials: "include" - }); - - window.location.href = "/login"; -} - // ----------------------------- // INITIAL TABLE LOAD // ----------------------------- @@ -319,9 +279,3 @@ document.addEventListener("keydown", (e) => { } }); -document.addEventListener("DOMContentLoaded", () => { - const logoutBtn = document.getElementById("logoutBtn"); - if (logoutBtn) { - logoutBtn.addEventListener("click", handleLogout); - } -}); diff --git a/frontend/js/login.js b/frontend/js/login.js new file mode 100644 index 0000000..3b13053 --- /dev/null +++ b/frontend/js/login.js @@ -0,0 +1,27 @@ +// ----------------------------- +// Login function +// ----------------------------- +async function handleLogin(e) { + e.preventDefault(); + + const user = document.getElementById("username").value.trim(); + const pass = document.getElementById("password").value; + + const res = await fetch("/api/login", { + method: "POST", + headers: { "Content-Type": "application/json" }, + credentials: "include", + body: JSON.stringify({ + username: user, + password: pass + }) + }); + + const data = await res.json(); + + if (data.status === "ok") { + window.location.href = "/hosts"; + } else { + document.getElementById("loginError").textContent = data.error; + } +} diff --git a/frontend/js/session.js b/frontend/js/session.js new file mode 100644 index 0000000..a140a4a --- /dev/null +++ b/frontend/js/session.js @@ -0,0 +1,21 @@ +// ----------------------------- +// Logout function +// ----------------------------- +async function handleLogout() { + await fetch("/api/logout", { + method: "POST", + credentials: "include" + }); + + window.location.href = "/login"; +} + +// ----------------------------- +// DOM Ready +// ----------------------------- +document.addEventListener("DOMContentLoaded", () => { + const logoutBtn = document.getElementById("logoutBtn"); + if (logoutBtn) { + logoutBtn.addEventListener("click", handleLogout); + } +}); diff --git a/frontend/login.html b/frontend/login.html index 4f91f95..9e725e3 100644 --- a/frontend/login.html +++ b/frontend/login.html @@ -37,7 +37,7 @@ - + \ No newline at end of file -- 2.47.3