mirror of
https://github.com/SinTan1729/chhoto-url
synced 2025-04-20 03:40:00 -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_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
|
||||
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
|
||||
```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.
|
||||
|
||||
|
|
|
@ -35,7 +35,7 @@ async fn add_link(req: String, data: web::Data<AppState>, session: Session) -> H
|
|||
HttpResponse::BadRequest().body(out.1)
|
||||
}
|
||||
} 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) {
|
||||
HttpResponse::Ok().body(utils::getall(&data.db))
|
||||
} 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"));
|
||||
HttpResponse::Ok().body(site_url)
|
||||
} 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!")
|
||||
}
|
||||
} else {
|
||||
HttpResponse::Forbidden().body("Wrong password!")
|
||||
HttpResponse::Forbidden().body("Not logged in!")
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,40 +1,41 @@
|
|||
const prepSubdir = (link) => {
|
||||
let thisPage = new URL(window.location.href);
|
||||
let subdir = thisPage.pathname;
|
||||
let out = (subdir+link).replace('//','/');
|
||||
let out = (subdir + link).replace('//', '/');
|
||||
console.log(out);
|
||||
return (subdir+link).replace('//','/');
|
||||
return (subdir + link).replace('//', '/');
|
||||
}
|
||||
|
||||
const getSiteUrl = async () => await fetch(prepSubdir("/api/siteurl"))
|
||||
.then(res => res.text())
|
||||
.then(text => {
|
||||
if (text == "unset") {
|
||||
return window.location.host.replace(/\/$/,'');
|
||||
const getSiteUrl = async () => {
|
||||
let url = await fetch(prepSubdir("/api/siteurl"))
|
||||
.then(res => res.text());
|
||||
if (url == "unset") {
|
||||
return window.location.host.replace(/\/$/, '');
|
||||
}
|
||||
else {
|
||||
return text.replace(/\/$/,'').replace(/^"/,'').replace(/"$/,'');
|
||||
return text.replace(/\/$/, '').replace(/^"/, '').replace(/"$/, '');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
const getVersion = async () => await fetch(prepSubdir("/api/version"))
|
||||
.then(res => res.text())
|
||||
.then(text => {
|
||||
return text;
|
||||
});
|
||||
const getVersion = async () => {
|
||||
let ver = await fetch(prepSubdir("/api/version"))
|
||||
.then(res => res.text());
|
||||
return ver;
|
||||
}
|
||||
|
||||
const refreshData = async () => {
|
||||
let reply = await fetch(prepSubdir("/api/all")).then(res => res.text());
|
||||
if (reply == "logged_out") {
|
||||
console.log("logged_out");
|
||||
document.getElementById("container").style.filter = "blur(2px)"
|
||||
let res = await fetch(prepSubdir("/api/all"));
|
||||
if (!res.ok) {
|
||||
let errorMsg = await res.text();
|
||||
console.log(errorMsg);
|
||||
document.getElementById("container").style.filter = "blur(2px)";
|
||||
document.getElementById("login-dialog").showModal();
|
||||
document.getElementById("password").focus();
|
||||
} else {
|
||||
data = JSON.parse(reply)
|
||||
let data = await res.json();
|
||||
displayData(data);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
const displayData = async (data) => {
|
||||
let version = await getVersion();
|
||||
|
@ -64,7 +65,7 @@ const displayData = async (data) => {
|
|||
table.innerHTML = ''; // Clear
|
||||
data.forEach(tr => table.appendChild(TR(tr, site)));
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
const showAlert = async (text, col) => {
|
||||
document.getElementById("alert-box")?.remove();
|
||||
|
@ -74,7 +75,7 @@ const showAlert = async (text, col) => {
|
|||
alertBox.style.color = col;
|
||||
alertBox.innerHTML = text;
|
||||
controls.appendChild(alertBox);
|
||||
};
|
||||
}
|
||||
|
||||
const TR = (row, site) => {
|
||||
const tr = document.createElement("tr");
|
||||
|
@ -86,7 +87,7 @@ const TR = (row, site) => {
|
|||
else {
|
||||
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("name", "hitsColumn");
|
||||
const btn = deleteButton(row["shortlink"]);
|
||||
|
@ -97,7 +98,7 @@ const TR = (row, site) => {
|
|||
tr.appendChild(btn);
|
||||
|
||||
return tr;
|
||||
};
|
||||
}
|
||||
|
||||
const copyShortUrl = async (link) => {
|
||||
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");
|
||||
}
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
const addProtocol = (input) => {
|
||||
var url = input.value.trim();
|
||||
|
@ -117,7 +118,7 @@ const addProtocol = (input) => {
|
|||
url = "https://" + url;
|
||||
}
|
||||
input.value = url;
|
||||
return input
|
||||
return input;
|
||||
}
|
||||
|
||||
const A_LONG = (s) => `<a href='${s}'>${s}</a>`;
|
||||
|
@ -138,7 +139,14 @@ const deleteButton = (shortUrl) => {
|
|||
showAlert(" ", "black");
|
||||
fetch(prepSubdir(`/api/del/${shortUrl}`), {
|
||||
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");
|
||||
|
@ -146,7 +154,7 @@ const deleteButton = (shortUrl) => {
|
|||
div.appendChild(btn);
|
||||
td.appendChild(div);
|
||||
return td;
|
||||
};
|
||||
}
|
||||
|
||||
const TD = (s, u) => {
|
||||
const td = document.createElement("td");
|
||||
|
@ -155,11 +163,11 @@ const TD = (s, u) => {
|
|||
td.appendChild(div);
|
||||
td.setAttribute("label", u);
|
||||
return td;
|
||||
};
|
||||
}
|
||||
|
||||
const submitForm = () => {
|
||||
const form = document.forms.namedItem("new-url-form");
|
||||
const data ={
|
||||
const data = {
|
||||
"longlink": form.elements["longUrl"].value,
|
||||
"shortlink": form.elements["shortUrl"].value,
|
||||
};
|
||||
|
@ -175,7 +183,7 @@ const submitForm = () => {
|
|||
})
|
||||
.then(res => {
|
||||
if (!res.ok) {
|
||||
showAlert("Short URL is not valid or it's already in use!", "red");
|
||||
showAlert(res.text(), "red");
|
||||
return "error";
|
||||
}
|
||||
else {
|
||||
|
@ -188,8 +196,8 @@ const submitForm = () => {
|
|||
shortUrl.value = "";
|
||||
refreshData();
|
||||
}
|
||||
});
|
||||
};
|
||||
})
|
||||
}
|
||||
|
||||
const submitLogin = () => {
|
||||
const password = document.getElementById("password");
|
||||
|
@ -224,4 +232,4 @@ const submitLogin = () => {
|
|||
e.preventDefault();
|
||||
submitLogin();
|
||||
}
|
||||
})();
|
||||
})()
|
||||
|
|
Loading…
Reference in a new issue