responder accepting &str and proper 404

This commit is contained in:
midefos 2025-01-18 15:33:59 +01:00
parent 7722a5eff5
commit e9e3b05dc5
3 changed files with 13 additions and 15 deletions

View File

@ -42,10 +42,6 @@ impl ServerBuilder {
} }
fn update_ip_filter_state(&mut self) { fn update_ip_filter_state(&mut self) {
if self.config.private_ips || !self.config.ips.is_empty() { self.config.ip_filter = self.config.private_ips || !self.config.ips.is_empty();
self.config.ip_filter = true;
} else {
self.config.ip_filter = false;
}
} }
} }

View File

@ -16,5 +16,5 @@ async fn main() {
} }
async fn handler(req: Request<Incoming>) -> Result<Response<Full<Bytes>>, Infallible> { async fn handler(req: Request<Incoming>) -> Result<Response<Full<Bytes>>, Infallible> {
Responder::text(format!("Hello World! {}", req.uri())) Responder::text(&format!("Hello World! {}", req.uri()))
} }

View File

@ -8,18 +8,18 @@ pub struct Responder;
impl Responder { impl Responder {
pub fn not_found() -> Result<Response<Full<Bytes>>, Infallible> { pub fn not_found() -> Result<Response<Full<Bytes>>, Infallible> {
Self::response_using_builder(Self::create_builder(401), "Not Found".to_string()) Self::response_using_builder(Self::create_builder(404), "Not Found")
} }
pub fn unathorized() -> Result<Response<Full<Bytes>>, Infallible> { pub fn unathorized() -> Result<Response<Full<Bytes>>, Infallible> {
Self::response_using_builder(Self::create_builder(401), "Unathorized".to_string()) Self::response_using_builder(Self::create_builder(401), "Unathorized")
} }
pub fn text(response: String) -> Result<Response<Full<Bytes>>, Infallible> { pub fn text(response: &str) -> Result<Response<Full<Bytes>>, Infallible> {
Self::response_using_builder(Self::create_builder(200), response) Self::response_using_builder(Self::create_builder(200), response)
} }
pub fn json<T>(json: T) -> Result<Response<Full<Bytes>>, Infallible> pub fn json<T>(json: &T) -> Result<Response<Full<Bytes>>, Infallible>
where where
T: Serialize, T: Serialize,
{ {
@ -28,25 +28,27 @@ impl Responder {
pub fn text_using_status( pub fn text_using_status(
status: u16, status: u16,
response: String, response: &str,
) -> Result<Response<Full<Bytes>>, Infallible> { ) -> Result<Response<Full<Bytes>>, Infallible> {
Self::response_using_builder(Self::create_builder(status), response) Self::response_using_builder(Self::create_builder(status), response)
} }
pub fn json_using_status<T>(status: u16, json: T) -> Result<Response<Full<Bytes>>, Infallible> pub fn json_using_status<T>(status: u16, json: &T) -> Result<Response<Full<Bytes>>, Infallible>
where where
T: Serialize, T: Serialize,
{ {
let builder = Self::create_builder(status).header(CONTENT_TYPE, "application/json"); let builder = Self::create_builder(status).header(CONTENT_TYPE, "application/json");
Self::response_using_builder(builder, serde_json::to_string(&json).unwrap()) Self::response_using_builder(builder, &serde_json::to_string(json).unwrap())
} }
fn response_using_builder( fn response_using_builder(
builder: Builder, builder: Builder,
response: String, response: &str,
) -> Result<Response<Full<Bytes>>, Infallible> { ) -> Result<Response<Full<Bytes>>, Infallible> {
Ok(builder.body(Full::new(Bytes::from(response))).unwrap()) Ok(builder
.body(Full::<Bytes>::from(response.to_string()))
.unwrap())
} }
fn create_builder(status: u16) -> Builder { fn create_builder(status: u16) -> Builder {