Browse Source

[salvo]: Upgrade to version 0.54.x (#8413)

Chrislearn Young 2 years ago
parent
commit
bbd412508c

+ 1 - 1
frameworks/Rust/salvo/Cargo.toml

@@ -49,7 +49,7 @@ markup = "0.13"
 mongodb = { version = "2.4.0", features = ["zstd-compression", "snappy-compression", "zlib-compression"] }
 once_cell = "1"
 rand = { version = "0.8", features = ["min_const_gen", "small_rng"] }
-salvo = { version = "0.53", default-features = false, features = ["anyhow", "http1", "affix"] }
+salvo = { version = "0.54", default-features = false, features = ["anyhow", "http1", "affix"] }
 serde = { version = "1", features = ["derive"] }
 serde_json = "1"
 # smallvec = "1"

+ 5 - 10
frameworks/Rust/salvo/src/main.rs

@@ -12,11 +12,6 @@ use serde::Serialize;
 
 mod utils;
 
-static SERVER_HEADER: HeaderValue = HeaderValue::from_static("salvo");
-static JSON_HEADER: HeaderValue = HeaderValue::from_static("application/json");
-static PLAIN_HEADER: HeaderValue = HeaderValue::from_static("text/plain");
-static HELLO_WORD: Bytes = Bytes::from_static(b"Hello, world!");
-
 #[derive(Serialize)]
 pub struct Message {
     pub message: &'static str,
@@ -25,8 +20,8 @@ pub struct Message {
 #[handler]
 fn json(res: &mut Response) {
     let headers = res.headers_mut();
-    headers.insert(header::SERVER, SERVER_HEADER.clone());
-    headers.insert(header::CONTENT_TYPE, JSON_HEADER.clone());
+    headers.insert(header::SERVER, HeaderValue::from_static("salvo"));
+    headers.insert(header::CONTENT_TYPE, HeaderValue::from_static("application/json"));
     let data = serde_json::to_vec(&Message {
         message: "Hello, World!",
     })
@@ -37,9 +32,9 @@ fn json(res: &mut Response) {
 #[handler]
 fn plaintext(res: &mut Response) {
     let headers = res.headers_mut();
-    headers.insert(header::SERVER, SERVER_HEADER.clone());
-    headers.insert(header::CONTENT_TYPE, PLAIN_HEADER.clone());
-    res.body(ResBody::Once(HELLO_WORD.clone()));
+    headers.insert(header::SERVER, HeaderValue::from_static("salvo"));
+    headers.insert(header::CONTENT_TYPE, HeaderValue::from_static("text/plain"));
+    res.body(ResBody::Once(Bytes::from_static(b"Hello, world!")));
 }
 #[tokio::main]
 async 

+ 8 - 11
frameworks/Rust/salvo/src/main_diesel.rs

@@ -33,9 +33,6 @@ use schema::*;
 type PgPool = Pool<ConnectionManager<PgConnection>>;
 
 static DB_POOL: OnceCell<PgPool> = OnceCell::new();
-static SERVER_HEADER: HeaderValue = HeaderValue::from_static("salvo");
-static JSON_HEADER: HeaderValue = HeaderValue::from_static("application/json");
-static HTML_HEADER: HeaderValue = HeaderValue::from_static("text/html; charset=utf-8");
 
 fn connect() -> Result<PooledConnection<ConnectionManager<PgConnection>>, PoolError> {
     unsafe { DB_POOL.get_unchecked().get() }
@@ -60,8 +57,8 @@ async fn world_row(res: &mut Response) -> Result<(), Error> {
 
     let data = serde_json::to_vec(&world).unwrap();
     let headers = res.headers_mut();
-    headers.insert(header::SERVER, SERVER_HEADER.clone());
-    headers.insert(header::CONTENT_TYPE, JSON_HEADER.clone());
+    headers.insert(header::SERVER, HeaderValue::from_static("salvo"));
+    headers.insert(header::CONTENT_TYPE, HeaderValue::from_static("application/json"));
     res.body(ResBody::Once(Bytes::from(data)));
     Ok(())
 }
@@ -81,8 +78,8 @@ async fn queries(req: &mut Request, res: &mut Response) -> Result<(), Error> {
 
     let data = serde_json::to_vec(&worlds)?;
     let headers = res.headers_mut();
-    headers.insert(header::SERVER, SERVER_HEADER.clone());
-    headers.insert(header::CONTENT_TYPE, JSON_HEADER.clone());
+    headers.insert(header::SERVER, HeaderValue::from_static("salvo"));
+    headers.insert(header::CONTENT_TYPE, HeaderValue::from_static("application/json"));
     res.body(ResBody::Once(Bytes::from(data)));
     Ok(())
 }
@@ -113,8 +110,8 @@ async fn updates(req: &mut Request, res: &mut Response) -> Result<(), Error> {
 
     let data = serde_json::to_vec(&worlds)?;
     let headers = res.headers_mut();
-    headers.insert(header::SERVER, SERVER_HEADER.clone());
-    headers.insert(header::CONTENT_TYPE, JSON_HEADER.clone());
+    headers.insert(header::SERVER, HeaderValue::from_static("salvo"));
+    headers.insert(header::CONTENT_TYPE, HeaderValue::from_static("application/json"));
     res.body(ResBody::Once(Bytes::from(data)));
     Ok(())
 }
@@ -133,8 +130,8 @@ async fn fortunes(res: &mut Response) -> Result<(), Error> {
     write!(&mut data, "{}", FortunesTemplate { items }).unwrap();
 
     let headers = res.headers_mut();
-    headers.insert(header::SERVER, SERVER_HEADER.clone());
-    headers.insert(header::CONTENT_TYPE, HTML_HEADER.clone());
+    headers.insert(header::SERVER, HeaderValue::from_static("salvo"));
+    headers.insert(header::CONTENT_TYPE, HeaderValue::from_static("text/html; charset=utf-8"));
     res.body(ResBody::Once(Bytes::from(data)));
     Ok(())
 }

+ 2 - 5
frameworks/Rust/salvo/src/main_lru.rs

@@ -28,9 +28,6 @@ use db_pg::PgConnection;
 
 static CACHED_WORLDS: OnceCell<LruCache<usize, World>> = OnceCell::new();
 
-static SERVER_HEADER: HeaderValue = HeaderValue::from_static("salvo");
-static JSON_HEADER: HeaderValue = HeaderValue::from_static("application/json");
-
 #[handler]
 fn cached_queries(req: &mut Request, res: &mut Response) -> Result<(), Error> {
     let count = req.query::<u16>("q").unwrap_or(1);
@@ -46,8 +43,8 @@ fn cached_queries(req: &mut Request, res: &mut Response) -> Result<(), Error> {
     }
     let data = serde_json::to_vec(&worlds)?;
     let headers = res.headers_mut();
-    headers.insert(header::SERVER, SERVER_HEADER.clone());
-    headers.insert(header::CONTENT_TYPE, JSON_HEADER.clone());
+    headers.insert(header::SERVER, HeaderValue::from_static("salvo"));
+    headers.insert(header::CONTENT_TYPE, HeaderValue::from_static("application/json"));
     res.body(ResBody::Once(Bytes::from(data)));
     Ok(())
 }

+ 8 - 12
frameworks/Rust/salvo/src/main_mongo.rs

@@ -29,10 +29,6 @@ mod utils;
 use db_mongo::*;
 use models_mongo::*;
 
-static SERVER_HEADER: HeaderValue = HeaderValue::from_static("salvo");
-static JSON_HEADER: HeaderValue = HeaderValue::from_static("application/json");
-static HTML_HEADER: HeaderValue = HeaderValue::from_static("text/html; charset=utf-8");
-
 #[handler]
 async fn world_row(res: &mut Response, depot: &mut Depot) -> Result<(), Error> {
     let mut rng = SmallRng::from_entropy();
@@ -43,8 +39,8 @@ async fn world_row(res: &mut Response, depot: &mut Depot) -> Result<(), Error> {
 
     let data = serde_json::to_vec(&world).unwrap();
     let headers = res.headers_mut();
-    headers.insert(header::SERVER, SERVER_HEADER.clone());
-    headers.insert(header::CONTENT_TYPE, JSON_HEADER.clone());
+    headers.insert(header::SERVER, HeaderValue::from_static("salvo"));
+    headers.insert(header::CONTENT_TYPE, HeaderValue::from_static("application/json"));
     res.body(ResBody::Once(Bytes::from(data)));
     Ok(())
 }
@@ -64,8 +60,8 @@ async fn queries(req: &mut Request, depot: &mut Depot, res: &mut Response) -> Re
 
     let data = serde_json::to_vec(&worlds)?;
     let headers = res.headers_mut();
-    headers.insert(header::SERVER, SERVER_HEADER.clone());
-    headers.insert(header::CONTENT_TYPE, JSON_HEADER.clone());
+    headers.insert(header::SERVER, HeaderValue::from_static("salvo"));
+    headers.insert(header::CONTENT_TYPE, HeaderValue::from_static("application/json"));
     res.body(ResBody::Once(Bytes::from(data)));
     Ok(())
 }
@@ -90,8 +86,8 @@ async fn updates(req: &mut Request, depot: &mut Depot, res: &mut Response) -> Re
     update_worlds(db.clone(), worlds).await?;
 
     let headers = res.headers_mut();
-    headers.insert(header::SERVER, SERVER_HEADER.clone());
-    headers.insert(header::CONTENT_TYPE, JSON_HEADER.clone());
+    headers.insert(header::SERVER, HeaderValue::from_static("salvo"));
+    headers.insert(header::CONTENT_TYPE, HeaderValue::from_static("application/json"));
     res.body(ResBody::Once(Bytes::from(data)));
     Ok(())
 }
@@ -105,8 +101,8 @@ async fn fortunes(res: &mut Response, depot: &mut Depot) -> Result<(), Error> {
     write!(&mut data, "{}", FortunesTemplate { items })?;
 
     let headers = res.headers_mut();
-    headers.insert(header::SERVER, SERVER_HEADER.clone());
-    headers.insert(header::CONTENT_TYPE, HTML_HEADER.clone());
+    headers.insert(header::SERVER, HeaderValue::from_static("salvo"));
+    headers.insert(header::CONTENT_TYPE, HeaderValue::from_static("text/html; charset=utf-8"));
     res.body(ResBody::Once(Bytes::from(data)));
     Ok(())
 }

+ 6 - 9
frameworks/Rust/salvo/src/main_mongo_raw.rs

@@ -28,9 +28,6 @@ mod utils;
 use db_mongo_raw::*;
 use models_mongo::*;
 
-static SERVER_HEADER: HeaderValue = HeaderValue::from_static("salvo");
-static JSON_HEADER: HeaderValue = HeaderValue::from_static("application/json");
-
 #[handler]
 async fn world_row(res: &mut Response, depot: &mut Depot) -> Result<(), Error> {
     let mut rng = SmallRng::from_entropy();
@@ -41,8 +38,8 @@ async fn world_row(res: &mut Response, depot: &mut Depot) -> Result<(), Error> {
 
     let data = serde_json::to_vec(&world).unwrap();
     let headers = res.headers_mut();
-    headers.insert(header::SERVER, SERVER_HEADER.clone());
-    headers.insert(header::CONTENT_TYPE, JSON_HEADER.clone());
+    headers.insert(header::SERVER, HeaderValue::from_static("salvo"));
+    headers.insert(header::CONTENT_TYPE, HeaderValue::from_static("application/json"));
     res.body(ResBody::Once(Bytes::from(data)));
     Ok(())
 }
@@ -62,8 +59,8 @@ async fn queries(req: &mut Request, depot: &mut Depot, res: &mut Response) -> Re
 
     let data = serde_json::to_vec(&worlds)?;
     let headers = res.headers_mut();
-    headers.insert(header::SERVER, SERVER_HEADER.clone());
-    headers.insert(header::CONTENT_TYPE, JSON_HEADER.clone());
+    headers.insert(header::SERVER, HeaderValue::from_static("salvo"));
+    headers.insert(header::CONTENT_TYPE, HeaderValue::from_static("application/json"));
     res.body(ResBody::Once(Bytes::from(data)));
     Ok(())
 }
@@ -90,8 +87,8 @@ async fn updates(req: &mut Request, depot: &mut Depot, res: &mut Response) -> Re
     update_worlds(db.clone(), worlds).await?;
 
     let headers = res.headers_mut();
-    headers.insert(header::SERVER, SERVER_HEADER.clone());
-    headers.insert(header::CONTENT_TYPE, JSON_HEADER.clone());
+    headers.insert(header::SERVER, HeaderValue::from_static("salvo"));
+    headers.insert(header::CONTENT_TYPE, HeaderValue::from_static("application/json"));
     res.body(ResBody::Once(Bytes::from(data)));
     Ok(())
 }

+ 38 - 19
frameworks/Rust/salvo/src/main_pg.rs

@@ -9,11 +9,11 @@ use std::thread::available_parallelism;
 
 use async_trait::async_trait;
 use bytes::Bytes;
+use dotenv::dotenv;
 use salvo::conn::tcp::TcpAcceptor;
 use salvo::http::header::{self, HeaderValue};
 use salvo::http::ResBody;
 use salvo::prelude::*;
-use dotenv::dotenv;
 use salvo::routing::FlowCtrl;
 
 mod db_pg;
@@ -21,10 +21,6 @@ mod models_pg;
 mod utils;
 use db_pg::PgConnection;
 
-static SERVER_HEADER: HeaderValue = HeaderValue::from_static("salvo");
-static JSON_HEADER: HeaderValue = HeaderValue::from_static("application/json");
-static HTML_HEADER: HeaderValue = HeaderValue::from_static("text/html; charset=utf-8");
-
 struct WorldHandler {
     conn: PgConnection,
 }
@@ -40,12 +36,18 @@ impl WorldHandler {
 }
 #[async_trait]
 impl Handler for WorldHandler {
-    async fn handle(&self, _req: &mut Request, _depot: &mut Depot, res: &mut Response, _ctrl: &mut FlowCtrl) {
+    async fn handle(
+        &self,
+        _req: &mut Request,
+        _depot: &mut Depot,
+        res: &mut Response,
+        _ctrl: &mut FlowCtrl,
+    ) {
         let world = self.conn.get_world().await.unwrap();
         let data = serde_json::to_vec(&world).unwrap();
         let headers = res.headers_mut();
-        headers.insert(header::SERVER, SERVER_HEADER.clone());
-        headers.insert(header::CONTENT_TYPE, JSON_HEADER.clone());
+        headers.insert(header::SERVER, HeaderValue::from_static("salvo"));
+        headers.insert(header::CONTENT_TYPE, HeaderValue::from_static("application/json"));
         res.body(ResBody::Once(Bytes::from(data)));
     }
 }
@@ -64,15 +66,21 @@ impl WorldsHandler {
 }
 #[async_trait]
 impl Handler for WorldsHandler {
-    async fn handle(&self, req: &mut Request, _depot: &mut Depot, res: &mut Response, _ctrl: &mut FlowCtrl) {
+    async fn handle(
+        &self,
+        req: &mut Request,
+        _depot: &mut Depot,
+        res: &mut Response,
+        _ctrl: &mut FlowCtrl,
+    ) {
         let count = req.query::<u16>("q").unwrap_or(1);
         let count = cmp::min(500, cmp::max(1, count));
         let worlds = self.conn.get_worlds(count).await.unwrap();
 
         let data = serde_json::to_vec(&worlds).unwrap();
         let headers = res.headers_mut();
-        headers.insert(header::SERVER, SERVER_HEADER.clone());
-        headers.insert(header::CONTENT_TYPE, JSON_HEADER.clone());
+        headers.insert(header::SERVER, HeaderValue::from_static("salvo"));
+        headers.insert(header::CONTENT_TYPE, HeaderValue::from_static("application/json"));
         res.body(ResBody::Once(Bytes::from(data)));
     }
 }
@@ -91,16 +99,21 @@ impl UpdatesHandler {
 }
 #[async_trait]
 impl Handler for UpdatesHandler {
-    async fn handle(&self, req: &mut Request, _depot: &mut Depot, res: &mut Response, _ctrl: &mut FlowCtrl) {
+    async fn handle(
+        &self,
+        req: &mut Request,
+        _depot: &mut Depot,
+        res: &mut Response,
+        _ctrl: &mut FlowCtrl,
+    ) {
         let count = req.query::<u16>("q").unwrap_or(1);
         let count = cmp::min(500, cmp::max(1, count));
-        res.headers_mut().insert(header::SERVER, SERVER_HEADER.clone());
         let worlds = self.conn.update(count).await.unwrap();
 
         let data = serde_json::to_vec(&worlds).unwrap();
         let headers = res.headers_mut();
-        headers.insert(header::SERVER, SERVER_HEADER.clone());
-        headers.insert(header::CONTENT_TYPE, JSON_HEADER.clone());
+        headers.insert(header::SERVER, HeaderValue::from_static("salvo"));
+        headers.insert(header::CONTENT_TYPE, HeaderValue::from_static("application/json"));
         res.body(ResBody::Once(Bytes::from(data)));
     }
 }
@@ -119,20 +132,26 @@ impl FortunesHandler {
 }
 #[async_trait]
 impl Handler for FortunesHandler {
-    async fn handle(&self, _req: &mut Request, _depot: &mut Depot, res: &mut Response, _ctrl: &mut FlowCtrl) {
+    async fn handle(
+        &self,
+        _req: &mut Request,
+        _depot: &mut Depot,
+        res: &mut Response,
+        _ctrl: &mut FlowCtrl,
+    ) {
         let mut data = String::new();
         write!(&mut data, "{}", self.conn.tell_fortune().await.unwrap()).unwrap();
 
         let headers = res.headers_mut();
-        headers.insert(header::SERVER, SERVER_HEADER.clone());
-        headers.insert(header::CONTENT_TYPE, HTML_HEADER.clone());
+        headers.insert(header::SERVER, HeaderValue::from_static("salvo"));
+        headers.insert(header::CONTENT_TYPE, HeaderValue::from_static("text/html; charset=utf-8"));
         res.body(ResBody::Once(Bytes::from(data)));
     }
 }
 
 fn main() {
     dotenv().ok();
-    
+
     let thread_count = available_parallelism().map(|n| n.get()).unwrap_or(16);
     let rt = tokio::runtime::Builder::new_current_thread()
         .enable_all()

+ 8 - 12
frameworks/Rust/salvo/src/main_pg_pool.rs

@@ -30,10 +30,6 @@ use models_pg_pool::*;
 
 static DB_POOL: OnceCell<Pool> = OnceCell::new();
 
-static SERVER_HEADER: HeaderValue = HeaderValue::from_static("salvo");
-static JSON_HEADER: HeaderValue = HeaderValue::from_static("application/json");
-static HTML_HEADER: HeaderValue = HeaderValue::from_static("text/html; charset=utf-8");
-
 fn pool() -> &'static Pool {
     unsafe { DB_POOL.get_unchecked() }
 }
@@ -48,8 +44,8 @@ async fn world_row(res: &mut Response) -> Result<(), Error> {
 
     let data = serde_json::to_vec(&world).unwrap();
     let headers = res.headers_mut();
-    headers.insert(header::SERVER, SERVER_HEADER.clone());
-    headers.insert(header::CONTENT_TYPE, JSON_HEADER.clone());
+    headers.insert(header::SERVER, HeaderValue::from_static("salvo"));
+    headers.insert(header::CONTENT_TYPE, HeaderValue::from_static("application/json"));
     res.body(ResBody::Once(Bytes::from(data)));
     Ok(())
 }
@@ -72,8 +68,8 @@ async fn queries(req: &mut Request, res: &mut Response) -> Result<(), Error> {
 
     let data = serde_json::to_vec(&worlds)?;
     let headers = res.headers_mut();
-    headers.insert(header::SERVER, SERVER_HEADER.clone());
-    headers.insert(header::CONTENT_TYPE, JSON_HEADER.clone());
+    headers.insert(header::SERVER, HeaderValue::from_static("salvo"));
+    headers.insert(header::CONTENT_TYPE, HeaderValue::from_static("application/json"));
     res.body(ResBody::Once(Bytes::from(data)));
     Ok(())
 }
@@ -107,8 +103,8 @@ async fn updates(req: &mut Request, res: &mut Response) -> Result<(), Error> {
 
     let data = serde_json::to_vec(&worlds)?;
     let headers = res.headers_mut();
-    headers.insert(header::SERVER, SERVER_HEADER.clone());
-    headers.insert(header::CONTENT_TYPE, JSON_HEADER.clone());
+    headers.insert(header::SERVER, HeaderValue::from_static("salvo"));
+    headers.insert(header::CONTENT_TYPE, HeaderValue::from_static("application/json"));
     res.body(ResBody::Once(Bytes::from(data)));
     Ok(())
 }
@@ -128,8 +124,8 @@ async fn fortunes(res: &mut Response) -> Result<(), Error> {
     write!(&mut data, "{}", FortunesTemplate { items }).unwrap();
 
     let headers = res.headers_mut();
-    headers.insert(header::SERVER, SERVER_HEADER.clone());
-    headers.insert(header::CONTENT_TYPE, HTML_HEADER.clone());
+    headers.insert(header::SERVER, HeaderValue::from_static("salvo"));
+    headers.insert(header::CONTENT_TYPE, HeaderValue::from_static("text/html; charset=utf-8"));
     res.body(ResBody::Once(Bytes::from(data)));
     Ok(())
 }

+ 4 - 8
frameworks/Rust/salvo/src/main_sqlx.rs

@@ -28,10 +28,6 @@ use models_sqlx::*;
 
 static DB_POOL: OnceCell<PgPool> = OnceCell::new();
 
-static SERVER_HEADER: HeaderValue = HeaderValue::from_static("salvo");
-static JSON_HEADER: HeaderValue = HeaderValue::from_static("application/json");
-static HTML_HEADER: HeaderValue = HeaderValue::from_static("text/html; charset=utf-8");
-
 fn pool() -> &'static PgPool {
     unsafe { DB_POOL.get_unchecked() }
 }
@@ -45,8 +41,8 @@ async fn world_row(res: &mut Response) -> Result<(), Error> {
 
     let data = serde_json::to_vec(&world).unwrap();
     let headers = res.headers_mut();
-    headers.insert(header::SERVER, SERVER_HEADER.clone());
-    headers.insert(header::CONTENT_TYPE, JSON_HEADER.clone());
+    headers.insert(header::SERVER, HeaderValue::from_static("salvo"));
+    headers.insert(header::CONTENT_TYPE, HeaderValue::from_static("application/json"));
     res.body(ResBody::Once(Bytes::from(data)));
     Ok(())
 }
@@ -65,8 +61,8 @@ async fn fortunes(res: &mut Response) -> Result<(), Error> {
     write!(&mut data, "{}", FortunesTemplate { items }).unwrap();
 
     let headers = res.headers_mut();
-    headers.insert(header::SERVER, SERVER_HEADER.clone());
-    headers.insert(header::CONTENT_TYPE, HTML_HEADER.clone());
+    headers.insert(header::SERVER, HeaderValue::from_static("salvo"));
+    headers.insert(header::CONTENT_TYPE, HeaderValue::from_static("text/html; charset=utf-8"));
     res.body(ResBody::Once(Bytes::from(data)));
     Ok(())
 }