1 use tonic::{transport::Server, Request, Response, Status};
2
3 pub mod hello_world {
4 tonic::include_proto!("helloworld");
5 }
6
7 use hello_world::{
8 greeter_server::{Greeter, GreeterServer},
9 HelloReply, HelloRequest,
10 };
11
12 #[derive(Debug, Default)]
13 pub struct MyGreeter {}
14
15 #[tonic::async_trait]
16 impl Greeter for MyGreeter {
17 #[tracing::instrument]
say_hello( &self, request: Request<HelloRequest>, ) -> Result<Response<HelloReply>, Status>18 async fn say_hello(
19 &self,
20 request: Request<HelloRequest>,
21 ) -> Result<Response<HelloReply>, Status> {
22 tracing::info!("received request");
23
24 let reply = hello_world::HelloReply {
25 message: format!("Hello {}!", request.into_inner().name),
26 };
27
28 tracing::debug!("sending response");
29
30 Ok(Response::new(reply))
31 }
32 }
33
34 #[tokio::main]
main() -> Result<(), Box<dyn std::error::Error>>35 async fn main() -> Result<(), Box<dyn std::error::Error>> {
36 tracing_subscriber::fmt()
37 .with_max_level(tracing::Level::DEBUG)
38 .init();
39
40 let addr = "[::1]:50051".parse().unwrap();
41 let greeter = MyGreeter::default();
42
43 tracing::info!(message = "Starting server.", %addr);
44
45 Server::builder()
46 .trace_fn(|_| tracing::info_span!("helloworld_server"))
47 .add_service(GreeterServer::new(greeter))
48 .serve(addr)
49 .await?;
50
51 Ok(())
52 }
53