Browse Source

Update actix fortunes yarte and remove worse (#6168)

Juan Aguilar 4 years ago
parent
commit
45bbd854fe

+ 1 - 2
frameworks/Rust/actix/Cargo.toml

@@ -33,8 +33,7 @@ actix-server = "1.0.4"
 actix-service = "1.0.6"
 actix-service = "1.0.6"
 snmalloc-rs = "0.2.6"
 snmalloc-rs = "0.2.6"
 askama = "0.8"
 askama = "0.8"
-markup = "0.4.1"
-yarte = "0.12"
+yarte = { version = "0.12", features = ["bytes-buf"] }
 serde = { version = "1.0", features = ["derive"] }
 serde = { version = "1.0", features = ["derive"] }
 serde_json = "1.0"
 serde_json = "1.0"
 env_logger = "0.7"
 env_logger = "0.7"

+ 1 - 1
frameworks/Rust/actix/actix-core.dockerfile

@@ -1,4 +1,4 @@
-FROM rust:1.44
+FROM rust:1.46
 
 
 RUN apt-get update -yqq && apt-get install -yqq cmake g++
 RUN apt-get update -yqq && apt-get install -yqq cmake g++
 
 

+ 1 - 1
frameworks/Rust/actix/actix-diesel.dockerfile

@@ -1,4 +1,4 @@
-FROM rust:1.44
+FROM rust:1.46
 
 
 RUN apt-get update -yqq && apt-get install -yqq cmake g++
 RUN apt-get update -yqq && apt-get install -yqq cmake g++
 
 

+ 1 - 1
frameworks/Rust/actix/actix-pg.dockerfile

@@ -1,4 +1,4 @@
-FROM rust:1.44
+FROM rust:1.46
 
 
 RUN apt-get update -yqq && apt-get install -yqq cmake g++
 RUN apt-get update -yqq && apt-get install -yqq cmake g++
 
 

+ 1 - 1
frameworks/Rust/actix/actix-raw.dockerfile

@@ -1,4 +1,4 @@
-FROM rust:1.44
+FROM rust:1.46
 
 
 RUN apt-get update -yqq && apt-get install -yqq cmake g++
 RUN apt-get update -yqq && apt-get install -yqq cmake g++
 
 

+ 1 - 1
frameworks/Rust/actix/actix.dockerfile

@@ -1,4 +1,4 @@
-FROM rust:1.44
+FROM rust:1.46
 
 
 RUN apt-get update -yqq && apt-get install -yqq cmake g++
 RUN apt-get update -yqq && apt-get install -yqq cmake g++
 
 

+ 3 - 2
frameworks/Rust/actix/src/db_pg.rs

@@ -1,3 +1,4 @@
+use std::borrow::Cow;
 use std::collections::HashMap;
 use std::collections::HashMap;
 use std::fmt::Write;
 use std::fmt::Write;
 use std::io;
 use std::io;
@@ -192,7 +193,7 @@ impl Handler<TellFortune> for PgConnection {
     fn handle(&mut self, _: TellFortune, _: &mut Self::Context) -> Self::Result {
     fn handle(&mut self, _: TellFortune, _: &mut Self::Context) -> Self::Result {
         let mut items = vec![Fortune {
         let mut items = vec![Fortune {
             id: 0,
             id: 0,
-            message: "Additional fortune added at request time.".to_string(),
+            message: Cow::Borrowed("Additional fortune added at request time."),
         }];
         }];
         let fut = self.cl.query_raw(&self.fortune, &[]);
         let fut = self.cl.query_raw(&self.fortune, &[]);
 
 
@@ -207,7 +208,7 @@ impl Handler<TellFortune> for PgConnection {
                 })?;
                 })?;
                 items.push(Fortune {
                 items.push(Fortune {
                     id: row.get(0),
                     id: row.get(0),
-                    message: row.get(1),
+                    message: Cow::Owned(row.get(1)),
                 });
                 });
             }
             }
 
 

+ 3 - 2
frameworks/Rust/actix/src/db_pg_direct.rs

@@ -1,3 +1,4 @@
+use std::borrow::Cow;
 use std::collections::HashMap;
 use std::collections::HashMap;
 use std::fmt::Write;
 use std::fmt::Write;
 use std::io;
 use std::io;
@@ -162,7 +163,7 @@ impl PgConnection {
     ) -> impl Future<Output = Result<Vec<Fortune>, io::Error>> {
     ) -> impl Future<Output = Result<Vec<Fortune>, io::Error>> {
         let mut items = vec![Fortune {
         let mut items = vec![Fortune {
             id: 0,
             id: 0,
-            message: "Additional fortune added at request time.".to_string(),
+            message: Cow::Borrowed("Additional fortune added at request time."),
         }];
         }];
 
 
         let fut = self.cl.query_raw(&self.fortune, &[]);
         let fut = self.cl.query_raw(&self.fortune, &[]);
@@ -178,7 +179,7 @@ impl PgConnection {
                 })?;
                 })?;
                 items.push(Fortune {
                 items.push(Fortune {
                     id: row.get(0),
                     id: row.get(0),
-                    message: row.get(1),
+                    message: Cow::Owned(row.get(1)),
                 });
                 });
             }
             }
 
 

+ 5 - 9
frameworks/Rust/actix/src/main_pg.rs

@@ -1,8 +1,6 @@
 #[global_allocator]
 #[global_allocator]
 static ALLOC: snmalloc_rs::SnMalloc = snmalloc_rs::SnMalloc;
 static ALLOC: snmalloc_rs::SnMalloc = snmalloc_rs::SnMalloc;
 
 
-use std::io::Write;
-
 use actix::prelude::*;
 use actix::prelude::*;
 use actix_http::error::ErrorInternalServerError;
 use actix_http::error::ErrorInternalServerError;
 use actix_http::{HttpService, KeepAlive};
 use actix_http::{HttpService, KeepAlive};
@@ -11,12 +9,13 @@ use actix_web::dev::{AppConfig, Body, Server};
 use actix_web::http::{header::CONTENT_TYPE, header::SERVER, HeaderValue, StatusCode};
 use actix_web::http::{header::CONTENT_TYPE, header::SERVER, HeaderValue, StatusCode};
 use actix_web::{web, App, Error, HttpRequest, HttpResponse};
 use actix_web::{web, App, Error, HttpRequest, HttpResponse};
 use bytes::BytesMut;
 use bytes::BytesMut;
+use yarte::ywrite_html;
 
 
 mod db_pg;
 mod db_pg;
 mod models;
 mod models;
 mod utils;
 mod utils;
 use crate::db_pg::{PgConnection, RandomWorld, RandomWorlds, TellFortune, UpdateWorld};
 use crate::db_pg::{PgConnection, RandomWorld, RandomWorlds, TellFortune, UpdateWorld};
-use crate::utils::{FortunesYarteTemplate, Writer};
+use crate::utils::Writer;
 
 
 async fn world_row(db: web::Data<Addr<PgConnection>>) -> Result<HttpResponse, Error> {
 async fn world_row(db: web::Data<Addr<PgConnection>>) -> Result<HttpResponse, Error> {
     let res = db
     let res = db
@@ -99,13 +98,10 @@ async fn fortune(db: web::Data<Addr<PgConnection>>) -> Result<HttpResponse, Erro
     match res {
     match res {
         Ok(fortunes) => {
         Ok(fortunes) => {
             let mut body = BytesMut::with_capacity(2048);
             let mut body = BytesMut::with_capacity(2048);
-            let mut writer = Writer(&mut body);
-            let _ = write!(writer, "{}", FortunesYarteTemplate { fortunes });
+            ywrite_html!(body, "{{> fortune }}");
 
 
-            let mut res = HttpResponse::with_body(
-                StatusCode::OK,
-                Body::Bytes(body.freeze().into()),
-            );
+            let mut res =
+                HttpResponse::with_body(StatusCode::OK, Body::Bytes(body.freeze()));
             res.headers_mut()
             res.headers_mut()
                 .insert(SERVER, HeaderValue::from_static("Actix"));
                 .insert(SERVER, HeaderValue::from_static("Actix"));
             res.headers_mut().insert(
             res.headers_mut().insert(

+ 5 - 4
frameworks/Rust/actix/src/main_platform.rs

@@ -2,7 +2,6 @@
 static ALLOC: snmalloc_rs::SnMalloc = snmalloc_rs::SnMalloc;
 static ALLOC: snmalloc_rs::SnMalloc = snmalloc_rs::SnMalloc;
 
 
 use std::future::Future;
 use std::future::Future;
-use std::io::Write;
 use std::pin::Pin;
 use std::pin::Pin;
 use std::task::{Context, Poll};
 use std::task::{Context, Poll};
 
 
@@ -15,13 +14,14 @@ use actix_service::{Service, ServiceFactory};
 use bytes::BytesMut;
 use bytes::BytesMut;
 use futures::future::ok;
 use futures::future::ok;
 use serde_json::to_writer;
 use serde_json::to_writer;
+use yarte::ywrite_html;
 
 
 mod db_pg_direct;
 mod db_pg_direct;
 mod models;
 mod models;
 mod utils;
 mod utils;
 
 
 use crate::db_pg_direct::PgConnection;
 use crate::db_pg_direct::PgConnection;
-use crate::utils::{FortunesTemplate, Writer};
+use crate::utils::Writer;
 
 
 struct App {
 struct App {
     db: PgConnection,
     db: PgConnection,
@@ -65,9 +65,10 @@ impl Service for App {
 
 
                 Box::pin(async move {
                 Box::pin(async move {
                     let fortunes = fut.await?;
                     let fortunes = fut.await?;
+
                     let mut body = BytesMut::with_capacity(2048);
                     let mut body = BytesMut::with_capacity(2048);
-                    let mut writer = Writer(&mut body);
-                    let _ = write!(writer, "{}", FortunesTemplate { fortunes });
+                    ywrite_html!(body, "{{> fortune }}");
+
                     let mut res =
                     let mut res =
                         Response::with_body(StatusCode::OK, Body::Bytes(body.freeze()));
                         Response::with_body(StatusCode::OK, Body::Bytes(body.freeze()));
                     let hdrs = res.headers_mut();
                     let hdrs = res.headers_mut();

+ 2 - 31
frameworks/Rust/actix/src/utils.rs

@@ -1,16 +1,15 @@
 #![allow(dead_code, unused_braces)]
 #![allow(dead_code, unused_braces)]
 
 
+use std::borrow::Cow;
 use std::{cmp, io};
 use std::{cmp, io};
 
 
 use bytes::{BufMut, BytesMut};
 use bytes::{BufMut, BytesMut};
 use serde::{Deserialize, Serialize};
 use serde::{Deserialize, Serialize};
-use yarte::Template;
 
 
-#[allow(non_snake_case)]
 #[derive(Serialize, Debug)]
 #[derive(Serialize, Debug)]
 pub struct Fortune {
 pub struct Fortune {
     pub id: i32,
     pub id: i32,
-    pub message: String,
+    pub message: Cow<'static, str>,
 }
 }
 
 
 pub const SIZE: usize = 27;
 pub const SIZE: usize = 27;
@@ -40,31 +39,3 @@ pub fn get_query_param(query: &str) -> u16 {
     };
     };
     cmp::min(500, cmp::max(1, q))
     cmp::min(500, cmp::max(1, q))
 }
 }
-
-markup::define! {
-    FortunesTemplate(fortunes: Vec<Fortune>) {
-        {markup::doctype()}
-        html {
-            head {
-                title { "Fortunes" }
-            }
-            body {
-                table {
-                    tr { th { "id" } th { "message" } }
-                    @for item in {fortunes} {
-                        tr {
-                            td { {item.id} }
-                            td { {markup::raw(v_htmlescape::escape(&item.message))} }
-                        }
-                    }
-                }
-            }
-        }
-    }
-}
-
-#[derive(Template)]
-#[template(path = "fortune")]
-pub struct FortunesYarteTemplate {
-    pub fortunes: Vec<Fortune>,
-}