Skip to content

Commit ff019d0

Browse files
committed
fix: json
1 parent ec2113e commit ff019d0

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

frameworks/Rust/sib/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ sib = { git = "https://github.com/PooyaEimandar/sib", default-features = false,
1818
bytes = { version = "1.10.1", default-features = false }
1919
mimalloc = { version = "0.1.47", features = ["secure"] }
2020
num_cpus = { version = "1.16" }
21+
serde = { version = "1.0.219", default-features = false, features = ["derive"] }
22+
serde_json = { version = "1.0.140" }
2123

2224
[profile.release]
2325
opt-level = 3

frameworks/Rust/sib/src/main.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,19 @@ use std::{
1212
#[global_allocator]
1313
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
1414

15+
#[derive(serde::Serialize)]
16+
struct JsonMessage<'a> {
17+
message: &'a str,
18+
}
19+
20+
impl Default for JsonMessage<'_> {
21+
fn default() -> Self {
22+
JsonMessage {
23+
message: "Hello, World!",
24+
}
25+
}
26+
}
27+
1528
struct H1Server<T>(pub T);
1629

1730
struct HService;
@@ -20,11 +33,12 @@ impl H1Service for HService {
2033
fn call<S: Read + Write>(&mut self, session: &mut Session<S>) -> std::io::Result<()> {
2134
if session.req_path() == Some("/json") {
2235
// Respond with JSON
36+
let json = serde_json::to_vec(&JsonMessage::default())?;
2337
session
2438
.status_code(Status::Ok)
2539
.header_str("Content-Type", "application/json")?
26-
.header_str("Content-Length", "27")?
27-
.body(&Bytes::from_static(b"{\"message\":\"Hello, World!\"}"))
40+
.header_str("Content-Length", &json.len().to_string())?
41+
.body(&Bytes::from(json))
2842
.eom();
2943
return Ok(());
3044
}

0 commit comments

Comments
 (0)