From fb1470b071b4088340dd48a27f53ebdf8d084120 Mon Sep 17 00:00:00 2001 From: yukang Date: Sat, 20 Jan 2024 17:58:48 +0800 Subject: [PATCH 1/2] Fix error message when sent HTTP GET to RPC --- rpc/src/server.rs | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/rpc/src/server.rs b/rpc/src/server.rs index 6d943211d5..8df6efd76b 100644 --- a/rpc/src/server.rs +++ b/rpc/src/server.rs @@ -1,11 +1,13 @@ use crate::IoHandler; -use axum::routing::post; +use axum::response::IntoResponse; +use axum::routing::{get, post}; use axum::{Extension, Router}; use ckb_app_config::RpcConfig; use ckb_async_runtime::Handle; use ckb_error::AnyError; use ckb_logger::info; +use axum::http::StatusCode; use ckb_stop_handler::{new_tokio_exit_rx, CancellationToken}; use futures_util::{SinkExt, TryStreamExt}; use jsonrpc_core::MetaIoHandler; @@ -90,14 +92,21 @@ impl RpcServer { .with_pipeline_size(4); // HTTP and WS server. - let method_router = - post(handle_jsonrpc::>).get(handle_jsonrpc_ws::>); + let post_router = post(handle_jsonrpc::>); + let get_router = if enable_websocket { + get(handle_jsonrpc_ws::>) + } else { + get(get_error_handler) + }; + let method_router = post_router.merge(get_router); + let mut app = Router::new() .route("/", method_router.clone()) .route("/*path", method_router) .layer(Extension(Arc::clone(rpc))) .layer(CorsLayer::permissive()) - .layer(TimeoutLayer::new(Duration::from_secs(30))); + .layer(TimeoutLayer::new(Duration::from_secs(30))) + .layer(Extension(stream_config.clone())); if enable_websocket { let ws_config: StreamServerConfig = @@ -179,3 +188,11 @@ impl RpcServer { Ok(tcp_address) } } + +/// used for compatible with old PRC error responce for GET +async fn get_error_handler() -> impl IntoResponse { + ( + StatusCode::METHOD_NOT_ALLOWED, + "Used HTTP Method is not allowed. POST or OPTIONS is required", + ) +} From 736fb19fee788088419bc85195f0721dd27e01a0 Mon Sep 17 00:00:00 2001 From: yukang Date: Sat, 20 Jan 2024 18:27:24 +0800 Subject: [PATCH 2/2] add /ping as health API --- rpc/src/server.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/rpc/src/server.rs b/rpc/src/server.rs index 8df6efd76b..07a4c20eed 100644 --- a/rpc/src/server.rs +++ b/rpc/src/server.rs @@ -103,6 +103,7 @@ impl RpcServer { let mut app = Router::new() .route("/", method_router.clone()) .route("/*path", method_router) + .route("/ping", get(ping_handler)) .layer(Extension(Arc::clone(rpc))) .layer(CorsLayer::permissive()) .layer(TimeoutLayer::new(Duration::from_secs(30))) @@ -189,6 +190,11 @@ impl RpcServer { } } +/// used for compatible with old health endpoint +async fn ping_handler() -> impl IntoResponse { + "pong" +} + /// used for compatible with old PRC error responce for GET async fn get_error_handler() -> impl IntoResponse { (