diff --git a/src/bin/main.rs b/src/bin/main.rs index 70911d0..62d1fdd 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -6,6 +6,7 @@ use oblivion::models::server::Server; use oblivion::path_route; use oblivion::utils::parser::OblivionRequest; use oblivion_codegen::async_route; +use serde_json::json; use std::env::args; use std::time::Instant; @@ -26,6 +27,14 @@ fn welcome(mut req: OblivionRequest) -> BaseResponse { ) } +#[async_route] +fn json(_req: OblivionRequest) -> BaseResponse { + BaseResponse::JsonResponse( + json!({"status": true, "msg": "只身堕入极暗之永夜, 以期再世涅槃之阳光"}), + 200, + ) +} + #[tokio::main] async fn main() { let args: Vec = args().collect(); @@ -42,6 +51,7 @@ async fn main() { router.route(RoutePath::new("/handler", RouteType::Path), handler); path_route!(&mut router, "/welcome" => welcome); + path_route!(&mut router, "/json" => json); let mut server = Server::new("127.0.0.1", 813, router); server.run().await; diff --git a/src/models/client.rs b/src/models/client.rs index 76fcf41..6803743 100644 --- a/src/models/client.rs +++ b/src/models/client.rs @@ -8,7 +8,7 @@ use crate::utils::parser::{length, Oblivion, OblivionPath}; use p256::ecdh::EphemeralSecret; use p256::PublicKey; -use serde_json::{from_str, json, to_string, Value}; +use serde_json::{from_str, json, Value}; use tokio::net::TcpStream; pub struct Response { @@ -49,15 +49,7 @@ impl Response { } pub fn json(&mut self) -> Result { - Ok(from_str::(match &to_string(&self.content) { - Ok(string) => string, - Err(_) => { - return Err(OblivionException::InvalidOblivion(Some( - "Decode error occured when serialize bytes as json.".to_string(), - ))) - } - }) - .unwrap()) + Ok(from_str::(&self.text()?).unwrap()) } }