build.rs
+1
-1
diff --git a/build.rs b/build.rs
index 3100f02..be4e0cb 100644
@@ -1,4 +1,4 @@
fn main() -> Result<(), Box<dyn std::error::Error>> {
tonic_prost_build::compile_protos("proto/helloworld.proto")?;
tonic_prost_build::compile_protos("proto/receipt_printer.proto")?;
Ok(())
}
proto/helloworld.proto
+0
-18
diff --git a/proto/helloworld.proto b/proto/helloworld.proto
deleted file mode 100644
index 6be2196..0000000
@@ -1,18 +0,0 @@
syntax = "proto3";
package helloworld;
service Greeter {
// Our SayHello rpc accepts HelloRequests and returns HelloReplies
rpc SayHello (HelloRequest) returns (HelloReply);
}
message HelloRequest {
// Request message contains the name to be greeted
string name = 1;
}
message HelloReply {
// Reply contains the greeting message
string message = 1;
}
proto/receipt_printer.proto
+20
-0
diff --git a/proto/receipt_printer.proto b/proto/receipt_printer.proto
new file mode 100644
index 0000000..05e4c05
@@ -0,0 +1,20 @@
syntax = "proto3";
package receipt_printer;
service ReceiptPrinter {
rpc Print(PrintRequest) returns (PrintResponse);
}
message PrintRequest {
oneof content {
TypstContent typst = 1;
}
}
message TypstContent {
optional string content = 1;
}
message PrintResponse {
}
src/main.rs
+14
-17
diff --git a/src/main.rs b/src/main.rs
index f0638ff..71ac8ea 100644
@@ -1,19 +1,17 @@
use tonic::{transport::Server, Request, Response, Status};
mod proto;
use hello_world::greeter_server::{Greeter, GreeterServer};
use hello_world::{HelloReply, HelloRequest};
use tonic::{Request, Response, Status, transport::Server};
pub mod hello_world {
tonic::include_proto!("helloworld"); // The string specified here must match the proto package name
}
use proto::receipt_printer_server::{ReceiptPrinter, ReceiptPrinterServer};
use proto::{PrintRequest, PrintResponse};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let addr = "[::1]:50051".parse()?;
let greeter = MyGreeter::default();
let receipt_printer_service = ReceiptPrinterService::default();
Server::builder()
.add_service(GreeterServer::new(greeter))
.add_service(ReceiptPrinterServer::new(receipt_printer_service))
.serve(addr)
.await?;
@@ -21,20 +19,19 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
}
#[derive(Debug, Default)]
pub struct MyGreeter {}
pub struct ReceiptPrinterService {}
#[tonic::async_trait]
impl Greeter for MyGreeter {
async fn say_hello(
impl ReceiptPrinter for ReceiptPrinterService {
async fn print(
&self,
request: Request<HelloRequest>, // Accept request of type HelloRequest
) -> Result<Response<HelloReply>, Status> { // Return an instance of type HelloReply
request: Request<PrintRequest>, // Accept request of type PrintRequest
) -> Result<Response<PrintResponse>, Status> {
// Return an instance of type PrintResponse
println!("Got a request: {:?}", request);
let reply = HelloReply {
message: format!("Hello {}!", request.into_inner().name), // We must use .into_inner() as the fields of gRPC requests and responses are private
};
let reply = PrintResponse {};
Ok(Response::new(reply)) // Send back our formatted greeting
Ok(Response::new(reply))
}
}
src/proto.rs
+1
-0
diff --git a/src/proto.rs b/src/proto.rs
new file mode 100644
index 0000000..a30c3ee
@@ -0,0 +1 @@
tonic::include_proto!("receipt_printer");