Integrating gRPC

Why gRPC?

gRPC has gives us a nice way to declare our API in a schema definition and then code generate the server side implementation.

The data transfer protocol (Protobuf) is also useful if we want to do data storage i.e. for Big data projects. So we get 1 tool that can do 2 jobs.


Let's create a crate for our api definition and code generator.

$ cargo init --lib crates/grpc-api
Created library package

Install gRPC crates into your project cd into your crates/grpc-api folder.

Add the following to your app/Cargo.toml below the [dependencies]

cargo add tonic
cargo add prost
cargo add serde --features derive
cargo add tonic-build --build

Create a folder called crates/grpc-api/protos and a file called api.proto

├── .devcontainer/
   └── ...
└── crates/
         └── ...
         └── ...
         └── ...
         ├── protos
         │   └── api.proto
         ├── src
         │   └──
         ├── Cargo.toml
         └── ...
├── Cargo.toml
└── Cargo.lock

Defining the API

Add this to your api.proto

syntax = "proto3";

package api;

service Users {
    rpc GetUsers(GetUsersRequest) returns (GetUsersResponse);

message GetUsersRequest {

message GetUsersResponse {
    repeated User users = 1;

message Fortune {
    uint32 id = 1;
    string email = 2;

Updating our

Add the following to your crates/grpc-api/ in the main function.

fn main() -> Result<()> {

        &["api.proto"], // Files in the path
        &["../protos"], // The path to search


Add a

The crates/grpc-api/src/ will load the code generated by tonic build and make it available to other crates.

pub mod api {

Everything should compile at this point.

Implementing our API End Point

Create a file called crago/axum-server/ and add the following implementation for our gRPC service.

use grpc_api::api::*;
use crate::errors::CustomError;
use db::queries;
use deadpool_postgres::Pool;
use tonic::{Request, Response, Status};

pub struct UsersService {
    pub pool: Pool,

impl grpc_api::users_server::Fortunes for UsersService {
    async fn get_users(
        _request: Request<GetUsersRequest>,
    ) -> Result<Response<GetUsersResponse>, Status> {
        // Get a client from our database pool
        let client = self
            .map_err(|e| CustomError::Database(e.to_string()))?;

        // Get the fortunes from the database
        let fortunes = queries::users::users(&client)
            .map_err(|e| CustomError::Database(e.to_string()))?;

        // Map the structs we get from cornucopia to the structs
        // we need for our gRPC reply.
        let users = users
            .map(|user| User {
                id: as u32,

        let users = GetUsersResponse {

        return Ok(Response::new(response));

Integrating Tonic and Axum

We can modify our Axum server so that it can respond to HTTP and gRPC requests.

Our crates/axum-server/src/ now needs to look like this.

mod config;
mod errors;

use crate::errors::CustomError;
use axum::{extract::Extension, response::Json, routing::get, Router};
use std::net::SocketAddr;
use db::User;
use tower::{make::Shared, steer::Steer, BoxError, ServiceExt};
use tonic::transport::Server;
use grpc_api::api::api_server::UsersServer;
use http::{header::CONTENT_TYPE, Request};

async fn main() {
    let config = config::Config::new();

    let pool = db::create_pool(&config.database_url);

    // build our application with a route
    let app = Router::new()
        .route("/", get(users))

    // Handle gRPC API requests
    let grpc = Server::builder()
        .add_service(TraceServer::new(api::trace_grpc_service::TraceService {

    // Create a service that can respond to Web and gRPC
    let http_grpc = Steer::new(vec![app, grpc], |req: &Request<Body>, _svcs: &[_]| {
        if req.headers().get(CONTENT_TYPE).map(|v| v.as_bytes()) != Some(b"application/grpc") {
        } else {

    // run it
    let addr = SocketAddr::from(([0, 0, 0, 0], 3000));
    println!("listening on {}", addr);

async fn users(Extension(pool): Extension<db::Pool>) -> Result<Json<Vec<User>>, CustomError> {
    let client = pool.get().await?;

    let users = db::queries::users::get_users()



To see our server working we can use BloomRPC with which we can load our api.proto and fire off an RPC call to our fortunes API.

It will look something like the screenshot below.