Skip to content

Commit 00341b8

Browse files
committed
[viz] Improve JSON response
1 parent b9f7af6 commit 00341b8

File tree

3 files changed

+26
-18
lines changed

3 files changed

+26
-18
lines changed

frameworks/Rust/viz/Cargo.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ required-features = ["diesel", "diesel-async", "sailfish"]
2525

2626
[dependencies]
2727
viz = "0.10"
28-
hyper = "1.5"
28+
hyper = "1.0"
2929
hyper-util = "0.1"
3030
http-body-util = "0.1"
3131
atoi = "2.0"
3232
serde = { version = "1.0", features = ["derive"] }
3333
serde_json = "1"
3434
mime = "0.3"
35-
rand = { version = "0.8", features = ["small_rng"] }
35+
rand = { version = "0.9", features = ["small_rng"] }
3636
thiserror = "2.0"
3737
futures-util = "0.3"
3838

@@ -66,3 +66,5 @@ sailfish = { version = "0.9", optional = true }
6666
[profile.release]
6767
lto = true
6868
codegen-units = 1
69+
strip = true
70+
opt-level = 3

frameworks/Rust/viz/src/db_pg.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::{collections::HashMap, fmt::Write, io, sync::Arc};
22

33
use futures_util::{stream::FuturesUnordered, StreamExt, TryFutureExt, TryStreamExt};
4-
use rand::{rngs::SmallRng, thread_rng, Rng, SeedableRng};
4+
use rand::{rng, rngs::SmallRng, Rng, SeedableRng};
55
use tokio::pin;
66
use tokio_postgres::{connect, types::ToSql, Client, NoTls, Statement};
77
use viz::{Error, IntoResponse, Response, StatusCode};
@@ -105,14 +105,14 @@ impl PgConnection {
105105
}
106106

107107
pub async fn get_world(&self) -> Result<World, PgError> {
108-
let mut rng = SmallRng::from_rng(&mut thread_rng()).unwrap();
108+
let mut rng = SmallRng::from_rng(&mut rng()).unwrap();
109109
let random_id = (rng.gen::<u32>() % 10_000 + 1) as i32;
110110

111111
self.query_one_world(random_id).await
112112
}
113113

114114
pub async fn get_worlds(&self, num: u16) -> Result<Vec<World>, PgError> {
115-
let mut rng = SmallRng::from_rng(&mut thread_rng()).unwrap();
115+
let mut rng = SmallRng::from_rng(&mut rng()).unwrap();
116116

117117
let worlds = FuturesUnordered::new();
118118

@@ -125,7 +125,7 @@ impl PgConnection {
125125
}
126126

127127
pub async fn update(&self, num: u16) -> Result<Vec<World>, PgError> {
128-
let mut rng = SmallRng::from_rng(&mut thread_rng()).unwrap();
128+
let mut rng = SmallRng::from_rng(&mut rng()).unwrap();
129129

130130
let worlds = FuturesUnordered::new();
131131

frameworks/Rust/viz/src/main.rs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use serde::Serialize;
44
use viz::{
5-
header::{HeaderValue, SERVER},
5+
header::{HeaderValue, CONTENT_TYPE, SERVER},
66
Bytes, Error, Request, Response, ResponseExt, Result, Router,
77
};
88

@@ -22,18 +22,24 @@ async fn plaintext(_: Request) -> Result<Response> {
2222
}
2323

2424
async fn json(_: Request) -> Result<Response> {
25-
let mut res = Response::with(
26-
http_body_util::Full::new(Bytes::from(
27-
serde_json::to_vec(&Message {
28-
message: "Hello, World!",
29-
})
30-
.unwrap(),
31-
)),
32-
mime::APPLICATION_JSON.as_ref(),
25+
let mut resp = Response::builder()
26+
.body(
27+
http_body_util::Full::new(Bytes::from(
28+
serde_json::to_vec(&Message {
29+
message: "Hello, World!",
30+
})
31+
.unwrap(),
32+
))
33+
.into(),
34+
)
35+
.unwrap();
36+
let headers = resp.headers_mut();
37+
headers.insert(SERVER, HeaderValue::from_static("Viz"));
38+
headers.insert(
39+
CONTENT_TYPE,
40+
HeaderValue::from_static(mime::APPLICATION_JSON.as_ref()),
3341
);
34-
res.headers_mut()
35-
.insert(SERVER, HeaderValue::from_static("Viz"));
36-
Ok(res)
42+
Ok(resp)
3743
}
3844

3945
#[tokio::main]

0 commit comments

Comments
 (0)