Rust service

This commit is contained in:
Eden Kirin
2023-03-19 22:27:38 +01:00
parent 1fa1b10699
commit a99b5d0014
7 changed files with 1150 additions and 6 deletions

View File

@ -105,9 +105,17 @@ go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.2
### Rust services
Update `rustc`
```sh
rustup update
```
#### [Tonic](https://github.com/hyperium/tonic)
Tonic is a gRPC over HTTP/2 implementation focused on high performance, interoperability, and flexibility. This library was created to have first class support of async/await and to act as a core building block for production systems written in Rust.
[Hello world tutorial](https://github.com/hyperium/tonic/blob/master/examples/helloworld-tutorial.md)
[Rust and gRPC: A complete guide](https://blog.logrocket.com/rust-and-grpc-a-complete-guide/)

View File

@ -1,4 +1,5 @@
syntax = "proto3";
package serve_currenttime;
service ServeCurrentTime {
rpc GetCurrentTime(GetCurrentTimeRequest) returns (GetCurrentTimeResponse);

File diff suppressed because it is too large Load Diff

View File

@ -5,8 +5,14 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
[[bin]]
name = "server"
path = "src/server.rs"
name = "serve_currenttime"
path = "src/main.rs"
[dependencies]
tonic = "0.8"
prost = "0.11"
tokio = { version = "1.19.2", features = ["macros", "rt-multi-thread"] }
[build-dependencies]
tonic-build = "0.8"

View File

@ -0,0 +1,2 @@
run:
cargo run --bin serve_currenttime

View File

@ -0,0 +1,4 @@
fn main() -> Result<(), Box<dyn std::error::Error>> {
tonic_build::compile_protos("../protos/serve_currenttime.proto")?;
Ok(())
}

View File

@ -1,3 +1,61 @@
fn main() {
println!("Hello, world!");
use serve_currenttime::{
serve_current_time_server::{ServeCurrentTime, ServeCurrentTimeServer},
GetCurrentTimeRequest, GetCurrentTimeResponse,
};
// use serve_currenttime::{
// serve_currenttime_server::{ServeCurrentTime, ServeCurrentTimeServer},
// GetCurrentTimeRequest, GetCurrentTimeResponse,
// };
use tonic::{transport::Server, Request, Response, Status};
pub mod serve_currenttime {
tonic::include_proto!("serve_currenttime");
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let address = "[::1]:8080".parse().unwrap();
let voting_service = ServeCurrentTimeService::default();
Server::builder()
.add_service(ServeCurrentTimeServer::new(voting_service))
.serve(address)
.await?;
Ok(())
}
#[derive(Debug, Default)]
pub struct ServeCurrentTimeService {}
#[tonic::async_trait]
impl ServeCurrentTime for ServeCurrentTimeService {
async fn get_current_time(
&self,
request: Request<GetCurrentTimeRequest>,
) -> Result<Response<GetCurrentTimeResponse>, Status> {
let r = request.into_inner();
Ok(Response::new(GetCurrentTimeResponse {
formatted_time: "xxx".to_string(),
hours: 11,
minutes: 22,
seconds: 33,
milliseconds: 44,
}))
// match r.vote {
// 0 => Ok(Response::new(serve_currenttime::GetCurrentTimeResponse {
// confirmation: { format!("Happy to confirm that you upvoted for {}", r.url) },
// })),
// 1 => Ok(Response::new(serve_currenttime::GetCurrentTimeResponse {
// confirmation: { format!("Confirmation that you downvoted for {}", r.url) },
// })),
// _ => Err(Status::new(
// tonic::Code::OutOfRange,
// "Invalid vote provided",
// )),
// }
}
}