-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
483 additions
and
181 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
[package] | ||
name = "yad-net" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
async-trait = {workspace = true} | ||
async-std = {workspace = true} | ||
|
||
|
||
jsonrpsee = { workspace = true, features=["server"] } | ||
salvo = { workspace = true} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod rpc; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
use jsonrpsee::RpcModule; | ||
use salvo::http::ParseError; | ||
use salvo::prelude::*; | ||
use std::str::from_utf8; | ||
use std::sync::Arc; | ||
|
||
pub trait RpcReply { | ||
type Output: Send + Sync + 'static; | ||
} | ||
|
||
pub async fn handler<T>(module: Option<&Arc<RpcModule<T>>>, req: &mut Request, res: &mut Response) { | ||
let payload = req | ||
.payload() | ||
.await | ||
.map(|msg| msg.as_slice()) | ||
.and_then(|msg| from_utf8(msg).map_err(|_| ParseError::ParseFromStr)); | ||
if let (Some(module), Ok(msg)) = (module, payload) { | ||
if let Ok((result, mut _stream)) = module.raw_json_request(msg).await { | ||
let result = result.result; | ||
res.set_status_code(StatusCode::OK); | ||
res.render(result); | ||
return; | ||
} | ||
return res.set_status_code(StatusCode::INTERNAL_SERVER_ERROR); | ||
} | ||
return res.set_status_code(StatusCode::INTERNAL_SERVER_ERROR); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
use salvo::prelude::*; | ||
use std::sync::Arc; | ||
|
||
mod processor; | ||
mod rpc; | ||
|
||
pub struct Scope {} | ||
|
||
impl Scope { | ||
pub fn new() -> Self { | ||
Scope {} | ||
} | ||
|
||
pub fn router() -> Router { | ||
let rpc_context = rpc::YadTrackingRpcContext; | ||
let rpc_module = Arc::new(rpc_context.module()); | ||
Router::with_path("tracking") | ||
.hoop(affix::inject(rpc_module)) | ||
.post(rpc_handler) | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
use async_trait::async_trait; | ||
use jsonrpsee::core::*; | ||
use jsonrpsee::proc_macros::rpc; | ||
use jsonrpsee::RpcModule; | ||
use salvo::prelude::*; | ||
use std::sync::Arc; | ||
use yad_net::rpc::RpcReply; | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::net::router; | ||
use salvo::prelude::*; | ||
|
||
#[async_std::test] | ||
async fn handle_rpc_test() { | ||
let addr = "127.0.0.1:7878"; | ||
let route = router(); | ||
async_std::task::spawn(Server::new(TcpListener::bind(addr)).serve(route)); | ||
|
||
let client = reqwest::Client::new(); | ||
let res = client | ||
.post("http://127.0.0.1:7878/tracking") | ||
.body(r#"{"jsonrpc": "2.0", "method": "tracking_bar", "params": [42, 23], "id": 1}"#) | ||
.send() | ||
.await | ||
.unwrap() | ||
.text() | ||
.await; | ||
dbg!(&res); | ||
} | ||
} | ||
|
||
#[handler] | ||
async fn rpc_handler(req: &mut Request, res: &mut Response, depot: &mut Depot) { | ||
let module = depot.obtain::<Arc<YadTrackingRpcModule>>(); | ||
yad_net::rpc::handler(module, req, res).await; | ||
} | ||
|
||
pub type YadTrackingRpcModule = RpcModule<YadTrackingRpcContext>; | ||
|
||
impl RpcReply for YadTrackingReply { | ||
type Output = Self; | ||
} | ||
|
||
#[derive(Serialize)] | ||
pub enum YadTrackingReply { | ||
Processed, | ||
} | ||
|
||
#[rpc(server, namespace = "tracking", server_bounds(T::Output: jsonrpsee::core::Serialize))] | ||
pub trait YadTrackingRpc<T: RpcReply> { | ||
#[method(name = "bar")] | ||
async fn bar(&self) -> RpcResult<T::Output>; | ||
|
||
#[method(name = "foo")] | ||
fn foo(&self) -> RpcResult<()>; | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct YadTrackingRpcContext; | ||
|
||
impl YadTrackingRpcContext { | ||
pub fn module(self) -> YadTrackingRpcModule { | ||
self.into_rpc() | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl YadTrackingRpcServer<YadTrackingReply> for YadTrackingRpcContext { | ||
async fn bar(&self) -> Result<<YadTrackingReply as RpcReply>::Output, Error> { | ||
Ok(YadTrackingReply::Processed) | ||
} | ||
|
||
fn foo(&self) -> RpcResult<()> { | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[package] | ||
name = "yad" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] |
Oops, something went wrong.