mirror of
https://github.com/SinTan1729/chhoto-url
synced 2025-04-20 11:50:01 -05:00
Compare commits
3 commits
2594051a54
...
7ad874a1ff
Author | SHA1 | Date | |
---|---|---|---|
7ad874a1ff | |||
604e95aa9c | |||
917be6ade4 |
3 changed files with 51 additions and 43 deletions
|
@ -122,7 +122,7 @@ default, the auto-generated links are adjective-name pairs. You can use UIDs by
|
||||||
the `slug_style` variable to `UID`. You can also set the length of those slug by setting
|
the `slug_style` variable to `UID`. You can also set the length of those slug by setting
|
||||||
the `slug_length` variable. It defaults to 8, and a minimum of 4 is supported.
|
the `slug_length` variable. It defaults to 8, and a minimum of 4 is supported.
|
||||||
|
|
||||||
## CLI usage instructions
|
## Instructions for CLI usage
|
||||||
The application can be used from the terminal using something like `curl`. In all the examples
|
The application can be used from the terminal using something like `curl`. In all the examples
|
||||||
below, replace `http://localhost:4567` with where your instance of `chhoto-url` is accessible.
|
below, replace `http://localhost:4567` with where your instance of `chhoto-url` is accessible.
|
||||||
|
|
||||||
|
@ -136,7 +136,7 @@ request, please add `-b cookie.txt` to use this authentication cookie.
|
||||||
|
|
||||||
To add a link, do
|
To add a link, do
|
||||||
```bash
|
```bash
|
||||||
curl -X POST -d '{"shhortlink":"<shortlink>", "longlink":<longlink>}' http://localhost:4567/api/new
|
curl -X POST -d '{"shortlink":"<shortlink>", "longlink":"<longlink>"}' http://localhost:4567/api/new
|
||||||
```
|
```
|
||||||
Send an empty `<shortlink>` if you want it to be auto-generated. The server will reply with the generated shortlink.
|
Send an empty `<shortlink>` if you want it to be auto-generated. The server will reply with the generated shortlink.
|
||||||
|
|
||||||
|
|
|
@ -35,7 +35,7 @@ async fn add_link(req: String, data: web::Data<AppState>, session: Session) -> H
|
||||||
HttpResponse::BadRequest().body(out.1)
|
HttpResponse::BadRequest().body(out.1)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
HttpResponse::Forbidden().body("logged_out")
|
HttpResponse::Forbidden().body("Not logged in!")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -45,7 +45,7 @@ async fn getall(data: web::Data<AppState>, session: Session) -> HttpResponse {
|
||||||
if auth::validate(session) {
|
if auth::validate(session) {
|
||||||
HttpResponse::Ok().body(utils::getall(&data.db))
|
HttpResponse::Ok().body(utils::getall(&data.db))
|
||||||
} else {
|
} else {
|
||||||
HttpResponse::Forbidden().body("logged_out")
|
HttpResponse::Forbidden().body("Not logged in!")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -56,7 +56,7 @@ async fn siteurl(session: Session) -> HttpResponse {
|
||||||
let site_url = env::var("site_url").unwrap_or(String::from("unset"));
|
let site_url = env::var("site_url").unwrap_or(String::from("unset"));
|
||||||
HttpResponse::Ok().body(site_url)
|
HttpResponse::Ok().body(site_url)
|
||||||
} else {
|
} else {
|
||||||
HttpResponse::Forbidden().body("logged_out")
|
HttpResponse::Forbidden().body("Not logged in!")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -124,7 +124,7 @@ async fn delete_link(
|
||||||
HttpResponse::NotFound().body("Not found!")
|
HttpResponse::NotFound().body("Not found!")
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
HttpResponse::Forbidden().body("Wrong password!")
|
HttpResponse::Forbidden().body("Not logged in!")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,35 +6,36 @@ const prepSubdir = (link) => {
|
||||||
return (subdir + link).replace('//', '/');
|
return (subdir + link).replace('//', '/');
|
||||||
}
|
}
|
||||||
|
|
||||||
const getSiteUrl = async () => await fetch(prepSubdir("/api/siteurl"))
|
const getSiteUrl = async () => {
|
||||||
.then(res => res.text())
|
let url = await fetch(prepSubdir("/api/siteurl"))
|
||||||
.then(text => {
|
.then(res => res.text());
|
||||||
if (text == "unset") {
|
if (url == "unset") {
|
||||||
return window.location.host.replace(/\/$/, '');
|
return window.location.host.replace(/\/$/, '');
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return text.replace(/\/$/, '').replace(/^"/, '').replace(/"$/, '');
|
return text.replace(/\/$/, '').replace(/^"/, '').replace(/"$/, '');
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
|
|
||||||
const getVersion = async () => await fetch(prepSubdir("/api/version"))
|
const getVersion = async () => {
|
||||||
.then(res => res.text())
|
let ver = await fetch(prepSubdir("/api/version"))
|
||||||
.then(text => {
|
.then(res => res.text());
|
||||||
return text;
|
return ver;
|
||||||
});
|
}
|
||||||
|
|
||||||
const refreshData = async () => {
|
const refreshData = async () => {
|
||||||
let reply = await fetch(prepSubdir("/api/all")).then(res => res.text());
|
let res = await fetch(prepSubdir("/api/all"));
|
||||||
if (reply == "logged_out") {
|
if (!res.ok) {
|
||||||
console.log("logged_out");
|
let errorMsg = await res.text();
|
||||||
document.getElementById("container").style.filter = "blur(2px)"
|
console.log(errorMsg);
|
||||||
|
document.getElementById("container").style.filter = "blur(2px)";
|
||||||
document.getElementById("login-dialog").showModal();
|
document.getElementById("login-dialog").showModal();
|
||||||
document.getElementById("password").focus();
|
document.getElementById("password").focus();
|
||||||
} else {
|
} else {
|
||||||
data = JSON.parse(reply)
|
let data = await res.json();
|
||||||
displayData(data);
|
displayData(data);
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
const displayData = async (data) => {
|
const displayData = async (data) => {
|
||||||
let version = await getVersion();
|
let version = await getVersion();
|
||||||
|
@ -64,7 +65,7 @@ const displayData = async (data) => {
|
||||||
table.innerHTML = ''; // Clear
|
table.innerHTML = ''; // Clear
|
||||||
data.forEach(tr => table.appendChild(TR(tr, site)));
|
data.forEach(tr => table.appendChild(TR(tr, site)));
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
const showAlert = async (text, col) => {
|
const showAlert = async (text, col) => {
|
||||||
document.getElementById("alert-box")?.remove();
|
document.getElementById("alert-box")?.remove();
|
||||||
|
@ -74,7 +75,7 @@ const showAlert = async (text, col) => {
|
||||||
alertBox.style.color = col;
|
alertBox.style.color = col;
|
||||||
alertBox.innerHTML = text;
|
alertBox.innerHTML = text;
|
||||||
controls.appendChild(alertBox);
|
controls.appendChild(alertBox);
|
||||||
};
|
}
|
||||||
|
|
||||||
const TR = (row, site) => {
|
const TR = (row, site) => {
|
||||||
const tr = document.createElement("tr");
|
const tr = document.createElement("tr");
|
||||||
|
@ -86,7 +87,7 @@ const TR = (row, site) => {
|
||||||
else {
|
else {
|
||||||
shortTD = TD(A_SHORT_INSECURE(row["shortlink"], site), "Short URL");
|
shortTD = TD(A_SHORT_INSECURE(row["shortlink"], site), "Short URL");
|
||||||
}
|
}
|
||||||
hitsTD = TD(row["hits"]);
|
let hitsTD = TD(row["hits"]);
|
||||||
hitsTD.setAttribute("label", "Hits");
|
hitsTD.setAttribute("label", "Hits");
|
||||||
hitsTD.setAttribute("name", "hitsColumn");
|
hitsTD.setAttribute("name", "hitsColumn");
|
||||||
const btn = deleteButton(row["shortlink"]);
|
const btn = deleteButton(row["shortlink"]);
|
||||||
|
@ -97,7 +98,7 @@ const TR = (row, site) => {
|
||||||
tr.appendChild(btn);
|
tr.appendChild(btn);
|
||||||
|
|
||||||
return tr;
|
return tr;
|
||||||
};
|
}
|
||||||
|
|
||||||
const copyShortUrl = async (link) => {
|
const copyShortUrl = async (link) => {
|
||||||
const site = await getSiteUrl();
|
const site = await getSiteUrl();
|
||||||
|
@ -109,7 +110,7 @@ const copyShortUrl = async (link) => {
|
||||||
showAlert("Could not copy short URL to clipboard, please do it manually.", "red");
|
showAlert("Could not copy short URL to clipboard, please do it manually.", "red");
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
}
|
||||||
|
|
||||||
const addProtocol = (input) => {
|
const addProtocol = (input) => {
|
||||||
var url = input.value.trim();
|
var url = input.value.trim();
|
||||||
|
@ -117,7 +118,7 @@ const addProtocol = (input) => {
|
||||||
url = "https://" + url;
|
url = "https://" + url;
|
||||||
}
|
}
|
||||||
input.value = url;
|
input.value = url;
|
||||||
return input
|
return input;
|
||||||
}
|
}
|
||||||
|
|
||||||
const A_LONG = (s) => `<a href='${s}'>${s}</a>`;
|
const A_LONG = (s) => `<a href='${s}'>${s}</a>`;
|
||||||
|
@ -138,7 +139,14 @@ const deleteButton = (shortUrl) => {
|
||||||
showAlert(" ", "black");
|
showAlert(" ", "black");
|
||||||
fetch(prepSubdir(`/api/del/${shortUrl}`), {
|
fetch(prepSubdir(`/api/del/${shortUrl}`), {
|
||||||
method: "DELETE"
|
method: "DELETE"
|
||||||
}).then(_ => refreshData());
|
}).then(res => {
|
||||||
|
if (res.ok) {
|
||||||
|
console.log("Deleted " + shortUrl);
|
||||||
|
} else {
|
||||||
|
console.log("Unable to delete " + shortUrl);
|
||||||
|
}
|
||||||
|
refreshData();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
td.setAttribute("name", "deleteBtn");
|
td.setAttribute("name", "deleteBtn");
|
||||||
|
@ -146,7 +154,7 @@ const deleteButton = (shortUrl) => {
|
||||||
div.appendChild(btn);
|
div.appendChild(btn);
|
||||||
td.appendChild(div);
|
td.appendChild(div);
|
||||||
return td;
|
return td;
|
||||||
};
|
}
|
||||||
|
|
||||||
const TD = (s, u) => {
|
const TD = (s, u) => {
|
||||||
const td = document.createElement("td");
|
const td = document.createElement("td");
|
||||||
|
@ -155,7 +163,7 @@ const TD = (s, u) => {
|
||||||
td.appendChild(div);
|
td.appendChild(div);
|
||||||
td.setAttribute("label", u);
|
td.setAttribute("label", u);
|
||||||
return td;
|
return td;
|
||||||
};
|
}
|
||||||
|
|
||||||
const submitForm = () => {
|
const submitForm = () => {
|
||||||
const form = document.forms.namedItem("new-url-form");
|
const form = document.forms.namedItem("new-url-form");
|
||||||
|
@ -175,7 +183,7 @@ const submitForm = () => {
|
||||||
})
|
})
|
||||||
.then(res => {
|
.then(res => {
|
||||||
if (!res.ok) {
|
if (!res.ok) {
|
||||||
showAlert("Short URL is not valid or it's already in use!", "red");
|
showAlert(res.text(), "red");
|
||||||
return "error";
|
return "error";
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
@ -188,8 +196,8 @@ const submitForm = () => {
|
||||||
shortUrl.value = "";
|
shortUrl.value = "";
|
||||||
refreshData();
|
refreshData();
|
||||||
}
|
}
|
||||||
});
|
})
|
||||||
};
|
}
|
||||||
|
|
||||||
const submitLogin = () => {
|
const submitLogin = () => {
|
||||||
const password = document.getElementById("password");
|
const password = document.getElementById("password");
|
||||||
|
@ -224,4 +232,4 @@ const submitLogin = () => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
submitLogin();
|
submitLogin();
|
||||||
}
|
}
|
||||||
})();
|
})()
|
||||||
|
|
Loading…
Reference in a new issue