cargo clippy --fix

This commit is contained in:
yly 2023-11-19 05:27:54 +08:00
parent 27c463bdf1
commit a3f84442a4

View File

@ -1,5 +1,5 @@
use axum::extract::Query; use axum::extract::Query;
use axum::http::{HeaderMap, HeaderValue, Uri}; use axum::http::{HeaderMap, HeaderValue};
use axum::response::{Html, Redirect}; use axum::response::{Html, Redirect};
use axum::{extract::State, http::StatusCode, response::IntoResponse, routing::get, Form, Router}; use axum::{extract::State, http::StatusCode, response::IntoResponse, routing::get, Form, Router};
use minijinja::{context, Environment}; use minijinja::{context, Environment};
@ -94,12 +94,9 @@ async fn auth(
return StatusCode::UNAUTHORIZED; return StatusCode::UNAUTHORIZED;
}; };
let mut locked = state.session.lock().await; let mut locked = state.session.lock().await;
if locked.contains_key(&s) { if let std::collections::hash_map::Entry::Occupied(mut e) = locked.entry(s) {
// FIX, when accessed /auth with correct cookie, the cookie's expiration is delayed // FIX, when accessed /auth with correct cookie, the cookie's expiration is delayed
let Some(v) = locked.insert( let Some(v) = Some(e.insert(Instant::now() + Duration::from_secs(*SESSION_ACTIVE_TIME))) else {
s,
Instant::now() + Duration::from_secs(*SESSION_ACTIVE_TIME),
) else {
tracing::info!("session:{} extended", session_token.value()); tracing::info!("session:{} extended", session_token.value());
return StatusCode::UNAUTHORIZED; return StatusCode::UNAUTHORIZED;
}; };
@ -108,13 +105,13 @@ async fn auth(
} }
} }
} }
return StatusCode::UNAUTHORIZED; StatusCode::UNAUTHORIZED
} }
async fn login( async fn login(
State(state): State<Arc<ServerState>>, State(state): State<Arc<ServerState>>,
cookies: Cookies, cookies: Cookies,
Query(mut params): Query<HashMap<String, String>>, Query(params): Query<HashMap<String, String>>,
Form(frm): Form<UserLoginForm>, Form(frm): Form<UserLoginForm>,
) -> Result<Redirect, (StatusCode, &'static str)> { ) -> Result<Redirect, (StatusCode, &'static str)> {
let conn = state.db.acquire().await; let conn = state.db.acquire().await;
@ -137,14 +134,14 @@ async fn login(
let s = Uuid::new_v4(); let s = Uuid::new_v4();
let mut locked = state.session.lock().await; let mut locked = state.session.lock().await;
locked.insert( locked.insert(
s.clone(), s,
Instant::now() + Duration::from_secs(*SESSION_ACTIVE_TIME), Instant::now() + Duration::from_secs(*SESSION_ACTIVE_TIME),
); );
let mut new_cookie = Cookie::new(&*COOKIE_NAME, s.to_string()); let mut new_cookie = Cookie::new(&*COOKIE_NAME, s.to_string());
new_cookie.set_domain(".aaronhu.cn"); new_cookie.set_domain(".aaronhu.cn");
cookies.add(new_cookie); cookies.add(new_cookie);
if let Some(original_uri) = params.get("original_url") { if let Some(original_uri) = params.get("original_url") {
return Ok(Redirect::to(&original_uri)); return Ok(Redirect::to(original_uri));
} }
return Err((StatusCode::ACCEPTED, "ok")); return Err((StatusCode::ACCEPTED, "ok"));
@ -152,7 +149,7 @@ async fn login(
return Err((StatusCode::UNAUTHORIZED, "wrong password")); return Err((StatusCode::UNAUTHORIZED, "wrong password"));
} }
} }
return Err((StatusCode::BAD_GATEWAY, "unreachable")); Err((StatusCode::BAD_GATEWAY, "unreachable"))
} }
async fn login_page(headers: HeaderMap<HeaderValue>) -> impl IntoResponse { async fn login_page(headers: HeaderMap<HeaderValue>) -> impl IntoResponse {
@ -173,11 +170,11 @@ async fn login_page(headers: HeaderMap<HeaderValue>) -> impl IntoResponse {
} }
} }
} }
return Html( Html(
template template
.render(context! { url => String::new() }) .render(context! { url => String::new() })
.unwrap_or("Error".to_string()), .unwrap_or("Error".to_string()),
); )
} }
pub fn check_otp(key_from_db: String, user_input_otp: String) -> bool { pub fn check_otp(key_from_db: String, user_input_otp: String) -> bool {
@ -196,7 +193,7 @@ pub fn check_otp(key_from_db: String, user_input_otp: String) -> bool {
return token == user_input_otp; return token == user_input_otp;
} }
} }
return false; false
} }
async fn gc(state: Arc<ServerState>) -> Result<(), String> { async fn gc(state: Arc<ServerState>) -> Result<(), String> {