Move address of grpc to rpc crate. But why static lifetime?

This commit is contained in:
projectmoon 2021-06-05 15:29:42 +00:00
parent 7a59e6246f
commit 6154bf2c0b
3 changed files with 21 additions and 23 deletions

View File

@ -13,7 +13,3 @@ tenebrous-rpc = { path = "../rpc" }
juniper = { git = "https://github.com/graphql-rust/juniper", branch = "master" }
juniper_rocket_async = { git = "https://github.com/graphql-rust/juniper", branch = "master" }
rocket = { git = "https://github.com/SergioBenitez/Rocket", branch = "master" }
# [dependencies.tokio]
# version = "1"
# features = [ "full" ]

View File

@ -6,26 +6,9 @@ use rocket::{response::content, Rocket, State};
use std::env;
use tenebrous_rpc::protos::dicebot::dicebot_client::DicebotClient;
use tenebrous_rpc::protos::dicebot::GetVariableRequest;
use tonic::{metadata::MetadataValue, transport::Channel as TonicChannel, Request as TonicRequest};
use tonic::{transport::Channel as TonicChannel, Request as TonicRequest};
use tracing_subscriber::filter::EnvFilter;
//grpc stuff
async fn create_client(
shared_secret: &str,
) -> Result<DicebotClient<TonicChannel>, Box<dyn std::error::Error>> {
let channel = TonicChannel::from_static("http://0.0.0.0:9090")
.connect()
.await?;
let bearer = MetadataValue::from_str(&format!("Bearer {}", shared_secret))?;
let client = DicebotClient::with_interceptor(channel, move |mut req: TonicRequest<()>| {
req.metadata_mut().insert("authorization", bearer.clone());
Ok(req)
});
Ok(client)
}
//api stuff
#[derive(GraphQLInputObject)]
struct UserVariableArgument {
@ -141,7 +124,7 @@ pub async fn main() -> Result<(), Box<dyn std::error::Error>> {
tracing_subscriber::fmt().with_env_filter(filter).init();
log::info!("Setting up gRPC connection");
let client = create_client("abc123").await?;
let client = tenebrous_rpc::create_client("http://localhost:9090", "abc123").await?;
log::info!("Listening on 127.0.0.1:8080");
let context = Context {

View File

@ -7,3 +7,22 @@ pub mod protos {
tonic::include_proto!("dicebot");
}
}
use protos::dicebot::dicebot_client::DicebotClient;
use tonic::{metadata::MetadataValue, transport::Channel as TonicChannel, Request as TonicRequest};
#[cfg(feature = "default")]
pub async fn create_client(
address: &'static str,
shared_secret: &str,
) -> Result<DicebotClient<TonicChannel>, Box<dyn std::error::Error>> {
let channel = TonicChannel::from_shared(address)?.connect().await?;
let bearer = MetadataValue::from_str(&format!("Bearer {}", shared_secret))?;
let client = DicebotClient::with_interceptor(channel, move |mut req: TonicRequest<()>| {
req.metadata_mut().insert("authorization", bearer.clone());
Ok(req)
});
Ok(client)
}