mirror of
https://github.com/SinTan1729/chhoto-url
synced 2024-12-25 07:18:35 -06:00
new: Got public mode working, testing needed
This commit is contained in:
parent
168cff94a2
commit
0fce881654
4 changed files with 58 additions and 22 deletions
|
@ -30,7 +30,7 @@ const VERSION: &str = env!("CARGO_PKG_VERSION");
|
|||
// Add new links
|
||||
#[post("/api/new")]
|
||||
async fn add_link(req: String, data: web::Data<AppState>, session: Session) -> HttpResponse {
|
||||
if auth::validate(session) {
|
||||
if env::var("public_mode") == Ok(String::from("Enable")) || auth::validate(session) {
|
||||
let out = utils::add_link(req, &data.db);
|
||||
if out.0 {
|
||||
HttpResponse::Created().body(out.1)
|
||||
|
@ -48,19 +48,20 @@ async fn getall(data: web::Data<AppState>, session: Session) -> HttpResponse {
|
|||
if auth::validate(session) {
|
||||
HttpResponse::Ok().body(utils::getall(&data.db))
|
||||
} else {
|
||||
HttpResponse::Unauthorized().body("Not logged in!")
|
||||
let body = if env::var("public_mode") == Ok(String::from("Enable")) {
|
||||
"Using public mode."
|
||||
} else {
|
||||
"Not logged in!"
|
||||
};
|
||||
HttpResponse::Unauthorized().body(body)
|
||||
}
|
||||
}
|
||||
|
||||
// Get the site URL
|
||||
#[get("/api/siteurl")]
|
||||
async fn siteurl(session: Session) -> HttpResponse {
|
||||
if auth::validate(session) {
|
||||
let site_url = env::var("site_url").unwrap_or(String::from("unset"));
|
||||
HttpResponse::Ok().body(site_url)
|
||||
} else {
|
||||
HttpResponse::Unauthorized().body("Not logged in!")
|
||||
}
|
||||
async fn siteurl() -> HttpResponse {
|
||||
let site_url = env::var("site_url").unwrap_or(String::from("unset"));
|
||||
HttpResponse::Ok().body(site_url)
|
||||
}
|
||||
|
||||
// Get the version number
|
||||
|
@ -116,6 +117,16 @@ async fn login(req: String, session: Session) -> HttpResponse {
|
|||
HttpResponse::Ok().body("Correct password!")
|
||||
}
|
||||
|
||||
// Handle logout
|
||||
#[delete("/api/logout")]
|
||||
async fn logout(session: Session) -> HttpResponse {
|
||||
if session.remove("chhoto-url-auth").is_some() {
|
||||
HttpResponse::Ok().body("Logged out!")
|
||||
} else {
|
||||
HttpResponse::Unauthorized().body("You don't seem to be logged in.")
|
||||
}
|
||||
}
|
||||
|
||||
// Delete a given shortlink
|
||||
#[delete("/api/del/{shortlink}")]
|
||||
async fn delete_link(
|
||||
|
@ -168,6 +179,7 @@ async fn main() -> Result<()> {
|
|||
.service(add_link)
|
||||
.service(delete_link)
|
||||
.service(login)
|
||||
.service(logout)
|
||||
.service(Files::new("/", "./resources/").index_file("index.html"))
|
||||
.default_service(web::get().to(error404))
|
||||
})
|
||||
|
|
|
@ -47,7 +47,7 @@
|
|||
</fieldset>
|
||||
</form>
|
||||
|
||||
<p name="loading-text">Loading links table...</p>
|
||||
<p id="loading-text">Loading links table...</p>
|
||||
<table class="pure-table">
|
||||
<caption>Active links</caption>
|
||||
<br />
|
||||
|
@ -65,7 +65,9 @@
|
|||
</table>
|
||||
</div>
|
||||
|
||||
<div name="github-link">
|
||||
<div name="links-div">
|
||||
<a id="admin-button" href="javascript:getLogin()">login</a>
|
||||
|
||||
<a id="version-number" href="https://github.com/SinTan1729/chhoto-url" target="_blank" rel="noopener noreferrer"
|
||||
hidden>Source Code</a>
|
||||
<!-- The version number would be inserted here -->
|
||||
|
|
|
@ -26,14 +26,32 @@ const getVersion = async () => {
|
|||
return ver;
|
||||
}
|
||||
|
||||
const showVersion = async () => {
|
||||
let version = await getVersion();
|
||||
link = document.getElementById("version-number");
|
||||
link.innerText = "v" + version;
|
||||
link.href = "https://github.com/SinTan1729/chhoto-url/releases/tag/" + version;
|
||||
link.hidden = false;
|
||||
}
|
||||
|
||||
const getLogin = async () => {
|
||||
document.getElementById("container").style.filter = "blur(2px)";
|
||||
document.getElementById("login-dialog").showModal();
|
||||
document.getElementById("password").focus();
|
||||
}
|
||||
|
||||
const refreshData = async () => {
|
||||
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();
|
||||
if (errorMsg == "Using public mode.") {
|
||||
loading_text = document.getElementById("loading-text");
|
||||
loading_text.style.display = "none";
|
||||
showVersion();
|
||||
} else {
|
||||
getLogin();
|
||||
}
|
||||
} else {
|
||||
let data = await res.json();
|
||||
displayData(data);
|
||||
|
@ -41,16 +59,14 @@ const refreshData = async () => {
|
|||
}
|
||||
|
||||
const displayData = async (data) => {
|
||||
let version = await getVersion();
|
||||
link = document.getElementById("version-number")
|
||||
link.innerText = "v" + version;
|
||||
link.href = "https://github.com/SinTan1729/chhoto-url/releases/tag/" + version;
|
||||
link.hidden = false;
|
||||
|
||||
showVersion();
|
||||
let site = await getSiteUrl();
|
||||
admin_button = document.getElementById("admin-button");
|
||||
admin_button.innerText = "logout";
|
||||
admin_button.href = "javascript:logOut()";
|
||||
|
||||
table_box = document.querySelector(".pure-table");
|
||||
loading_text = document.getElementsByName("loading-text")[0];
|
||||
loading_text = document.getElementById("loading-text");
|
||||
|
||||
if (data.length == 0) {
|
||||
table_box.style.visibility = "hidden";
|
||||
|
@ -221,6 +237,12 @@ const submitLogin = () => {
|
|||
})
|
||||
}
|
||||
|
||||
const logOut = async () => {
|
||||
let reply = await fetch(prepSubdir("/api/logout"), {method: "DELETE"}).then(res => res.text());
|
||||
console.log(reply);
|
||||
location.reload();
|
||||
}
|
||||
|
||||
(async () => {
|
||||
await refreshData();
|
||||
|
||||
|
|
|
@ -65,7 +65,7 @@ form input[name="shortUrl"]::placeholder {
|
|||
text-transform: none;
|
||||
}
|
||||
|
||||
div[name="github-link"] {
|
||||
div[name="links-div"] {
|
||||
position: absolute;
|
||||
right: 0.5%;
|
||||
top: 0.5%;
|
||||
|
|
Loading…
Reference in a new issue