grpc / grpc/grpc-rust

Framework Layer on top of Tonic

Open
#1,406 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
12.5k
Forks
1.3k
Avg merge
4d 7h
Merged PRs (30d)
24

Description

## Feature Request

Following this issue: https://github.com/hyperium/tonic/issues/886, i came with this code:

```rust
use hyper::{Body, Request, Response};
use std::convert::Infallible;
use tonic::body::BoxBody;
use tonic::transport::NamedService;
use tonic::transport::Server as TonicServer;
use tower::Service;

pub struct FRPCServer
where
S: Service, Response = Response, Error = Infallible>
+ NamedService
+ Clone
+ Send
+ 'static,
S::Future: Send + 'static,
{
grpc_services: Vec>,
grpc_reflection_services: Vec<&'static [u8]>,
}

impl FRPCServer
where
S: Service, Response = Response, Error = Infallible>
+ NamedService
+ Clone
+ Send
+ 'static,
S::Future: Send + 'static,
{
pub fn new() -> Self {
FRPCServer {
// grpc_server: TonicServer::builder(),
grpc_services: Vec::new(),
grpc_reflection_services: Vec::new(),
}
}

pub fn add_service(&mut self, service: S, reflection: Option<&'static [u8]>) -> &mut Self {
self.grpc_services.push(Box::new(service));

if reflection.is_some() {
self.grpc_reflection_services.push(reflection.unwrap());
}

self
}

pub async fn listen(self) -> Result<(), Box> {
let addr = "127.0.0.1:5051".parse().unwrap();

let mut server = TonicServer::builder();

let mut services = self.grpc_services;
let first_service = services.remove(0);

let mut router = server.add_service(*first_service.clone());
for service in services {
router = router.add_service(*service.clone());
}

// TODO: if enabled
let mut reflection_service = tonic_reflection::server::Builder::configure();
for reflection in self.grpc_reflection_services {
reflection_service =
reflection_service.register_encoded_file_descriptor_set(reflection.clone())
}

let reflection_service_build = reflection_service.build().unwrap();

router
.add_service(reflection_service_build)
.serve(addr)
.await?;

Ok(())
}
}
```

But i am stuck with the type of S when i want to use it like:
```rust
server.add_service(account_service, None);
server.add_service(notification_service, None);
```

I found an `mismatched types expected struct ` error because `notification_service` is not `account_service`.
It will be really cool if someone can help me with that.

thanks guys.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.