1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#![allow(clippy::ptr_arg)]
#[allow(unused_imports)]
use async_trait::async_trait;
#[allow(unused_imports)]
use serde::{Deserialize, Serialize};
#[allow(unused_imports)]
use std::borrow::Cow;
#[allow(unused_imports)]
use wasmbus_rpc::{
client, context, deserialize, serialize, Message, MessageDispatch, RpcError, Transport,
};
pub const SMITHY_VERSION: &str = "1.0";
pub type Headers = std::collections::HashMap<String, String>;
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct HttpRequest {
#[serde(with = "serde_bytes")]
#[serde(default)]
pub body: Vec<u8>,
pub header: Headers,
#[serde(default)]
pub method: String,
#[serde(default)]
pub path: String,
#[serde(rename = "queryString")]
#[serde(default)]
pub query_string: String,
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct HttpResponse {
#[serde(with = "serde_bytes")]
#[serde(default)]
pub body: Vec<u8>,
pub header: Headers,
#[serde(default)]
pub status: String,
#[serde(rename = "statusCode")]
pub status_code: u32,
}
#[async_trait]
pub trait HttpServer {
async fn handle_request(
&self,
ctx: &context::Context<'_>,
arg: &HttpRequest,
) -> Result<HttpResponse, RpcError>;
}
#[async_trait]
pub trait HttpServerReceiver: MessageDispatch + HttpServer {
async fn dispatch(
&self,
ctx: &context::Context<'_>,
message: &Message<'_>,
) -> Result<Message<'_>, RpcError> {
match message.method {
"HandleRequest" => {
let value: HttpRequest = deserialize(message.arg.as_ref())?;
let resp = HttpServer::handle_request(self, ctx, &value).await?;
let buf = Cow::Owned(serialize(&resp)?);
Ok(Message {
method: "HttpServer.HandleRequest",
arg: buf,
})
}
_ => Err(RpcError::MethodNotHandled(format!(
"HttpServer::{}",
message.method
))),
}
}
}
#[derive(Debug)]
pub struct HttpServerSender<T> {
transport: T,
config: client::SendConfig,
}
impl<T: Transport> HttpServerSender<T> {
pub fn new(config: client::SendConfig, transport: T) -> Self {
HttpServerSender { transport, config }
}
}
#[async_trait]
impl<T: Transport + std::marker::Sync + std::marker::Send> HttpServer for HttpServerSender<T> {
#[allow(unused)]
async fn handle_request(
&self,
ctx: &context::Context<'_>,
arg: &HttpRequest,
) -> Result<HttpResponse, RpcError> {
let arg = serialize(arg)?;
let resp = self
.transport
.send(
ctx,
&self.config,
Message {
method: "HandleRequest",
arg: Cow::Borrowed(&arg),
},
)
.await?;
let value = deserialize(resp.arg.as_ref())?;
Ok(value)
}
}