gRPC Controllers
In Sword, a gRPC controller is a struct that implements a trait generated by tonic from a .proto file. This trait defines the gRPC methods the controller must implement.
Defining a Controller
rust
use sword::grpc::*;
use sword::prelude::*;
#[controller(kind = Controller::Grpc, service = UserGrpcServiceServer)]
pub struct UsersController;
#[sword::grpc::async_trait]
impl UserGrpcService for UsersController {
async fn list_users(
&self,
req: Request<ListUsersRequest>,
) -> GrpcResult<ListUsersReply> {
tracing::info!("ListUsers gRPC method called");
Ok(Response::new(ListUsersReply { users: vec![] }))
}
}The UserGrpcService trait is generated from a .proto file like the following:
proto
syntax = "proto3";
package users;
service UserGrpcService {
rpc ListUsers (ListUsersRequest) returns (ListUsersReply);
}
message ListUsersRequest {}
message ListUsersReply {
repeated UserItem users = 1;
}
message UserItem {
string id = 1;
string username = 2;
}
