rate limit
Some checks are pending
Docker Build and Push / build (push) Waiting to run

This commit is contained in:
Tsuki 2025-08-17 20:11:51 +08:00
parent f521c2102c
commit 22f57d1c7e

View File

@ -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());
let router_s = if cfg!(debug_assertions) {
Router::new().route("/", get(graphql_playground))
} else {
Router::new()
.route("/", get(graphql_playground))
.route("/graphql", get(graphql_playground).post(graphql_handler))
// .route_layer(
// RateLimitLayer::<RealIp>::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(),
// )
};
router_s
.route("/graphql", get(not_found).post(graphql_handler))
.route_layer(
RateLimitLayer::<RealIp>::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")
}