build: finished following tonic tutorial

This commit is contained in:
əlemi 2022-07-10 20:44:43 +02:00
parent 65300edf90
commit 6e26282faf
No known key found for this signature in database
GPG key ID: BBCBFE5D7244634E
3 changed files with 45 additions and 8 deletions

2
.gitignore vendored
View file

@ -1 +1,3 @@
/target /target
Cargo.lock

View file

@ -1,13 +1,21 @@
use tonic::{transport::Server, Request, Response, Status};
pub mod proto_core { pub mod proto_core {
tonic::include_proto!("core"); tonic::include_proto!("core");
} }
use proto_core::session_server::{Session, SessionServer}; use proto_core::session_client::SessionClient;
use proto_core::{SessionRequest, SessionResponse}; use proto_core::SessionRequest;
pub fn main() { #[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut client = SessionClient::connect("http://[::1]:50051").await?;
let request = tonic::Request::new(SessionRequest {
session_id: 0,
});
let response = client.create(request).await?;
println!("RESPONSE={:?}", response);
Ok(())
} }

View file

@ -1,13 +1,40 @@
use tonic::{transport::Server, Request, Response, Status}; use tonic::{transport::Server, Request, Response, Status};
use proto_core::session_server::{Session, SessionServer};
use proto_core::{SessionRequest, SessionResponse};
pub mod proto_core { pub mod proto_core {
tonic::include_proto!("core"); tonic::include_proto!("core");
} }
use proto_core::session_server::{Session, SessionServer}; #[derive(Debug, Default)]
use proto_core::{SessionRequest, SessionResponse}; pub struct TestSession {}
pub fn main() { #[tonic::async_trait]
impl Session for TestSession {
async fn create(
&self,
request: Request<SessionRequest>,
) -> Result<Response<SessionResponse>, Status> {
println!("Got a request: {:?}", request);
let reply = proto_core::SessionResponse {
session_id: request.into_inner().session_id,
};
Ok(Response::new(reply))
}
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let addr = "[::1]:50051".parse()?;
let greeter = TestSession::default();
Server::builder()
.add_service(SessionServer::new(greeter))
.serve(addr)
.await?;
Ok(())
} }