From 6154bf2c0b3f17caf76f45c1d60bcf59464ccf27 Mon Sep 17 00:00:00 2001 From: projectmoon Date: Sat, 5 Jun 2021 15:29:42 +0000 Subject: [PATCH] Move address of grpc to rpc crate. But why static lifetime? --- api/Cargo.toml | 4 ---- api/src/main.rs | 21 ++------------------- rpc/src/lib.rs | 19 +++++++++++++++++++ 3 files changed, 21 insertions(+), 23 deletions(-) diff --git a/api/Cargo.toml b/api/Cargo.toml index 5d372c2..716883e 100644 --- a/api/Cargo.toml +++ b/api/Cargo.toml @@ -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" ] diff --git a/api/src/main.rs b/api/src/main.rs index 5ac13a6..2824b92 100644 --- a/api/src/main.rs +++ b/api/src/main.rs @@ -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, Box> { - 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> { 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 { diff --git a/rpc/src/lib.rs b/rpc/src/lib.rs index 98c5bad..d2c7b83 100644 --- a/rpc/src/lib.rs +++ b/rpc/src/lib.rs @@ -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, Box> { + 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) +}