Code cleanup

This commit is contained in:
SinTan1729 2022-11-10 23:19:42 -06:00
parent 53ba9eb129
commit e5f1839822
3 changed files with 27 additions and 26 deletions

View File

@ -36,7 +36,7 @@ public class App {
get("/all", Routes::getAll); get("/all", Routes::getAll);
post("/new", Routes::addUrl); post("/new", Routes::addUrl);
delete("/:shortUrl", Routes::delete); delete("/:shortUrl", Routes::delete);
get("/siteUrl", Routes::siteUrl); get("/site", Routes::getSiteUrl);
}); });
get("/:shortUrl", Routes::goToLongUrl); get("/:shortUrl", Routes::goToLongUrl);

View File

@ -18,17 +18,16 @@ public class Routes {
public static String addUrl(Request req, Response res) { public static String addUrl(Request req, Response res) {
var body = req.body(); var body = req.body();
if (body.endsWith(";")) {
body = body + "$";
}
var split = body.split(";"); var split = body.split(";");
String longUrl = split[0]; String longUrl = split[0];
if (split[1].equals("$")) { String shortUrl;
split[1] = Utils.randomString(); try {
shortUrl = split[1];
} catch (ArrayIndexOutOfBoundsException e) {
shortUrl = Utils.randomString();
} }
String shortUrl = split[1];
shortUrl = shortUrl.toLowerCase(); shortUrl = shortUrl.toLowerCase();
var shortUrlPresent = urlRepository 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"); return System.getenv().getOrDefault("site_url", "unset");
} }

View File

@ -1,11 +1,13 @@
const siteName = async () => await fetch("/api/siteUrl").then(res => res.text()).then(text => { const getSiteUrl = async () => await fetch("/api/site")
if (text == "unset") { .then(res => res.text())
return window.location.host; .then(text => {
} if (text == "unset") {
else { return window.location.host;
return text; }
} else {
}); return text;
}
});
const refreshData = async () => { const refreshData = async () => {
let data = await fetch("/api/all").then(res => res.text()); let data = await fetch("/api/all").then(res => res.text());
@ -23,7 +25,7 @@ const refreshData = async () => {
}; };
const displayData = async (data) => { const displayData = async (data) => {
let site = await siteName(); let site = await getSiteUrl();
site = site.replace(/(^\w+:|^)\/\//, ''); site = site.replace(/(^\w+:|^)\/\//, '');
table_box = document.querySelector(".pure-table"); table_box = document.querySelector(".pure-table");
if (data.length == 0) { 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(); document.getElementById("alertBox")?.remove();
const controls = document.querySelector(".pure-controls"); const controls = document.querySelector(".pure-controls");
const alertBox = document.createElement("p"); const alertBox = document.createElement("p");
alertBox.setAttribute("id", "alertBox"); alertBox.setAttribute("id", "alertBox");
alertBox.setAttribute("style", `color:${t}`); alertBox.setAttribute("style", `color:${col}`);
alertBox.innerHTML = s; alertBox.innerHTML = text;
controls.appendChild(alertBox); controls.appendChild(alertBox);
}; };
@ -62,10 +64,10 @@ const TR = (row, site) => {
return tr; return tr;
}; };
const copyShortUrl = async (s) => { const copyShortUrl = async (link) => {
const site = await siteName(); const site = await getSiteUrl();
navigator.clipboard.writeText(`${site}/${s}`); navigator.clipboard.writeText(`${site}/${link}`);
addAlertBox(`Short URL ${s} copied to clipboard!`, "green"); addAlertBox(`Short URL ${link} copied to clipboard!`, "green");
}; };
const A = (s) => `<a href='${s}'>${s}</a>`; const A = (s) => `<a href='${s}'>${s}</a>`;