Skip to content

Commit

Permalink
Merge pull request #2 from fu050409/main
Browse files Browse the repository at this point in the history
🚨 修复客户端json解析的问题
  • Loading branch information
fu050409 authored Dec 26, 2023
2 parents 8bdd8e3 + 60b2476 commit 20167df
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
10 changes: 10 additions & 0 deletions src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<String> = args().collect();
Expand All @@ -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;
Expand Down
12 changes: 2 additions & 10 deletions src/models/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -49,15 +49,7 @@ impl Response {
}

pub fn json(&mut self) -> Result<Value, OblivionException> {
Ok(from_str::<Value>(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::<Value>(&self.text()?).unwrap())
}
}

Expand Down

0 comments on commit 20167df

Please sign in to comment.