diff --git a/Cargo.toml b/Cargo.toml index 0f8d6de4..fe86e4f5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,7 +31,7 @@ members = [ "template_askama", "template_handlebars", "template_tera", -# "template_yarte", + "template_yarte", "todo", # "udp-echo", "unix-socket", diff --git a/template_yarte/Cargo.toml b/template_yarte/Cargo.toml index 90272f29..651017f8 100644 --- a/template_yarte/Cargo.toml +++ b/template_yarte/Cargo.toml @@ -10,12 +10,13 @@ workspace = ".." [dependencies] env_logger = "0.7" -yarte = { version = "0.3", features=["with-actix-web"] } +yarte = { git = "https://github.com/botika/yarte", features = ["with-actix-web"] } -actix-web = "1.0" +actix-rt = "1.0" +actix-web = "2.0.0-alpha" [build-dependencies] -yarte = { version = "0.3", features=["with-actix-web"] } +yarte = { git = "https://github.com/botika/yarte", features = ["with-actix-web"] } [dev-dependencies] bytes = "0.4" diff --git a/template_yarte/src/main.rs b/template_yarte/src/main.rs index ee4af804..3d3c3e27 100644 --- a/template_yarte/src/main.rs +++ b/template_yarte/src/main.rs @@ -1,44 +1,42 @@ -#[macro_use] -extern crate actix_web; - use std::collections::HashMap; -use actix_web::{middleware::Logger, web, App, HttpServer, Responder}; +use actix_web::{get, middleware::Logger, web, App, HttpServer, Responder}; use yarte::Template; #[derive(Template)] -#[template(path = "index.hbs")] +#[template(path = "index.hbs", err = "Some error message")] struct IndexTemplate { query: web::Query>, } #[get("/")] -pub fn index(query: web::Query>) -> impl Responder { +async fn index(query: web::Query>) -> impl Responder { IndexTemplate { query } } -fn main() -> std::io::Result<()> { +#[actix_rt::main] +async fn main() -> std::io::Result<()> { std::env::set_var("RUST_LOG", "actix_web=info"); env_logger::init(); // start http server HttpServer::new(move || App::new().wrap(Logger::default()).service(index)) .bind("127.0.0.1:8080")? - .run() + .start() + .await } #[cfg(test)] mod test { use super::*; - use actix_web::{http, test as atest}; - use bytes::Bytes; + use actix_web::{http, test as atest, web::Bytes}; - #[test] - fn test() { - let mut app = atest::init_service(App::new().service(index)); + #[actix_rt::test] + async fn test() { + let mut app = atest::init_service(App::new().service(index)).await; let req = atest::TestRequest::with_uri("/").to_request(); - let resp = atest::call_service(&mut app, req); + let resp = atest::call_service(&mut app, req).await; assert!(resp.status().is_success()); @@ -47,7 +45,7 @@ mod test { "text/html; charset=utf-8" ); - let bytes = atest::read_body(resp); + let bytes = atest::read_body(resp).await; assert_eq!( bytes, Bytes::from_static( @@ -67,7 +65,7 @@ mod test { ); let req = atest::TestRequest::with_uri("/?name=foo&lastname=bar").to_request(); - let resp = atest::call_service(&mut app, req); + let resp = atest::call_service(&mut app, req).await; assert!(resp.status().is_success()); @@ -76,7 +74,7 @@ mod test { "text/html; charset=utf-8" ); - let bytes = atest::read_body(resp); + let bytes = atest::read_body(resp).await; assert_eq!( bytes, Bytes::from_static( @@ -91,12 +89,16 @@ mod test { ); let req = atest::TestRequest::with_uri("/?name=foo").to_request(); - let resp = atest::call_service(&mut app, req); + let resp = atest::call_service(&mut app, req).await; assert!(resp.status().is_server_error()); + let bytes = atest::read_body(resp).await; + + assert_eq!(bytes, Bytes::from_static("Some error message".as_ref())); + let req = atest::TestRequest::with_uri("/?lastname=bar").to_request(); - let resp = atest::call_service(&mut app, req); + let resp = atest::call_service(&mut app, req).await; assert!(resp.status().is_success()); @@ -105,7 +107,7 @@ mod test { "text/html; charset=utf-8" ); - let bytes = atest::read_body(resp); + let bytes = atest::read_body(resp).await; assert_eq!( bytes, Bytes::from_static(