Skip to main content

serve

Function serve 

Source
pub async fn serve<S, B>(svc: S, req: Request) -> Response<B>
where S: Service<Request, Response = Response<B>, Error = Infallible>,
Available on crate features grpc and http only.
Expand description

Serve a gRPC request by forwarding it to a tower service.

This function is designed to work with tonic-generated server types, which implement tower::Service<http::Request<B>> for any body B satisfying http_body::Body + Send + 'static.

The response is returned as an http::Response<B> which implements IntoResponse, so it integrates directly with the #[http_service] handler return type.

§Extracting a gRPC service from a [Router]

If you have multiple services, you can compose them with [tonic::transport::server::Router] at the type level, or simply match on the request path and delegate to different serve calls.

§Example

use spin_sdk::http::{IntoResponse, Request};
use spin_sdk::{grpc, http_service};
#[http_service]
async fn handler(req: Request) -> impl IntoResponse {
    grpc::serve(GreeterServer::new(MyGreeter), req).await
}