From 22f57d1c7e372549893aad1dc51300e6e083b322 Mon Sep 17 00:00:00 2001 From: Tsuki Date: Sun, 17 Aug 2025 20:11:51 +0800 Subject: [PATCH] rate limit --- src/app.rs | 43 +++++++++++++++++++++++++++---------------- 1 file changed, 27 insertions(+), 16 deletions(-) diff --git a/src/app.rs b/src/app.rs index da55334..7487319 100644 --- a/src/app.rs +++ b/src/app.rs @@ -10,6 +10,7 @@ use async_graphql::{ use async_graphql_axum::{GraphQLRequest, GraphQLResponse, GraphQLSubscription}; use axum::{ extract::{FromRef, State}, + http::{Method, StatusCode}, response::{Html, IntoResponse}, routing::get, Router, @@ -95,22 +96,28 @@ pub async fn create_router( }; let router = ReverseProxy::new("/api", &config.tile_server_url.as_str()); - 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(), - // ) + + let router_s = if cfg!(debug_assertions) { + Router::new().route("/", get(graphql_playground)) + } else { + Router::new() + }; + + router_s + .route("/graphql", get(not_found).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_service("/ws", GraphQLSubscription::new(schema)) .layer(CorsLayer::permissive()) .merge(router) @@ -133,3 +140,7 @@ async fn graphql_handler( async fn graphql_playground() -> impl IntoResponse { Html(playground_source(GraphQLPlaygroundConfig::new("/graphql"))) } + +async fn not_found() -> impl IntoResponse { + (StatusCode::NOT_FOUND, "Not Found") +}