Browse Source

Salvo: upgrade version 0.23 (#7347)

* Salvo: upgrade version 0.23

* salvo: upgrade to 0.24
Chrislearn Young 3 years ago
parent
commit
a3961e72a8
2 changed files with 19 additions and 12 deletions
  1. 4 3
      frameworks/Rust/salvo/Cargo.toml
  2. 15 9
      frameworks/Rust/salvo/src/main.rs

+ 4 - 3
frameworks/Rust/salvo/Cargo.toml

@@ -18,16 +18,17 @@ path = "src/main_pg.rs"
 [dependencies]
 [dependencies]
 anyhow = "1.0"
 anyhow = "1.0"
 async-trait = "0.1.51"
 async-trait = "0.1.51"
+bytes = "1.1.0"
 diesel = { version = "1.4", features = ["postgres", "r2d2"] }
 diesel = { version = "1.4", features = ["postgres", "r2d2"] }
 futures = "0.3"
 futures = "0.3"
-markup = "0.12"
+markup = "0.13.0"
 mimalloc = { version = "0.1.25", default-features = false }
 mimalloc = { version = "0.1.25", default-features = false }
 once_cell = "1.5.2"
 once_cell = "1.5.2"
 rand = { version = "0.8.3", features = ["min_const_gen", "small_rng"] }
 rand = { version = "0.8.3", features = ["min_const_gen", "small_rng"] }
 random-fast-rng = "0.1.1"
 random-fast-rng = "0.1.1"
-salvo = { version = "0.22", features = ["anyhow"] }
+salvo = { version = "0.24", features = ["anyhow"] }
 serde = { version = "1.0", features = ["derive"] }
 serde = { version = "1.0", features = ["derive"] }
-serde_json = "1.0.64"
+serde_json = "1.0.81"
 smallvec = "1.6.1"
 smallvec = "1.6.1"
 snmalloc-rs = { version = "0.2.24", features = ["1mib", "native-cpu"] }
 snmalloc-rs = { version = "0.2.24", features = ["1mib", "native-cpu"] }
 tokio = { version = "1", features = ["macros", "rt"] }
 tokio = { version = "1", features = ["macros", "rt"] }

+ 15 - 9
frameworks/Rust/salvo/src/main.rs

@@ -4,7 +4,9 @@ static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
 use std::sync::Arc;
 use std::sync::Arc;
 use std::thread::available_parallelism;
 use std::thread::available_parallelism;
 
 
+use bytes::BytesMut;
 use salvo::http::header::{self, HeaderValue};
 use salvo::http::header::{self, HeaderValue};
+use salvo::http::response::Body;
 use salvo::prelude::*;
 use salvo::prelude::*;
 use serde::Serialize;
 use serde::Serialize;
 
 
@@ -17,19 +19,23 @@ pub struct Message {
 }
 }
 
 
 #[fn_handler]
 #[fn_handler]
-async fn json(res: &mut Response) {
-    res.headers_mut().insert(header::SERVER, HeaderValue::from_static("S"));
-    res.render(Json(Message {
+fn json(res: &mut Response) {
+    let headers = res.headers_mut();
+    headers.insert(header::SERVER, HeaderValue::from_static("S"));
+    headers.insert(header::CONTENT_TYPE, HeaderValue::from_static("application/json"));
+    let data = serde_json::to_vec(&Message {
         message: "Hello, World!",
         message: "Hello, World!",
-    }));
+    })
+    .unwrap();
+    res.set_body(Body::Bytes(BytesMut::from(data.as_slice())));
 }
 }
 
 
 #[fn_handler]
 #[fn_handler]
-async fn plaintext(res: &mut Response) {
-    res.headers_mut().insert(header::SERVER, HeaderValue::from_static("S"));
-    res.headers_mut()
-        .insert(header::CONTENT_TYPE, HeaderValue::from_static("text/plain"));
-    res.write_body(HELLO_WORLD).ok();
+fn plaintext(res: &mut Response) {
+    let headers = res.headers_mut();
+    headers.insert(header::SERVER, HeaderValue::from_static("S"));
+    headers.insert(header::CONTENT_TYPE, HeaderValue::from_static("text/plain"));
+    res.set_body(Body::Bytes(BytesMut::from(HELLO_WORLD)));
 }
 }
 
 
 fn main() {
 fn main() {