Skip to content

Commit

Permalink
fix: working tonic example
Browse files Browse the repository at this point in the history
  • Loading branch information
0xAlcibiades committed Dec 22, 2024
1 parent 3c3a004 commit 4c45624
Show file tree
Hide file tree
Showing 4 changed files with 442 additions and 451 deletions.
24 changes: 6 additions & 18 deletions examples/tonic.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
use std::net::SocketAddr;
use std::sync::Arc;

use bytes::Bytes;
use http::Request;
use http_body_util::{Full};
use hyper::body::Incoming;
use hyper::Response;
use hyper_util::rt::TokioExecutor;
use hyper_util::server::conn::auto::Builder as HttpConnectionBuilder;
use hyper_util::service::TowerToHyperService;
use rustls::ServerConfig;
use tokio::net::TcpListener;
use tokio_stream::wrappers::TcpListenerStream;
use tonic::server::NamedService;
use tonic::service::AxumRouter;
use tonic::transport::Server;
use tower::ServiceExt;
use tracing::{info, Level};

use postel::{load_certs, load_private_key, serve_http_with_shutdown};
Expand All @@ -30,9 +23,7 @@ impl NamedService for GreeterService {
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
// Initialize logging
tracing_subscriber::fmt()
.with_max_level(Level::INFO)
.init();
tracing_subscriber::fmt().with_max_level(Level::INFO).init();

// Configure server address
let addr = SocketAddr::from(([127, 0, 0, 1], 8443));
Expand Down Expand Up @@ -72,13 +63,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let server = tokio::spawn(async move {
info!("Server starting up...");

let svc = <AxumRouter as ServiceExt<Request<Incoming>>>::map_response::<_, Response<Full<Bytes>>>(Server::builder()
let svc = Server::builder()
.add_service(health_service)
.into_service()
.into_axum_router(), |res| {
// TODO the issue here is that streams are not sync
res.map(|_| Full::new(Bytes::from("placeholder")))
});
.into_axum_router();

let hyper_svc = TowerToHyperService::new(svc);

Expand All @@ -92,8 +80,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
info!("Shutdown signal received");
}),
)
.await
.expect("Server failed unexpectedly");
.await
.expect("Server failed unexpectedly");
});

// Keep the main thread running until Ctrl+C
Expand All @@ -105,4 +93,4 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
server.await?;

Ok(())
}
}
2 changes: 1 addition & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ pub(crate) fn handle_accept_error(e: impl Into<TransportError>) -> ControlFlow<T

/// A type alias for the source of an error, which is a boxed trait object.
/// This allows for dynamic dispatch and type erasure of the original error type.
type Source = Box<dyn StdError + Send + Sync + 'static>;
type Source = crate::Error;

/// Represents errors that originate from the server.
/// This struct provides a public API for error handling.
Expand Down
Loading

0 comments on commit 4c45624

Please sign in to comment.