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 extract in timeout handler #268

Merged
merged 2 commits into from
Dec 1, 2023
Merged

Conversation

yukiiiteru
Copy link
Member

Motivation

The previous TimeoutLayer in volo-http only supports Fn(&HttpContext) -> IntoResponse and defining such a function is not graceful.

This PR adds a new handler trait HandlerWithoutRequest. Like Handler of Route, it supports any arguments supported by FromContext, e.g.,

use volo_http::{Server, StatusCode, Uri, Address, layer::TimeoutLayer, Address, };

async fn hello() -> &'static str {
    "hello, world\n"
}

fn timeout_handler(uri: Uri, peer: Address) -> StatusCode {
    tracing::info!("Timeout on `{}`, peer: {}", uri, peer);
    StatusCode::INTERNAL_SERVER_ERROR
}

#[volo::main]
async fn main() {
    let app = Router::new()
        .route("/", get(hello))
        .layer(TimeoutLayer::new(Duration::from_secs(1), timeout_handler}));

    let addr: SocketAddr = "[::]:8000".parse().unwrap();
    let addr = volo::net::Address::from(addr);

    Server::new(app).run(addr).await.unwrap();
}

Additionally, volo-http re-exports some common types from bytes, http or hyper, while examples avoids direct dependencies on them.

Signed-off-by: Yu Li <liyu.yukiteru@bytedance.com>
Signed-off-by: Yu Li <liyu.yukiteru@bytedance.com>
@yukiiiteru yukiiiteru requested review from a team as code owners November 30, 2023 09:18
@yukiiiteru yukiiiteru enabled auto-merge (squash) November 30, 2023 10:09
@yukiiiteru yukiiiteru merged commit f0056d7 into main Dec 1, 2023
10 checks passed
@yukiiiteru yukiiiteru deleted the feat/volo-http branch December 1, 2023 03:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants