Clean up main crate and more work on macro
Signed-off-by: Lucio Franco <[email protected]>
This commit is contained in:
+32
-23
@@ -41,14 +41,15 @@ pub fn server(attr: TokenStream, item: TokenStream) -> TokenStream {
|
||||
|
||||
let ts = quote! {
|
||||
use tonic::_codegen;
|
||||
use proto::*;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct GrpcServer {
|
||||
inner: std::sync::Arc<#s>,
|
||||
}
|
||||
|
||||
impl From<#s> for GrpcServer {
|
||||
fn from(t: #s) -> Self {
|
||||
impl GrpcServer {
|
||||
fn new(t: #s) -> Self {
|
||||
Self { inner: std::sync::Arc::new(t) }
|
||||
}
|
||||
}
|
||||
@@ -67,8 +68,8 @@ pub fn server(attr: TokenStream, item: TokenStream) -> TokenStream {
|
||||
}
|
||||
}
|
||||
|
||||
impl _codegen::Service<_codegen::http::Request<()>> for GrpcServer {
|
||||
type Response = tonic::Response<tonic::body::BoxAsyncBody>;
|
||||
impl _codegen::Service<_codegen::http::Request<tower_h2::RecvBody>> for GrpcServer {
|
||||
type Response = _codegen::http::Response<tonic::body::BoxAsyncBody>;
|
||||
type Error = tonic::error::Never;
|
||||
type Future = _codegen::ResponseFuture2<Self::Response, Self::Error>;
|
||||
|
||||
@@ -76,40 +77,48 @@ pub fn server(attr: TokenStream, item: TokenStream) -> TokenStream {
|
||||
Ok(()).into()
|
||||
}
|
||||
|
||||
fn call(&mut self, request: _codegen::http::Request<()>) -> Self::Future {
|
||||
fn call(&mut self, request: _codegen::http::Request<tower_h2::RecvBody>) -> Self::Future {
|
||||
let inner = self.inner.clone();
|
||||
|
||||
match request.uri().path() {
|
||||
"/helloworld.Greeter/SayHello" => {
|
||||
use tonic::_codegen::*;
|
||||
use tonic::*;
|
||||
|
||||
pub struct SayHello(pub std::sync::Arc<#s>);
|
||||
|
||||
impl tonic::server::UnaryService<HelloRequest> for SayHello {
|
||||
type Response = HelloReply;
|
||||
type Future = Pin<Box<dyn Future<Output = Result<Response<Self::Response>, Status>> + Send + 'static>>;
|
||||
|
||||
fn call(&mut self, request: Request<HelloRequest>) -> Self::Future {
|
||||
let inner = self.0.clone();
|
||||
let fut = async move {
|
||||
inner.#m_ident(request).await
|
||||
};
|
||||
Box::pin(fut)
|
||||
}
|
||||
}
|
||||
|
||||
let inner = self.inner.clone();
|
||||
|
||||
|
||||
let fut = async move {
|
||||
let codec = tonic::codec::UnitCodec::default();
|
||||
let method = SayHello(inner);
|
||||
let codec = tonic::codec::ProstCodec::new();
|
||||
let mut grpc = tonic::server::Grpc::new(codec);
|
||||
|
||||
let response = match grpc.unary_request(request).await {
|
||||
Ok(request) => inner.#m_ident(request).await,
|
||||
Err(status) => Err(status),
|
||||
};
|
||||
|
||||
let response = grpc.unary_response(response.unwrap())
|
||||
.await
|
||||
.map(|b| tonic::body::BoxAsyncBody::new(b));
|
||||
// .map(|b| tonic::body::BoxBody::map(b))
|
||||
|
||||
Ok(response)
|
||||
let res = grpc.unary(method, request).await;
|
||||
Ok(res)
|
||||
};
|
||||
|
||||
|
||||
Box::pin(fut)
|
||||
},
|
||||
|
||||
"helloworld.Greeter/SayHelloStream" => {
|
||||
unimplemented!()
|
||||
},
|
||||
_ => unimplemented!("use grpc unimplemented")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
original.extend(TokenStream::from(ts));
|
||||
|
||||
@@ -1,52 +0,0 @@
|
||||
#![feature(async_await)]
|
||||
|
||||
use futures::Stream;
|
||||
use std::time::Duration;
|
||||
use tokio::timer::Delay;
|
||||
use tonic::{Request, Response, Status};
|
||||
|
||||
// #[derive(Debug)]
|
||||
// struct HelloRequest;
|
||||
// #[derive(Debug)]
|
||||
// struct HelloResponse;
|
||||
|
||||
#[derive(Default, Clone)]
|
||||
pub struct MyGreeter {
|
||||
data: String,
|
||||
}
|
||||
|
||||
#[tonic::server(service = "proto/helloworld.proto")]
|
||||
impl MyGreeter {
|
||||
pub async fn say_hello(&self, request: Request<()>) -> Result<Response<()>, Status> {
|
||||
println!("Got a request: {:?}", request);
|
||||
|
||||
let string = &self.data;
|
||||
|
||||
let when = tokio::clock::now() + Duration::from_millis(100);
|
||||
Delay::new(when).await;
|
||||
|
||||
println!("My data: {:?}", string);
|
||||
|
||||
Delay::new(when).await;
|
||||
|
||||
Ok(Response::new(()))
|
||||
}
|
||||
|
||||
// pub async fn streaming(&self, request: Request<impl Stream>) -> Result<Response<impl Stream>, Status> {
|
||||
// unimplemented!()
|
||||
// }
|
||||
|
||||
// pub async fn server_stream(&self, request: Request<()>) -> Result<impl Stream, Status> {
|
||||
// unimplemented!()
|
||||
// }
|
||||
|
||||
// pub async fn client_stream(&self, request: Request<impl Stream>) -> Result<(), Status> {
|
||||
// unimplemented!()
|
||||
// }
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn grpc() {
|
||||
let svc = MyGreeter::default();
|
||||
let mut _server = GrpcServer::from(svc);
|
||||
}
|
||||
Reference in New Issue
Block a user