feat: add protobuf
Some checks failed
continuous-integration/drone/push Build is failing

Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
Kasper Juul Hermansen 2024-02-11 13:31:45 +01:00
parent 39d4109c36
commit ae1f91b2b1
Signed by: kjuulh
GPG Key ID: 9AA7BC13CE474394
5 changed files with 711 additions and 69 deletions

680
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -11,3 +11,8 @@ tracing-subscriber.workspace = true
clap.workspace = true clap.workspace = true
dotenv.workspace = true dotenv.workspace = true
axum.workspace = true axum.workspace = true
prost = "0.12.3"
tonic = "0.11.0"
[build-dependencies]
tonic-build = "0.11.0"

View File

@ -0,0 +1,26 @@
use std::path::PathBuf;
fn main() {
let proto_dir = PathBuf::from(std::env!("CARGO_MANIFEST_DIR"))
.join("schemas")
.join("proto");
let proto_files = proto_dir
.read_dir()
.unwrap()
.flatten()
.map(|e| (e.path(), e.metadata()))
.filter_map(|(path, file_type)| {
file_type
.ok()
.filter(|file_type| file_type.is_file())
.map(|_| path)
})
.collect::<Vec<_>>();
if proto_files.is_empty() {
panic!("failed to find any proto files in {}", proto_dir.display());
}
tonic_build::compile_protos(proto_files.first().unwrap()).unwrap();
}

View File

@ -0,0 +1,18 @@
syntax = "proto3";
package flux_releaser;
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {}
}
// The request message containing the user's name.
message HelloRequest {
string name = 1;
}
// The response message containing the greetings.
message HelloReply {
string message = 1;
}

View File

@ -3,6 +3,9 @@ use std::net::SocketAddr;
use axum::routing::get; use axum::routing::get;
use axum::Router; use axum::Router;
use clap::{Parser, Subcommand}; use clap::{Parser, Subcommand};
use grpc::{greeter_server, HelloReply, HelloRequest};
use tokio::net::TcpListener;
use tonic::transport::Server;
#[derive(Parser)] #[derive(Parser)]
#[command(author, version, about, long_about = None, subcommand_required = true)] #[command(author, version, about, long_about = None, subcommand_required = true)]
@ -19,6 +22,42 @@ enum Commands {
}, },
} }
async fn axum_serve(listener: TcpListener, app: Router) -> anyhow::Result<()> {
axum::serve(listener, app.into_make_service()).await?;
Ok(())
}
mod grpc {
tonic::include_proto!("flux_releaser");
}
#[derive(Debug, Default)]
pub struct FluxReleaserGrpc {}
#[tonic::async_trait]
impl greeter_server::Greeter for FluxReleaserGrpc {
async fn say_hello(
&self,
request: tonic::Request<HelloRequest>,
) -> std::result::Result<tonic::Response<HelloReply>, tonic::Status> {
Ok(tonic::Response::new(HelloReply {
message: "something".into(),
}))
}
}
async fn tonic_serve() -> anyhow::Result<()> {
tracing::info!("grpc listening on: :7900");
Server::builder()
.add_service(greeter_server::GreeterServer::new(
FluxReleaserGrpc::default(),
))
.serve("[::1]:7900".parse()?)
.await?;
Ok(())
}
#[tokio::main] #[tokio::main]
async fn main() -> anyhow::Result<()> { async fn main() -> anyhow::Result<()> {
dotenv::dotenv().ok(); dotenv::dotenv().ok();
@ -33,9 +72,15 @@ async fn main() -> anyhow::Result<()> {
tracing::info!("listening on {}", host); tracing::info!("listening on {}", host);
let listener = tokio::net::TcpListener::bind(host).await.unwrap(); let listener = tokio::net::TcpListener::bind(host).await.unwrap();
axum::serve(listener, app.into_make_service())
.await tokio::select! {
.unwrap(); res = axum_serve(listener, app) => {
res?;
},
res = tonic_serve() => {
res?;
},
};
} }
Ok(()) Ok(())