adding responder

This commit is contained in:
midefos 2025-01-16 20:04:13 +01:00
parent f89c1582f3
commit f5f3ee67a5
3 changed files with 18 additions and 5 deletions

View File

@ -1,5 +1,6 @@
mod builder;
mod config;
mod server;
mod responder;
pub use server::Server;

15
src/responder.rs Normal file
View File

@ -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<Response<Full<Bytes>>, Infallible> {
Ok(Response::builder()
.status(401)
.body(Full::new(Bytes::from("Unauthorized")))
.unwrap())
}
}

View File

@ -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()
}
}
}),