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

Refactor hyper example #56

Merged
merged 2 commits into from
Aug 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 14 additions & 18 deletions examples/hyper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::sync::{Arc, Mutex};
use http_body_util::Full;
use hyper::body::{Bytes, Incoming};
use hyper::server::conn::http1::Builder as ConnectionBuilder;
use hyper::{Method, Request, Response};
use hyper::{Method, Request, Response, StatusCode};
use hyper_util::rt::TokioIo;
use tokio::net::TcpListener;
use tower::service_fn;
Expand All @@ -26,7 +26,7 @@ async fn blog(_req: Request<Incoming>) -> hyper::Result<Response<Body>> {
// 404 handler
async fn not_found(_req: Request<Incoming>) -> hyper::Result<Response<Body>> {
Ok(Response::builder()
.status(404)
.status(StatusCode::NOT_FOUND)
.body(Body::default())
.unwrap())
}
Expand All @@ -43,27 +43,23 @@ type Router = HashMap<Method, matchit::Router<Service>>;

async fn route(router: Arc<Router>, req: Request<Incoming>) -> hyper::Result<Response<Body>> {
// find the subrouter for this request method
let router = match router.get(req.method()) {
Some(router) => router,
let Some(router) = router.get(req.method()) else {
// if there are no routes for this method, respond with 405 Method Not Allowed
None => {
return Ok(Response::builder()
.status(405)
.body(Body::default())
.unwrap())
}
return Ok(Response::builder()
.status(StatusCode::METHOD_NOT_ALLOWED)
.body(Body::default())
.unwrap());
};

// find the service for this request path
match router.at(req.uri().path()) {
Ok(found) => {
// lock the service for a very short time, just to clone the service
let mut service = found.value.lock().unwrap().clone();
service.call(req).await
}
let Ok(found) = router.at(req.uri().path()) else {
// if we there is no matching service, call the 404 handler
Err(_) => not_found(req).await,
}
return not_found(req).await;
};

// lock the service for a very short time, just to clone the service
let mut service = found.value.lock().unwrap().clone();
service.call(req).await
}

#[tokio::main]
Expand Down
Loading