Skip to content
This repository has been archived by the owner on Apr 16, 2020. It is now read-only.

How to write codes in the server when the proto file has more than one services ? #200

Open
najcit opened this issue Aug 21, 2019 · 3 comments

Comments

@najcit
Copy link

najcit commented Aug 21, 2019

the content of the proto file:

syntax = "proto3";
package helloworld;

// The greeting service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {}
}

// The request message containing the user's name.
message HelloRequest {
string name = 1;
}

// The response message containing the greetings
message HelloReply {
string message = 1;
}
service Talk {
rpc TalkSomething (TalkRequest) returns (TalkReply) {}
}

message TalkRequest {
string name = 1;
}

message TalkReply {
string message = 1;
}

the codes of the server.rs file:
let new_service = server::GreeterServer::new(Greet);

let mut server = Server::new(new_service);

let http = Http::new().http2_only(true).clone();

let addr = "[::1]:50051".parse().unwrap();
let bind = TcpListener::bind(&addr).expect("bind");

let serve = bind
    .incoming()
    .for_each(move |sock| {
        if let Err(e) = sock.set_nodelay(true) {
            return Err(e);
        }

        let serve = server.serve_with(sock, http.clone());
        tokio::spawn(serve.map_err(|e| error!("hyper error: {:?}", e)));

        Ok(())
    })
    .map_err(|e| eprintln!("accept error: {}", e));

tokio::run(serve)

how to modify these code ?
thank you!!!

@davidvartan
Copy link

davidvartan commented Aug 21, 2019

This looks to be unimplemented at the moment (see: #2) However, it's easy enough to use tokio to ::spawn two services on different ports for the time being.

@LucioFranco
Copy link
Member

What @davidvartan suggested works, there are ways you can manually build a router to serve both on the same socket. This would require you to detect the incoming path. Mostly likely need to write some custom services like so https://github.com/tower-rs/tower-hyper/blob/master/examples/server.rs#L41. This is roughly what tower-grpc-build does but you can wrap it. Then dispatch the call based on the incoming path.

@najcit
Copy link
Author

najcit commented Aug 22, 2019

ok, I will try it in the next day by using your idea.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants