Skip to content

Commit

Permalink
feat: ultimate-grpc 添加 tower-http 中间件
Browse files Browse the repository at this point in the history
  • Loading branch information
yangjing committed Sep 11, 2024
1 parent 128d468 commit ca58a2e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ utoipa-scalar = { version = "0.2.0-beta", features = ["axum"] }
asserhttp = { version = "0.7", features = ["reqwest"] }
dotenvy = "0.15"
# -- Data Science
polars = "0.42"
polars = "0.43"
# An Excel/OpenDocument Spreadsheets reader and deserializer in pure rust
calamine = "0.25"
# -- RPC
Expand Down
1 change: 1 addition & 0 deletions crates/ultimate-grpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ ultimate = { workspace = true, features = ["tonic"] }
http = { workspace = true }
futures = { workspace = true }
tower-service = { workspace = true }
tower-http = { workspace = true }
tonic = { workspace = true }
tonic-types = { workspace = true }
tonic-web = { workspace = true, optional = true }
Expand Down
14 changes: 10 additions & 4 deletions crates/ultimate-grpc/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use futures::{Future, TryFutureExt};
use prost_types::FieldMask;
use tonic::{metadata::MetadataMap, service::RoutesBuilder, transport::Server, Status};
use tower_http::{compression::CompressionLayer, trace::TraceLayer};
use ultimate::{
configuration::model::{GrpcConf, SecurityConf},
security::{jose::JwtPayload, SecurityUtils},
Expand All @@ -18,12 +19,12 @@ where
let grpc_addr = conf.server_addr.parse()?;

#[cfg(not(feature = "tonic-web"))]
let mut b = Server::builder();

let b = Server::builder();
#[cfg(feature = "tonic-web")]
let mut b = Server::builder().accept_http1(true).layer(tonic_web::GrpcWebLayer::new());
let b = Server::builder().accept_http1(true).layer(tonic_web::GrpcWebLayer::new());

let mut routes_builder = RoutesBuilder::default();

f(&mut routes_builder);

#[cfg(feature = "tonic-reflection")]
Expand All @@ -37,7 +38,12 @@ where

// let s = router.into_service();

let serve = b.add_routes(routes_builder.routes()).serve(grpc_addr).map_err(DataError::from);
let serve = b
.layer(CompressionLayer::new())
.layer(TraceLayer::new_for_grpc())
.add_routes(routes_builder.routes())
.serve(grpc_addr)
.map_err(DataError::from);
Ok(serve)
}

Expand Down

0 comments on commit ca58a2e

Please sign in to comment.