web3-proxy/web3_proxy/src/frontend/http_proxy.rs

38 lines
1.2 KiB
Rust
Raw Normal View History

2022-06-05 22:58:47 +03:00
use axum::{http::StatusCode, response::IntoResponse, Extension, Json};
2022-07-07 06:22:09 +03:00
use axum_client_ip::ClientIp;
2022-06-05 22:58:47 +03:00
use std::sync::Arc;
2022-06-16 05:53:37 +03:00
use super::errors::handle_anyhow_error;
2022-08-04 04:10:27 +03:00
use super::{rate_limit_by_ip, rate_limit_by_key};
2022-06-16 05:53:37 +03:00
use crate::{app::Web3ProxyApp, jsonrpc::JsonRpcRequestEnum};
2022-08-05 22:22:23 +03:00
pub async fn public_proxy_web3_rpc(
2022-07-07 06:22:09 +03:00
Json(payload): Json<JsonRpcRequestEnum>,
Extension(app): Extension<Arc<Web3ProxyApp>>,
ClientIp(ip): ClientIp,
2022-06-05 22:58:47 +03:00
) -> impl IntoResponse {
2022-08-04 04:10:27 +03:00
if let Err(x) = rate_limit_by_ip(&app, &ip).await {
return x.into_response();
}
match app.proxy_web3_rpc(payload).await {
Ok(response) => (StatusCode::OK, Json(&response)).into_response(),
Err(err) => handle_anyhow_error(None, None, err).await.into_response(),
}
}
2022-07-07 06:22:09 +03:00
2022-08-04 04:10:27 +03:00
pub async fn user_proxy_web3_rpc(
Json(payload): Json<JsonRpcRequestEnum>,
Extension(app): Extension<Arc<Web3ProxyApp>>,
key: String,
) -> impl IntoResponse {
if let Err(x) = rate_limit_by_key(&app, &key).await {
return x.into_response();
2022-07-07 06:22:09 +03:00
}
match app.proxy_web3_rpc(payload).await {
2022-06-05 22:58:47 +03:00
Ok(response) => (StatusCode::OK, Json(&response)).into_response(),
2022-07-07 06:29:47 +03:00
Err(err) => handle_anyhow_error(None, None, err).await.into_response(),
2022-06-05 22:58:47 +03:00
}
}