diff --git a/actix/src/auth.rs b/actix/src/auth.rs index df09b3f..336b03e 100644 --- a/actix/src/auth.rs +++ b/actix/src/auth.rs @@ -8,21 +8,12 @@ pub fn validate(session: Session) -> bool { } let token = session.get::("session-token"); - if token.is_err() { - false - } else if !check(token.unwrap()) { - false - } else { - true - } + token.is_ok() && check(token.unwrap()) } fn check(token: Option) -> bool { - if token.is_none() { - false - } else { - let token_body = token.unwrap(); - let token_parts: Vec<&str> = token_body.split(";").collect(); + if let Some(token_body) = token { + let token_parts: Vec<&str> = token_body.split(';').collect(); if token_parts.len() < 2 { false } else { @@ -32,13 +23,10 @@ fn check(token: Option) -> bool { .duration_since(SystemTime::UNIX_EPOCH) .expect("Time went backwards!") .as_secs(); - if token_text == "session-token" && time_now < token_time + 1209600 { - // There are 1209600 seconds in 14 days - true - } else { - false - } + token_text == "session-token" && time_now < token_time + 1209600 // There are 1209600 seconds in 14 days } + } else { + false } } diff --git a/actix/src/database.rs b/actix/src/database.rs index 3239217..5057570 100644 --- a/actix/src/database.rs +++ b/actix/src/database.rs @@ -6,7 +6,7 @@ pub fn find_url(shortlink: &str, db: &Connection) -> String { .unwrap(); let links = statement - .query_map([shortlink], |row| Ok(row.get("long_url")?)) + .query_map([shortlink], |row| row.get("long_url")) .unwrap(); let mut longlink = "".to_string(); @@ -33,7 +33,7 @@ pub fn getall(db: &Connection) -> Vec { links } -pub fn add_hit(shortlink: &str, db: &Connection) -> () { +pub fn add_hit(shortlink: &str, db: &Connection) { db.execute( "UPDATE urls SET hits = hits + 1 WHERE short_url = ?1", [shortlink], @@ -42,16 +42,14 @@ pub fn add_hit(shortlink: &str, db: &Connection) -> () { } pub fn add_link(shortlink: String, longlink: String, db: &Connection) -> bool { - match db.execute( + db.execute( "INSERT INTO urls (long_url, short_url, hits) VALUES (?1, ?2, ?3)", (longlink, shortlink, 0), - ) { - Ok(_) => true, - Err(_) => false, - } + ) + .is_ok() } -pub fn delete_link(shortlink: String, db: &Connection) -> () { +pub fn delete_link(shortlink: String, db: &Connection) { db.execute("DELETE FROM urls WHERE short_url = ?1", [shortlink]) .unwrap(); } diff --git a/actix/src/main.rs b/actix/src/main.rs index ca1a540..33b4919 100644 --- a/actix/src/main.rs +++ b/actix/src/main.rs @@ -1,5 +1,3 @@ -use std::env; - use actix_files::{Files, NamedFile}; use actix_session::{storage::CookieSessionStore, Session, SessionMiddleware}; use actix_web::{ @@ -9,6 +7,7 @@ use actix_web::{ App, HttpResponse, HttpServer, Responder, }; use rusqlite::Connection; +use std::env; mod auth; mod database; mod utils; @@ -67,7 +66,7 @@ async fn error404() -> impl Responder { async fn link_handler(shortlink: web::Path, data: web::Data) -> impl Responder { let shortlink_str = shortlink.to_string(); let longlink = utils::get_longurl(shortlink_str, &data.db); - if longlink == "".to_string() { + if longlink == *"" { Redirect::to("/err/404") } else { database::add_hit(shortlink.as_str(), &data.db); diff --git a/actix/src/utils.rs b/actix/src/utils.rs index 4559da0..c798a51 100644 --- a/actix/src/utils.rs +++ b/actix/src/utils.rs @@ -28,14 +28,14 @@ pub fn add_link(req: String, db: &Connection) -> (bool, String) { let mut shortlink; if chunks.len() > 1 { shortlink = chunks[1].to_string().to_lowercase(); - if shortlink == "".to_string() { + if shortlink == *"" { shortlink = random_name(); } } else { shortlink = random_name(); } - if validate_link(shortlink.as_str()) && get_longurl(shortlink.clone(), db) == "".to_string() { + if validate_link(shortlink.as_str()) && get_longurl(shortlink.clone(), db) == *"" { ( database::add_link(shortlink.clone(), longlink, db), shortlink,