Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(volo-http): support any body or error for server #434

Merged
merged 1 commit into from
May 17, 2024

Conversation

wfly1998
Copy link
Member

This commit add more generic types for Service on server side. The body and error can be converted through layers, e.g., you can use the middleware::from_fn for convert Service<Cx, Request<Incoming>> to Service<Cx, Request<Bytes>>.

Example is also updated.

Motivation

In previous implementation, the Router and MethodRouter only supports fixed Body (http::Request with volo_http::body::Body), which is inconvenient.

Solution

This PR makes layer able to convert types of body and error. An example is updated:

async fn test_body_and_err(
    _: &mut ServerContext,
    _: ServerRequest<Bytes>,
) -> Result<ServerResponse, ()> {
    Ok(ServerResponse::default())
}

async fn map_body_and_err(
    cx: &mut ServerContext,
    req: ServerRequest,
    next: Next<Bytes, ()>,
) -> Result<ServerResponse, Infallible> {
    let (parts, body) = req.into_parts();
    let body = body.into_bytes().await.unwrap();
    let req = Request::from_parts(parts, body);
    Ok(next.run(cx, req).await.unwrap())
}

fn body_error_router() -> Router {
    Router::new()
        .route(
            "/body_err/test",
            get_service(service_fn(test_body_and_err))
                .post_service(service_fn(test_body_and_err)),
        )
        .layer(middleware::from_fn(map_body_and_err))
}

The outer Router uses Request with Body and Error = Infallible, while the inner service uses Request with Bytes and Error = (), and please notice that the middleware converted the types for supporting the operation.

Because all the above types have default generic types (body is hyper::body::Incoming and error is Infallible), in this PR, no break changes introduced. But if you want to use this feature, the FromRequest, Next (used for middleware::from_fn) should be updated with a new generic type B as type of the body.

This commit add more generic types for Service on server side. The
body and error can be converted through layers, e.g., you can use the
`middleware::from_fn` for convert `Service<Cx, Request<Incoming>>` to
`Service<Cx, Request<Bytes>>`.

Example is also updated.

Signed-off-by: Yu Li <liyu.yukiteru@bytedance.com>
@wfly1998 wfly1998 requested review from a team as code owners May 16, 2024 14:44
@PureWhiteWu PureWhiteWu added the A-volo-http This issue concerns the `volo-http` crate. label May 16, 2024
@wfly1998 wfly1998 merged commit a8c9c1c into cloudwego:main May 17, 2024
14 checks passed
@wfly1998 wfly1998 deleted the feat/server-any-body branch May 17, 2024 03:10
wfly1998 added a commit to wfly1998/volo that referenced this pull request May 22, 2024
Since cloudwego#434 updated `from_fn` and it can modify types of request body
and error, but extractor of request will refuse any body without
`http_body::Body`.

Considering that extractor of request is not common when using
`from_fn`, we remove it in this commit, and add support for changing
type of response.

With this commit, users can build more complex service, and example
is also updated.

Signed-off-by: Yu Li <liyu.yukiteru@bytedance.com>
wfly1998 added a commit that referenced this pull request May 23, 2024
Since #434 updated `from_fn` and it can modify types of request body
and error, but extractor of request will refuse any body without
`http_body::Body`.

Considering that extractor of request is not common when using
`from_fn`, we remove it in this commit, and add support for changing
type of response.

With this commit, users can build more complex service, and example
is also updated.

Signed-off-by: Yu Li <liyu.yukiteru@bytedance.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-volo-http This issue concerns the `volo-http` crate.
Development

Successfully merging this pull request may close these issues.

None yet

2 participants