WLOVZ5OJEFQG57LMUNNIFHX6E7J5OZ6L2HEKY3N7KNNSLWMP2I4AC fn main() {println!("Hello, world!");
use tonic::{transport::Server, Request, Response, Status};use bookstore::bookstore_server::{Bookstore, BookstoreServer};use bookstore::{GetBookRequest, GetBookResponse};mod bookstore {include!("bookstore.rs");// Add thispub(crate) const FILE_DESCRIPTOR_SET: &[u8] =tonic::include_file_descriptor_set!("greeter_descriptor");}#[derive(Default)]pub struct BookStoreImpl {}#[tonic::async_trait]impl Bookstore for BookStoreImpl {async fn get_book(&self,request: Request<GetBookRequest>,) -> Result<Response<GetBookResponse>, Status> {println!("Request from {:?}", request.remote_addr());let response = GetBookResponse {id: request.into_inner().id,author: "Peter".to_owned(),name: "Zero to One".to_owned(),year: 2014,};Ok(Response::new(response))}
#[tokio::main]async fn main() -> Result<(), Box<dyn std::error::Error>> {let addr = "[::1]:50051".parse().unwrap();let bookstore = BookStoreImpl::default();// Add thislet reflection_service = tonic_reflection::server::Builder::configure().register_encoded_file_descriptor_set(bookstore::FILE_DESCRIPTOR_SET).build().unwrap();println!("Bookstore server listening on {}", addr);Server::builder().add_service(BookstoreServer::new(bookstore)).add_service(reflection_service) // Add this.serve(addr).await?;Ok(())}
syntax = "proto3";package bookstore;// The book store service definition.service Bookstore {// Retrieve a bookrpc GetBook(GetBookRequest) returns (GetBookResponse) {}}// The request with a id of the bookmessage GetBookRequest {string id = 1;}// The response details of a bookmessage GetBookResponse {string id = 1;string name = 2;string author = 3;int32 year = 4;}
use std::{env, path::PathBuf};fn main() {let proto_file = "./proto/rfkpos.proto";let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());tonic_build::configure().build_server(true).file_descriptor_set_path(out_dir.join("greeter_descriptor.bin")).out_dir("./src").compile(&[proto_file], &["."]).unwrap_or_else(|e| panic!("protobuf compile error: {:?}", e));println!("cargo:rerun-if-changed={}", proto_file);}
tonic = "0.8.2"tonic-reflection = "0.5.0"tokio = { version = "1.21.2", features = ["macros", "rt-multi-thread"] }prost = "0.11.0"[build-dependencies]tonic-build = "0.8.2"