1
0
Fork 0
mirror of https://github.com/SinTan1729/chhoto-url synced 2024-12-26 23:58:35 -06:00

Code cleanup

This commit is contained in:
Sayantan Santra 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);
post("/new", Routes::addUrl);
delete("/:shortUrl", Routes::delete);
get("/siteUrl", Routes::siteUrl);
get("/site", Routes::getSiteUrl);
});
get("/:shortUrl", Routes::goToLongUrl);

View file

@ -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");
}

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")
.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) => `<a href='${s}'>${s}</a>`;