responder accepting &str and proper 404
This commit is contained in:
		| @@ -42,10 +42,6 @@ impl ServerBuilder { | ||||
|     } | ||||
|  | ||||
|     fn update_ip_filter_state(&mut self) { | ||||
|         if self.config.private_ips || !self.config.ips.is_empty() { | ||||
|             self.config.ip_filter = true; | ||||
|         } else { | ||||
|             self.config.ip_filter = false; | ||||
|         } | ||||
|         self.config.ip_filter = self.config.private_ips || !self.config.ips.is_empty(); | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -16,5 +16,5 @@ async fn main() { | ||||
| } | ||||
|  | ||||
| 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())) | ||||
| } | ||||
|   | ||||
| @@ -8,18 +8,18 @@ pub struct Responder; | ||||
|  | ||||
| impl Responder { | ||||
|     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> { | ||||
|         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) | ||||
|     } | ||||
|  | ||||
|     pub fn json<T>(json: T) -> Result<Response<Full<Bytes>>, Infallible> | ||||
|     pub fn json<T>(json: &T) -> Result<Response<Full<Bytes>>, Infallible> | ||||
|     where | ||||
|         T: Serialize, | ||||
|     { | ||||
| @@ -28,25 +28,27 @@ impl Responder { | ||||
|  | ||||
|     pub fn text_using_status( | ||||
|         status: u16, | ||||
|         response: String, | ||||
|         response: &str, | ||||
|     ) -> Result<Response<Full<Bytes>>, Infallible> { | ||||
|         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 | ||||
|         T: Serialize, | ||||
|     { | ||||
|         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( | ||||
|         builder: Builder, | ||||
|         response: String, | ||||
|         response: &str, | ||||
|     ) -> 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 { | ||||
|   | ||||
		Reference in New Issue
	
	Block a user