From f5f3ee67a5c0d8e94f49d3dd297fa03047d2f9e7 Mon Sep 17 00:00:00 2001 From: midefos Date: Thu, 16 Jan 2025 20:04:13 +0100 Subject: [PATCH] adding responder --- src/lib.rs | 1 + src/responder.rs | 15 +++++++++++++++ src/server.rs | 7 ++----- 3 files changed, 18 insertions(+), 5 deletions(-) create mode 100644 src/responder.rs diff --git a/src/lib.rs b/src/lib.rs index 78e55d4..edef2d3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,6 @@ mod builder; mod config; mod server; +mod responder; pub use server::Server; diff --git a/src/responder.rs b/src/responder.rs new file mode 100644 index 0000000..a222c5f --- /dev/null +++ b/src/responder.rs @@ -0,0 +1,15 @@ +use http::Response; +use http_body_util::Full; +use hyper::body::Bytes; +use std::convert::Infallible; + +pub struct Responder; + +impl Responder { + pub fn unathorized() -> Result>, Infallible> { + Ok(Response::builder() + .status(401) + .body(Full::new(Bytes::from("Unauthorized"))) + .unwrap()) + } +} diff --git a/src/server.rs b/src/server.rs index edb19ec..126c43e 100644 --- a/src/server.rs +++ b/src/server.rs @@ -1,4 +1,4 @@ -use crate::{builder::ServerBuilder, config::ServerConfig}; +use crate::{builder::ServerBuilder, config::ServerConfig, responder::Responder}; use http1::Builder; use http_body_util::Full; use hyper::{body::Incoming, server::conn::http1, service::service_fn, Request, Response}; @@ -58,10 +58,7 @@ impl Server { if config.is_req_authorized(&req) { handler(req).await } else { - Ok(Response::builder() - .status(401) - .body(Full::new(Bytes::from("Unauthorized"))) - .unwrap()) + Responder::unathorized() } } }),