5 个不稳定版本
0.2.0 | 2024年2月10日 |
---|---|
0.1.0 | 2023年1月13日 |
0.0.3 | 2022年8月31日 |
0.0.2 | 2022年6月18日 |
0.0.1 | 2022年6月18日 |
#1601 在 Web编程
89 每月下载量
17KB
197 行
axum_tonic
一个用于使用Tonic与Axum配合的微小库。
此库使得使用不同类型的中间件与不同的tonic服务变得简单。
推荐的使用方法是创建两个独立的根路由器,一个用于grpc,一个用于rest。然后两者可以在根目录下合并,并转换为make服务。
请参阅Axum或Tonic的文档以获取有关各自框架的更多信息。
示例
/// A middleware that does nothing, but just passes on the request.
async fn do_nothing_middleware<B>(req: Request<B>, next: Next<B>) -> Result<Response, GrpcStatus> {
Ok(next.run(req).await)
}
/// A middleware that cancels the request with a grpc status-code
async fn cancel_request_middleware<B>(_req: Request<B>, _next: Next<B>) -> Result<Response, GrpcStatus> {
Err(tonic::Status::cancelled("Canceled").into())
}
#[tokio::main]
async fn main() {
// Spawn the Server
tokio::task::spawn(async move {
// The first grpc-service has middleware that accepts the request.
let grpc_router1 = Router::new()
.nest_tonic(Test1Server::new(Test1Service))
.layer(from_fn(do_nothing_middleware));
// The second grpc-service instead cancels the request
let grpc_router2 = Router::new()
.nest_tonic(Test2Server::new(Test2Service))
.layer(from_fn(cancel_request_middleware));
// Merge both routers into one.
let grpc_router = grpc_router1.merge(grpc_router2);
// This is the normal rest-router, to which all normal requests are routed
let rest_router = Router::new()
.nest("/", Router::new().route("/123", get(|| async move {})))
.route("/", get(|| async move {}));
// Combine both services into one
let service = RestGrpcService::new(rest_router, grpc_router);
// And serve at 127.0.0.1:8080
axum::Server::bind(&"127.0.0.1:8080".parse().unwrap())
.serve(service.into_make_service())
.await
.unwrap();
});
tokio::time::sleep(Duration::from_millis(100)).await;
// Connect to the server with a grpc-client
let channel = Channel::from_static("http://127.0.0.1:8080")
.connect()
.await
.unwrap();
let mut client1 = Test1Client::new(channel.clone());
let mut client2 = Test2Client::new(channel);
// The first request will succeed
client1.test1(Test1Request {}).await.unwrap();
// While the second one gives a grpc Status::Canceled code.
assert_eq!(
client2.test2(Test2Request {}).await.unwrap_err().code(),
tonic::Code::Cancelled,
);
}
依赖项
~6.5–9MB
~162K SLoC