From fb3706ff3862382bfd362396cfb39e8caf8336e0 Mon Sep 17 00:00:00 2001 From: tsuki Date: Wed, 13 Aug 2025 22:00:56 +0800 Subject: [PATCH] rate limit --- src/app.rs | 26 +++++++++++++------------- src/graphql/query.rs | 16 ---------------- 2 files changed, 13 insertions(+), 29 deletions(-) diff --git a/src/app.rs b/src/app.rs index afb35d1..e62bb49 100644 --- a/src/app.rs +++ b/src/app.rs @@ -94,19 +94,19 @@ pub async fn create_router( Router::new() .route("/", get(graphql_playground)) .route("/graphql", get(graphql_playground).post(graphql_handler)) - // .route_layer( - // RateLimitLayer::::builder() - // .with_route( - // (Method::GET, "/graphql"), - // Quota::new(Duration::from_millis(100), NonZero::new(10).unwrap()), - // ) - // .with_route( - // (Method::POST, "/graphql"), - // Quota::new(Duration::from_millis(100), NonZero::new(10).unwrap()), - // ) - // .with_gc_interval(1000) - // .default_handle_error(), - // ) + .route_layer( + RateLimitLayer::::builder() + .with_route( + (Method::GET, "/graphql"), + Quota::new(Duration::from_millis(100), NonZero::new(10).unwrap()), + ) + .with_route( + (Method::POST, "/graphql"), + Quota::new(Duration::from_millis(100), NonZero::new(10).unwrap()), + ) + .with_gc_interval(1000) + .default_handle_error(), + ) .route_service("/ws", GraphQLSubscription::new(schema)) .layer(CorsLayer::permissive()) .merge(router) diff --git a/src/graphql/query.rs b/src/graphql/query.rs index a9d7106..fa3de28 100644 --- a/src/graphql/query.rs +++ b/src/graphql/query.rs @@ -1246,8 +1246,6 @@ impl QueryRoot { // ==================== Blog 相关查询 ==================== - /// 获取博客文章列表 - #[graphql(guard = "RequireReadPermission::new(\"blogs\")")] async fn blogs( &self, ctx: &Context<'_>, @@ -1263,7 +1261,6 @@ impl QueryRoot { } /// 根据ID获取博客文章 - #[graphql(guard = "RequireReadPermission::new(\"blogs\")")] async fn blog(&self, ctx: &Context<'_>, id: Uuid) -> Result { let blog_service = ctx.data::()?; blog_service @@ -1273,7 +1270,6 @@ impl QueryRoot { } /// 根据slug获取博客文章 - #[graphql(guard = "RequireReadPermission::new(\"blogs\")")] async fn blog_by_slug(&self, ctx: &Context<'_>, slug: String) -> Result { let blog_service = ctx.data::()?; blog_service @@ -1282,8 +1278,6 @@ impl QueryRoot { .map_err(|e| GraphQLError::new(e.to_string())) } - /// 获取博客文章详情(包含分类和标签) - #[graphql(guard = "RequireReadPermission::new(\"blogs\")")] async fn blog_detail(&self, ctx: &Context<'_>, id: Uuid) -> Result { let blog_service = ctx.data::()?; blog_service @@ -1292,8 +1286,6 @@ impl QueryRoot { .map_err(|e| GraphQLError::new(e.to_string())) } - /// 获取博客统计信息 - #[graphql(guard = "RequireReadPermission::new(\"blogs\")")] async fn blog_stats(&self, ctx: &Context<'_>) -> Result { let blog_service = ctx.data::()?; blog_service @@ -1302,8 +1294,6 @@ impl QueryRoot { .map_err(|e| GraphQLError::new(e.to_string())) } - /// 获取博客分类列表 - #[graphql(guard = "RequireReadPermission::new(\"blog_categories\")")] async fn blog_categories( &self, ctx: &Context<'_>, @@ -1316,8 +1306,6 @@ impl QueryRoot { .map_err(|e| GraphQLError::new(e.to_string())) } - /// 根据ID获取博客分类 - #[graphql(guard = "RequireReadPermission::new(\"blog_categories\")")] async fn blog_category(&self, ctx: &Context<'_>, id: Uuid) -> Result { let blog_service = ctx.data::()?; blog_service @@ -1326,8 +1314,6 @@ impl QueryRoot { .map_err(|e| GraphQLError::new(e.to_string())) } - /// 获取博客标签列表 - #[graphql(guard = "RequireReadPermission::new(\"blog_tags\")")] async fn blog_tags( &self, ctx: &Context<'_>, @@ -1340,8 +1326,6 @@ impl QueryRoot { .map_err(|e| GraphQLError::new(e.to_string())) } - /// 根据ID获取博客标签 - #[graphql(guard = "RequireReadPermission::new(\"blog_tags\")")] async fn blog_tag(&self, ctx: &Context<'_>, id: Uuid) -> Result { let blog_service = ctx.data::()?; blog_service