Rust service
This commit is contained in:
@ -105,9 +105,17 @@ go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.2
|
|||||||
|
|
||||||
### Rust services
|
### Rust services
|
||||||
|
|
||||||
|
Update `rustc`
|
||||||
|
|
||||||
|
```sh
|
||||||
|
rustup update
|
||||||
|
```
|
||||||
|
|
||||||
#### [Tonic](https://github.com/hyperium/tonic)
|
#### [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.
|
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/)
|
[Rust and gRPC: A complete guide](https://blog.logrocket.com/rust-and-grpc-a-complete-guide/)
|
||||||
|
|||||||
@ -1,4 +1,5 @@
|
|||||||
syntax = "proto3";
|
syntax = "proto3";
|
||||||
|
package serve_currenttime;
|
||||||
|
|
||||||
service ServeCurrentTime {
|
service ServeCurrentTime {
|
||||||
rpc GetCurrentTime(GetCurrentTimeRequest) returns (GetCurrentTimeResponse);
|
rpc GetCurrentTime(GetCurrentTimeRequest) returns (GetCurrentTimeResponse);
|
||||||
1065
src/serve_currenttime/Cargo.lock
generated
1065
src/serve_currenttime/Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@ -5,8 +5,14 @@ edition = "2021"
|
|||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
|
||||||
|
|
||||||
[[bin]]
|
[[bin]]
|
||||||
name = "server"
|
name = "serve_currenttime"
|
||||||
path = "src/server.rs"
|
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"
|
||||||
|
|||||||
2
src/serve_currenttime/Makefile
Normal file
2
src/serve_currenttime/Makefile
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
run:
|
||||||
|
cargo run --bin serve_currenttime
|
||||||
4
src/serve_currenttime/build.rs
Normal file
4
src/serve_currenttime/build.rs
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
tonic_build::compile_protos("../protos/serve_currenttime.proto")?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
@ -1,3 +1,61 @@
|
|||||||
fn main() {
|
use serve_currenttime::{
|
||||||
println!("Hello, world!");
|
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",
|
||||||
|
// )),
|
||||||
|
// }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user