diff --git a/src/main/java/tk/draganczuk/url/App.java b/src/main/java/tk/draganczuk/url/App.java index 15f6a1f..9efbac0 100644 --- a/src/main/java/tk/draganczuk/url/App.java +++ b/src/main/java/tk/draganczuk/url/App.java @@ -36,7 +36,7 @@ public class App { get("/all", Routes::getAll); post("/new", Routes::addUrl); delete("/:shortUrl", Routes::delete); - get("/siteUrl", Routes::siteUrl); + get("/site", Routes::getSiteUrl); }); get("/:shortUrl", Routes::goToLongUrl); diff --git a/src/main/java/tk/draganczuk/url/Routes.java b/src/main/java/tk/draganczuk/url/Routes.java index 1860b2e..75ab61d 100644 --- a/src/main/java/tk/draganczuk/url/Routes.java +++ b/src/main/java/tk/draganczuk/url/Routes.java @@ -18,17 +18,16 @@ public class Routes { public static String addUrl(Request req, Response res) { var body = req.body(); - if (body.endsWith(";")) { - body = body + "$"; - } var split = body.split(";"); - String longUrl = split[0]; - - if (split[1].equals("$")) { - split[1] = Utils.randomString(); + + String shortUrl; + try { + shortUrl = split[1]; + } catch (ArrayIndexOutOfBoundsException e) { + shortUrl = Utils.randomString(); } - String shortUrl = split[1]; + shortUrl = shortUrl.toLowerCase(); var shortUrlPresent = urlRepository @@ -42,7 +41,7 @@ public class Routes { } } - public static String siteUrl(Request req, Response res) { + public static String getSiteUrl(Request req, Response res) { return System.getenv().getOrDefault("site_url", "unset"); } diff --git a/src/main/resources/public/js/main.js b/src/main/resources/public/js/main.js index c4633b4..91a27e0 100644 --- a/src/main/resources/public/js/main.js +++ b/src/main/resources/public/js/main.js @@ -1,11 +1,13 @@ -const siteName = async () => await fetch("/api/siteUrl").then(res => res.text()).then(text => { - if (text == "unset") { - return window.location.host; - } - else { - return text; - } -}); +const getSiteUrl = async () => await fetch("/api/site") + .then(res => res.text()) + .then(text => { + if (text == "unset") { + return window.location.host; + } + else { + return text; + } + }); const refreshData = async () => { let data = await fetch("/api/all").then(res => res.text()); @@ -23,7 +25,7 @@ const refreshData = async () => { }; const displayData = async (data) => { - let site = await siteName(); + let site = await getSiteUrl(); site = site.replace(/(^\w+:|^)\/\//, ''); table_box = document.querySelector(".pure-table"); if (data.length == 0) { @@ -37,13 +39,13 @@ const displayData = async (data) => { } }; -const addAlertBox = async (s, t) => { +const addAlertBox = async (text, col) => { document.getElementById("alertBox")?.remove(); const controls = document.querySelector(".pure-controls"); const alertBox = document.createElement("p"); alertBox.setAttribute("id", "alertBox"); - alertBox.setAttribute("style", `color:${t}`); - alertBox.innerHTML = s; + alertBox.setAttribute("style", `color:${col}`); + alertBox.innerHTML = text; controls.appendChild(alertBox); }; @@ -62,10 +64,10 @@ const TR = (row, site) => { return tr; }; -const copyShortUrl = async (s) => { - const site = await siteName(); - navigator.clipboard.writeText(`${site}/${s}`); - addAlertBox(`Short URL ${s} copied to clipboard!`, "green"); +const copyShortUrl = async (link) => { + const site = await getSiteUrl(); + navigator.clipboard.writeText(`${site}/${link}`); + addAlertBox(`Short URL ${link} copied to clipboard!`, "green"); }; const A = (s) => `${s}`;