1 //! A HelloWorld example that uses a custom codec instead of the default Prost codec.
2 //!
3 //! Generated code is the output of codegen as defined in the `examples/build.rs` file.
4 //! The generation is the one with .codec_path("crate::common::SmallBufferCodec")
5 //! The generated code assumes that a module `crate::common` exists which defines
6 //! `SmallBufferCodec`, and `SmallBufferCodec` must have a Default implementation.
7
8 use tonic::{transport::Server, Request, Response, Status};
9
10 pub mod common;
11
12 pub mod small_buf {
13 include!(concat!(env!("OUT_DIR"), "/smallbuf/helloworld.rs"));
14 }
15 use small_buf::{
16 greeter_server::{Greeter, GreeterServer},
17 HelloReply, HelloRequest,
18 };
19
20 #[derive(Default)]
21 pub struct MyGreeter {}
22
23 #[tonic::async_trait]
24 impl Greeter for MyGreeter {
say_hello( &self, request: Request<HelloRequest>, ) -> Result<Response<HelloReply>, Status>25 async fn say_hello(
26 &self,
27 request: Request<HelloRequest>,
28 ) -> Result<Response<HelloReply>, Status> {
29 println!("Got a request from {:?}", request.remote_addr());
30
31 let reply = HelloReply {
32 message: format!("Hello {}!", request.into_inner().name),
33 };
34 Ok(Response::new(reply))
35 }
36 }
37
38 #[tokio::main]
main() -> Result<(), Box<dyn std::error::Error>>39 async fn main() -> Result<(), Box<dyn std::error::Error>> {
40 let addr = "[::1]:50051".parse().unwrap();
41 let greeter = MyGreeter::default();
42
43 println!("GreeterServer listening on {}", addr);
44
45 Server::builder()
46 .add_service(GreeterServer::new(greeter))
47 .serve(addr)
48 .await?;
49
50 Ok(())
51 }
52