blockscout / actix-prost

Actix route generator for gRPC server
MIT License
5 stars 1 forks source link

actix-prost

Generate actix handles and routes with ease!

Usage

You can see some examples in tests crate

Limitations

Currently, due to lack of prost support for custom extensions, google.api.http options are not supported in proto files.

Instead, you can write .yaml api, which we can read and parse, and provide it in build.rs

Steps

Add this to Cargo.toml

[dependencies]
# we're not released yet
actix-prost = { git = "https://github.com/blockscout/actix-prost" }
actix-web = "4"
serde = { version = "1", features = ["derive"] }
async-trait = "0.1"
prost = "0.11"
tonic = "0.8"

[build-dependencies]
actix-prost-build = { git = "https://github.com/blockscout/actix-prost" }
tonic-build = "0.8"
prost-build = "0.11"

And add this to build.rs

use actix_prost_build::{ActixGenerator, GeneratorList};
use prost_build::{Config, ServiceGenerator};
use std::path::Path;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let api = "path/to/api.yaml";
    let gens = Box::new(GeneratorList::new(vec![
        // tonic generator is required, because we need it's trait
        tonic_build::configure().service_generator(),
        // actix generator
        Box::new(ActixGenerator::new(api).unwrap()),
    ]));
    let mut config = Config::new();
    config
        .service_generator(generator)
        // this is not required, but it will force protoc to check that yaml is valid
        .protoc_arg(format!("grpc_api_configuration={},output_format=yaml", api))
        // this is required
        .type_attribute(".", "#[derive(serde::Serialize, serde::Deserialize)]");
    config.compile_protos(protos, includes)?;
    Ok(())
}

What's here and what's not

✔️ = done, ⌛ = will be done soon, ❌ = not planned