Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor!: adds a port override to the client setup #169

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,14 @@ Here's a basic example to create a client:
use dapr;

async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Get the Dapr port and create a connection
let port: u16 = std::env::var("DAPR_GRPC_PORT")?.parse()?;
let addr = format!("https://127.0.0.1:{}", port);
// Dapr GRPC Address
let addr = "https://127.0.0.1";

// Dapr GRPC Port (optional)
let port = "50001";

// Create the client
let mut client = dapr::Client::<dapr::client::TonicClient>::connect(addr).await?;
let mut client = dapr::Client::<dapr::client::TonicClient>::connect(addr, port).await?;
```

## Explore more examples
Expand Down
2 changes: 1 addition & 1 deletion examples/actors/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let addr = "https://127.0.0.1".to_string();

// Create the client
let mut client = dapr::Client::<dapr::client::TonicClient>::connect(addr).await?;
let mut client = dapr::Client::<dapr::client::TonicClient>::connect(addr, None).await?;

let data = MyRequest {
name: "test".to_string(),
Expand Down
2 changes: 1 addition & 1 deletion examples/client/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let addr = "https://127.0.0.1".to_string();

// Create the client
let mut client = dapr::Client::<dapr::client::TonicClient>::connect(addr).await?;
let mut client = dapr::Client::<dapr::client::TonicClient>::connect(addr, None).await?;

let key = String::from("hello");

Expand Down
2 changes: 1 addition & 1 deletion examples/configuration/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let addr = "https://127.0.0.1".to_string();

// Create the client
let mut client = DaprClient::connect(addr).await?;
let mut client = DaprClient::connect(addr, None).await?;

let key = String::from("hello");

Expand Down
2 changes: 1 addition & 1 deletion examples/crypto/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
sleep(std::time::Duration::new(2, 0)).await;
let addr = "https://127.0.0.1".to_string();

let mut client = dapr::Client::<dapr::client::TonicClient>::connect(addr).await?;
let mut client = dapr::Client::<dapr::client::TonicClient>::connect(addr, None).await?;

let encrypted = client
.encrypt(
Expand Down
2 changes: 1 addition & 1 deletion examples/invoke/grpc/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Set the Dapr address
let address = "https://127.0.0.1".to_string();

let mut client = DaprClient::connect(address).await?;
let mut client = DaprClient::connect(address, None).await?;

let request = HelloRequest {
name: "Test".to_string(),
Expand Down
2 changes: 1 addition & 1 deletion examples/pubsub/publisher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let addr = "https://127.0.0.1".to_string();

// Create the client
let mut client = dapr::Client::<dapr::client::TonicClient>::connect(addr).await?;
let mut client = dapr::Client::<dapr::client::TonicClient>::connect(addr, None).await?;

// name of the pubsub component
let pubsub_name = "pubsub".to_string();
Expand Down
2 changes: 1 addition & 1 deletion examples/query_state/query1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let addr = "https://127.0.0.1".to_string();

// Create the client
let mut client = dapr::Client::<dapr::client::TonicClient>::connect(addr).await?;
let mut client = dapr::Client::<dapr::client::TonicClient>::connect(addr, None).await?;

let query_condition = json!({
"filter": {
Expand Down
2 changes: 1 addition & 1 deletion examples/query_state/query2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let addr = "https://127.0.0.1".to_string();

// Create the client
let mut client = dapr::Client::<dapr::client::TonicClient>::connect(addr).await?;
let mut client = dapr::Client::<dapr::client::TonicClient>::connect(addr, None).await?;

let query_condition = json!({
"filter": {
Expand Down
2 changes: 1 addition & 1 deletion examples/secrets-bulk/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let addr = "https://127.0.0.1".to_string();

// Create the client
let mut client = dapr::Client::<dapr::client::TonicClient>::connect(addr).await?;
let mut client = dapr::Client::<dapr::client::TonicClient>::connect(addr, None).await?;

let secret_store = "localsecretstore";

Expand Down
28 changes: 25 additions & 3 deletions src/client.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use log::error;
use serde_json::Value;
use std::collections::HashMap;

Expand All @@ -21,10 +22,31 @@ impl<T: DaprInterface> Client<T> {
///
/// # Arguments
///
/// * `addr` - Address of gRPC server to connect to.
pub async fn connect(addr: String) -> Result<Self, Error> {
/// * `addr` - Address of the gRPC server to connect to.
/// * `port` - Port of the gRPC server to connect to.
pub async fn connect(addr: String, port: Option<String>) -> Result<Self, Error> {
const DEFAULT_DAPR_GRPC_PORT: u16 = 50001;

// Get the Dapr port to create a connection
let port: u16 = std::env::var("DAPR_GRPC_PORT")?.parse()?;
let port: u16 = match port {
Some(port) => {
// Handle port
let port = port.parse::<u16>().unwrap();
match port {
1..=65535 => port,
_ => {
panic!("invalid port specified: {port:?}")
}
}
}
None => match std::env::var("DAPR_GRPC_PORT")?.parse::<u16>() {
Ok(port) => port,
Err(e) => {
error!("error retrieving port from the env var DAPR_GRPC_PORT, using default: {e:?}");
DEFAULT_DAPR_GRPC_PORT
}
},
};
let address = format!("{}:{}", addr, port);

Ok(Client(T::connect(address).await?))
Expand Down
Loading