From 73ba894e7ed7ccdad7daa862731a5f3ae618eff5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E8=BF=90=E5=AE=B6?= Date: Thu, 6 Jun 2024 15:02:01 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A7=BB=E9=99=A4=E6=97=A0=E7=94=A8=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../{login_cache.rs => account_cache.rs} | 0 library/src/cache/mod.rs | 2 +- library/src/core/local_cache.rs | 39 ------------------- library/src/core/mod.rs | 1 - library/src/middleware/req_token.rs | 2 +- library/src/resp/mod.rs | 1 - library/src/resp/rejection.rs | 34 ---------------- library/src/resp/response.rs | 6 +-- 8 files changed, 3 insertions(+), 82 deletions(-) rename library/src/cache/{login_cache.rs => account_cache.rs} (100%) delete mode 100644 library/src/core/local_cache.rs delete mode 100644 library/src/resp/rejection.rs diff --git a/library/src/cache/login_cache.rs b/library/src/cache/account_cache.rs similarity index 100% rename from library/src/cache/login_cache.rs rename to library/src/cache/account_cache.rs diff --git a/library/src/cache/mod.rs b/library/src/cache/mod.rs index ee743b8..2585d14 100644 --- a/library/src/cache/mod.rs +++ b/library/src/cache/mod.rs @@ -1 +1 @@ -pub mod login_cache; \ No newline at end of file +pub mod account_cache; \ No newline at end of file diff --git a/library/src/core/local_cache.rs b/library/src/core/local_cache.rs deleted file mode 100644 index b9d0d14..0000000 --- a/library/src/core/local_cache.rs +++ /dev/null @@ -1,39 +0,0 @@ -// use std::collections::HashMap; -// use moka::future::Cache; -// -// -// use std::any::{Any, TypeId}; -// use std::collections::HashMap; -// use std::lazy::SyncLazy; -// use moka::sync::Cache; -// -// type CacheKey = String; -// -// // 全局缓存,键为 String,值为 Box -// static GLOBAL_CACHE: SyncLazy>>> = SyncLazy::new(|| { -// HashMap::new() -// }); -// -// fn insert(key: CacheKey, value: T) { -// let cache_type_id = TypeId::of::(); -// let mut cache = GLOBAL_CACHE.entry(cache_type_id).or_insert_with(|| { -// let config = moka::Config::builder().max_capacity(1000).build(); -// Cache::with_config(config) -// }); -// cache.insert(key, Box::new(value)); -// } -// -// fn get(key: CacheKey) -> Option { -// let cache_type_id = TypeId::of::(); -// GLOBAL_CACHE.get(&cache_type_id)?.get(&key)?.downcast::().ok() -// } -// -// fn main() { -// insert("int_key".into(), 42); -// let int_value: Option = get("int_key".into()); -// println!("Integer value: {:?}", int_value); -// -// insert("string_key".into(), "hello".to_string()); -// let string_value: Option = get("string_key".into()); -// println!("String value: {:?}", string_value); -// } \ No newline at end of file diff --git a/library/src/core/mod.rs b/library/src/core/mod.rs index c8911b9..170293b 100644 --- a/library/src/core/mod.rs +++ b/library/src/core/mod.rs @@ -1,4 +1,3 @@ pub mod config; pub mod logger; pub mod db; -pub mod local_cache; diff --git a/library/src/middleware/req_token.rs b/library/src/middleware/req_token.rs index 02fbe74..0f5132a 100644 --- a/library/src/middleware/req_token.rs +++ b/library/src/middleware/req_token.rs @@ -2,7 +2,7 @@ use axum::{extract::Request, middleware::Next, response::{IntoResponse, Response use http::{header, StatusCode}; use jsonwebtoken::{decode, DecodingKey, Validation}; -use crate::{cache::login_cache::LOGIN_CACHE, config, token::Claims}; +use crate::{cache::account_cache::LOGIN_CACHE, config, token::Claims}; const WHITE_LIST: &[(&str, &str)] = &[ ("GET", "/api/v1/users/:id"), diff --git a/library/src/resp/mod.rs b/library/src/resp/mod.rs index eb0d3bd..7c6929d 100644 --- a/library/src/resp/mod.rs +++ b/library/src/resp/mod.rs @@ -1,4 +1,3 @@ -pub mod rejection; pub mod response; pub mod status; pub mod pageable; \ No newline at end of file diff --git a/library/src/resp/rejection.rs b/library/src/resp/rejection.rs deleted file mode 100644 index 90ef222..0000000 --- a/library/src/resp/rejection.rs +++ /dev/null @@ -1,34 +0,0 @@ -use axum::{ - extract::rejection::JsonRejection, - response::{IntoResponse, Response}, -}; -use axum_extra::extract::WithRejection; -use thiserror::Error; - -use super::response::ResErr; - -#[derive(Debug, Error)] -pub enum MyRejection { - // The `#[from]` attribute generates `From for MyRejection` - // implementation. See `thiserror` docs for more information - #[error(transparent)] - JSONExtractor(#[from] JsonRejection), -} - -// We implement `IntoResponse` so MyRejection can be used as a response -impl IntoResponse for MyRejection { - fn into_response(self) -> Response { - let err = match self { - MyRejection::JSONExtractor(x) => match x { - JsonRejection::JsonDataError(e) => ResErr::ErrData(Some(e.body_text())), - JsonRejection::JsonSyntaxError(e) => ResErr::ErrData(Some(e.body_text())), - JsonRejection::MissingJsonContentType(e) => ResErr::ErrData(Some(e.body_text())), - _ => ResErr::ErrSystem(None), - }, - }; - - err.into_response() - } -} - -pub type IRejection = WithRejection; diff --git a/library/src/resp/response.rs b/library/src/resp/response.rs index f962cc8..c59f066 100644 --- a/library/src/resp/response.rs +++ b/library/src/resp/response.rs @@ -156,11 +156,7 @@ impl Display for ResErr { } } -impl StdError for ResErr { - fn source(&self) -> Option<&(dyn StdError + 'static)> { - self.source() - } -} +impl StdError for ResErr {} impl From for ResErr { fn from(value: String) -> Self {