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

Return Result when creating DaprHttpServer #253

Merged
merged 2 commits into from
Dec 10, 2024
Merged
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
39 changes: 32 additions & 7 deletions dapr/src/server/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ pub struct DaprHttpServer {

impl DaprHttpServer {
/// Creates a new instance of the Dapr HTTP server with default options.
///
/// # Panics
///
/// This function panics if the Dapr Sidecar cannot be reached!
/// For a non-panicking version that allows you to handle any errors yourself, see:
/// [DaprHttpServer::try_new_with_dapr_port]
pub async fn new() -> Self {
let dapr_port: u16 = std::env::var("DAPR_GRPC_PORT")
.unwrap_or("3501".into())
Expand All @@ -92,19 +98,38 @@ impl DaprHttpServer {
Self::with_dapr_port(dapr_port).await
}

/// Creates a new instance of the Dapr HTTP server that connects to the Dapr sidecar on the
/// given dapr_port.
///
/// # Panics
///
/// This function panics if the Dapr Sidecar cannot be reached!
/// For a non-panicking version that allows you to handle any errors yourself, see:
/// [DaprHttpServer::try_new_with_dapr_port]
pub async fn with_dapr_port(dapr_port: u16) -> Self {
let dapr_addr = format!("https://127.0.0.1:{}", dapr_port);

let cc = match TonicClient::connect(dapr_addr).await {
match Self::try_new_with_dapr_port(dapr_port).await {
Ok(c) => c,
Err(err) => panic!("failed to connect to dapr: {}", err),
};
}
}

/// Creates a new instance of the Dapr HTTP server that connects to the Dapr sidecar on the
/// given dapr_port.
///
/// In contrast to the other functions that create a DaprHttpServer, this function does
/// not panic, but instead returns a Result.
pub async fn try_new_with_dapr_port(
dapr_port: u16,
) -> Result<Self, Box<dyn std::error::Error>> {
let dapr_addr = format!("https://127.0.0.1:{}", dapr_port);

let cc = TonicClient::connect(dapr_addr).await?;
let rt = ActorRuntime::new(cc);

DaprHttpServer {
Ok(DaprHttpServer {
actor_runtime: Arc::new(rt),
shutdown_signal: None,
}
})
}

pub fn with_graceful_shutdown<F>(self, signal: F) -> Self
Expand Down Expand Up @@ -138,7 +163,7 @@ impl DaprHttpServer {
.unwrap_or(8080);

let address = format!("127.0.0.1:{}", port.unwrap_or(default_port));
let listener = TcpListener::bind(address).await.unwrap();
let listener = TcpListener::bind(address).await?;

let server = axum::serve(listener, app.into_make_service());

Expand Down
Loading