Skip to content

Commit 45bbd85

Browse files
authored
Update actix fortunes yarte and remove worse (#6168)
1 parent 41d87eb commit 45bbd85

File tree

11 files changed

+24
-55
lines changed

11 files changed

+24
-55
lines changed

frameworks/Rust/actix/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ actix-server = "1.0.4"
3333
actix-service = "1.0.6"
3434
snmalloc-rs = "0.2.6"
3535
askama = "0.8"
36-
markup = "0.4.1"
37-
yarte = "0.12"
36+
yarte = { version = "0.12", features = ["bytes-buf"] }
3837
serde = { version = "1.0", features = ["derive"] }
3938
serde_json = "1.0"
4039
env_logger = "0.7"

frameworks/Rust/actix/actix-core.dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM rust:1.44
1+
FROM rust:1.46
22

33
RUN apt-get update -yqq && apt-get install -yqq cmake g++
44

frameworks/Rust/actix/actix-diesel.dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM rust:1.44
1+
FROM rust:1.46
22

33
RUN apt-get update -yqq && apt-get install -yqq cmake g++
44

frameworks/Rust/actix/actix-pg.dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM rust:1.44
1+
FROM rust:1.46
22

33
RUN apt-get update -yqq && apt-get install -yqq cmake g++
44

frameworks/Rust/actix/actix-raw.dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM rust:1.44
1+
FROM rust:1.46
22

33
RUN apt-get update -yqq && apt-get install -yqq cmake g++
44

frameworks/Rust/actix/actix.dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM rust:1.44
1+
FROM rust:1.46
22

33
RUN apt-get update -yqq && apt-get install -yqq cmake g++
44

frameworks/Rust/actix/src/db_pg.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::borrow::Cow;
12
use std::collections::HashMap;
23
use std::fmt::Write;
34
use std::io;
@@ -192,7 +193,7 @@ impl Handler<TellFortune> for PgConnection {
192193
fn handle(&mut self, _: TellFortune, _: &mut Self::Context) -> Self::Result {
193194
let mut items = vec![Fortune {
194195
id: 0,
195-
message: "Additional fortune added at request time.".to_string(),
196+
message: Cow::Borrowed("Additional fortune added at request time."),
196197
}];
197198
let fut = self.cl.query_raw(&self.fortune, &[]);
198199

@@ -207,7 +208,7 @@ impl Handler<TellFortune> for PgConnection {
207208
})?;
208209
items.push(Fortune {
209210
id: row.get(0),
210-
message: row.get(1),
211+
message: Cow::Owned(row.get(1)),
211212
});
212213
}
213214

frameworks/Rust/actix/src/db_pg_direct.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::borrow::Cow;
12
use std::collections::HashMap;
23
use std::fmt::Write;
34
use std::io;
@@ -162,7 +163,7 @@ impl PgConnection {
162163
) -> impl Future<Output = Result<Vec<Fortune>, io::Error>> {
163164
let mut items = vec![Fortune {
164165
id: 0,
165-
message: "Additional fortune added at request time.".to_string(),
166+
message: Cow::Borrowed("Additional fortune added at request time."),
166167
}];
167168

168169
let fut = self.cl.query_raw(&self.fortune, &[]);
@@ -178,7 +179,7 @@ impl PgConnection {
178179
})?;
179180
items.push(Fortune {
180181
id: row.get(0),
181-
message: row.get(1),
182+
message: Cow::Owned(row.get(1)),
182183
});
183184
}
184185

frameworks/Rust/actix/src/main_pg.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
#[global_allocator]
22
static ALLOC: snmalloc_rs::SnMalloc = snmalloc_rs::SnMalloc;
33

4-
use std::io::Write;
5-
64
use actix::prelude::*;
75
use actix_http::error::ErrorInternalServerError;
86
use actix_http::{HttpService, KeepAlive};
@@ -11,12 +9,13 @@ use actix_web::dev::{AppConfig, Body, Server};
119
use actix_web::http::{header::CONTENT_TYPE, header::SERVER, HeaderValue, StatusCode};
1210
use actix_web::{web, App, Error, HttpRequest, HttpResponse};
1311
use bytes::BytesMut;
12+
use yarte::ywrite_html;
1413

1514
mod db_pg;
1615
mod models;
1716
mod utils;
1817
use crate::db_pg::{PgConnection, RandomWorld, RandomWorlds, TellFortune, UpdateWorld};
19-
use crate::utils::{FortunesYarteTemplate, Writer};
18+
use crate::utils::Writer;
2019

2120
async fn world_row(db: web::Data<Addr<PgConnection>>) -> Result<HttpResponse, Error> {
2221
let res = db
@@ -99,13 +98,10 @@ async fn fortune(db: web::Data<Addr<PgConnection>>) -> Result<HttpResponse, Erro
9998
match res {
10099
Ok(fortunes) => {
101100
let mut body = BytesMut::with_capacity(2048);
102-
let mut writer = Writer(&mut body);
103-
let _ = write!(writer, "{}", FortunesYarteTemplate { fortunes });
101+
ywrite_html!(body, "{{> fortune }}");
104102

105-
let mut res = HttpResponse::with_body(
106-
StatusCode::OK,
107-
Body::Bytes(body.freeze().into()),
108-
);
103+
let mut res =
104+
HttpResponse::with_body(StatusCode::OK, Body::Bytes(body.freeze()));
109105
res.headers_mut()
110106
.insert(SERVER, HeaderValue::from_static("Actix"));
111107
res.headers_mut().insert(

frameworks/Rust/actix/src/main_platform.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
static ALLOC: snmalloc_rs::SnMalloc = snmalloc_rs::SnMalloc;
33

44
use std::future::Future;
5-
use std::io::Write;
65
use std::pin::Pin;
76
use std::task::{Context, Poll};
87

@@ -15,13 +14,14 @@ use actix_service::{Service, ServiceFactory};
1514
use bytes::BytesMut;
1615
use futures::future::ok;
1716
use serde_json::to_writer;
17+
use yarte::ywrite_html;
1818

1919
mod db_pg_direct;
2020
mod models;
2121
mod utils;
2222

2323
use crate::db_pg_direct::PgConnection;
24-
use crate::utils::{FortunesTemplate, Writer};
24+
use crate::utils::Writer;
2525

2626
struct App {
2727
db: PgConnection,
@@ -65,9 +65,10 @@ impl Service for App {
6565

6666
Box::pin(async move {
6767
let fortunes = fut.await?;
68+
6869
let mut body = BytesMut::with_capacity(2048);
69-
let mut writer = Writer(&mut body);
70-
let _ = write!(writer, "{}", FortunesTemplate { fortunes });
70+
ywrite_html!(body, "{{> fortune }}");
71+
7172
let mut res =
7273
Response::with_body(StatusCode::OK, Body::Bytes(body.freeze()));
7374
let hdrs = res.headers_mut();

0 commit comments

Comments
 (0)