Is there a way to add a custom prefix to an axum
route serving tonic
grpc-web
?
#1980
-
I need to use a "prefix" for grpc routes served with let mut api = Router::new().route(
"/api",
on(MethodFilter::GET | MethodFilter::POST, my_api),
);
api = api.route(
"/grpc/players.PlayerService/*endpoint", // I need the prefix "grpc" here as you can see
axum::routing::any_service(grpc_server),
); Is there a way? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 6 replies
-
I haven't tested it with a client but something like // build the grpc service
let reflection_service = tonic_reflection::server::Builder::configure()
.register_encoded_file_descriptor_set(proto::FILE_DESCRIPTOR_SET)
.build()
.unwrap();
let grpc = tonic::transport::Server::builder()
.add_service(reflection_service)
.add_service(GreeterServer::new(GrpcServiceImpl::default()))
.into_service();
// need to map the error type to `Infallible`
let grpc = HandleErrorLayer::new(|err: BoxError| async move {
tracing::error!(err, "grpc service failed");
// from https://docs.rs/tonic/latest/src/tonic/status.rs.html#100
let internal_error_code = 13;
[("grpc-status", internal_error_code)]
})
.layer(grpc);
let app = Router::new()
.nest_service("/grpc", grpc)
.route("/regular-route", get(|| async { "Hello, World!" }));
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
tracing::debug!("listening on {}", addr);
axum::Server::bind(&addr)
.serve(app.into_make_service())
.await
.unwrap(); should work. Basically just using |
Beta Was this translation helpful? Give feedback.
-
I'm using your code and it works: use crate::routes::grpc;
use axum::{error_handling::HandleErrorLayer, routing::get, BoxError};
use std::net::SocketAddr;
use tower::Layer;
pub mod routes;
pub mod grpc {
// I don't know what to return here
pub fn new_grpc_server() -> Router {
let router = tonic::transport::Server::builder()
.add_service(tonic_web::enable(HelloGreeterServer::new(
HelloServiceImpl::default(),
)))
.add_service(tonic_web::enable(WorldGreeterServer::new(
WorldServiceImpl::default(),
)));
// From https://github.com/tokio-rs/axum/discussions/1980
// let grpc = HandleErrorLayer::new(|err: BoxError| async move {
// eprintln!("{} {}", err, "grpc service failed");
// // from https://docs.rs/tonic/latest/src/tonic/status.rs.html#100
// let internal_error_code = 13;
// [("grpc-status", internal_error_code)]
// })
// .layer(server);
router
}
}
#[tokio::main]
async fn main() {
let mut api = axum::Router::new().route("/api", get(|| async { "Hello, World!" }));
let grpc_server = grpc::new_grpc_server();
// Is it possible to move this func in grpc::new_grpc_server() ?
let grpc = HandleErrorLayer::new(|err: BoxError| async move {
eprintln!("{} {}", err, "grpc service failed");
// from https://docs.rs/tonic/latest/src/tonic/status.rs.html#100
let internal_error_code = 13;
[("grpc-status", internal_error_code)]
})
.layer(grpc_server.into_service());
api = api.nest_service("/*rpc", grpc);
let addr = SocketAddr::from(([0, 0, 0, 0], 3000));
axum::Server::bind(&addr)
.serve(api.into_make_service())
.await
.unwrap();
} But I don't know how to move the What would be the function signature? https://github.com/frederikhors/issue-tonic-web/blob/fix-using-nest_service/src/main.rs |
Beta Was this translation helpful? Give feedback.
I haven't tested it with a client but something like