mirror of
https://github.com/actix/examples
synced 2024-11-27 16:02:57 +01:00
Update template yarte (#210)
This commit is contained in:
parent
22c7d747c2
commit
22fd5f3869
@ -31,7 +31,7 @@ members = [
|
|||||||
"template_askama",
|
"template_askama",
|
||||||
"template_handlebars",
|
"template_handlebars",
|
||||||
"template_tera",
|
"template_tera",
|
||||||
# "template_yarte",
|
"template_yarte",
|
||||||
"todo",
|
"todo",
|
||||||
# "udp-echo",
|
# "udp-echo",
|
||||||
"unix-socket",
|
"unix-socket",
|
||||||
|
@ -10,12 +10,13 @@ workspace = ".."
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
env_logger = "0.7"
|
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]
|
[build-dependencies]
|
||||||
yarte = { version = "0.3", features=["with-actix-web"] }
|
yarte = { git = "https://github.com/botika/yarte", features = ["with-actix-web"] }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
bytes = "0.4"
|
bytes = "0.4"
|
||||||
|
@ -1,44 +1,42 @@
|
|||||||
#[macro_use]
|
|
||||||
extern crate actix_web;
|
|
||||||
|
|
||||||
use std::collections::HashMap;
|
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;
|
use yarte::Template;
|
||||||
|
|
||||||
#[derive(Template)]
|
#[derive(Template)]
|
||||||
#[template(path = "index.hbs")]
|
#[template(path = "index.hbs", err = "Some error message")]
|
||||||
struct IndexTemplate {
|
struct IndexTemplate {
|
||||||
query: web::Query<HashMap<String, String>>,
|
query: web::Query<HashMap<String, String>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/")]
|
#[get("/")]
|
||||||
pub fn index(query: web::Query<HashMap<String, String>>) -> impl Responder {
|
async fn index(query: web::Query<HashMap<String, String>>) -> impl Responder {
|
||||||
IndexTemplate { query }
|
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");
|
std::env::set_var("RUST_LOG", "actix_web=info");
|
||||||
env_logger::init();
|
env_logger::init();
|
||||||
|
|
||||||
// start http server
|
// start http server
|
||||||
HttpServer::new(move || App::new().wrap(Logger::default()).service(index))
|
HttpServer::new(move || App::new().wrap(Logger::default()).service(index))
|
||||||
.bind("127.0.0.1:8080")?
|
.bind("127.0.0.1:8080")?
|
||||||
.run()
|
.start()
|
||||||
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use super::*;
|
use super::*;
|
||||||
use actix_web::{http, test as atest};
|
use actix_web::{http, test as atest, web::Bytes};
|
||||||
use bytes::Bytes;
|
|
||||||
|
|
||||||
#[test]
|
#[actix_rt::test]
|
||||||
fn test() {
|
async fn test() {
|
||||||
let mut app = atest::init_service(App::new().service(index));
|
let mut app = atest::init_service(App::new().service(index)).await;
|
||||||
|
|
||||||
let req = atest::TestRequest::with_uri("/").to_request();
|
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());
|
assert!(resp.status().is_success());
|
||||||
|
|
||||||
@ -47,7 +45,7 @@ mod test {
|
|||||||
"text/html; charset=utf-8"
|
"text/html; charset=utf-8"
|
||||||
);
|
);
|
||||||
|
|
||||||
let bytes = atest::read_body(resp);
|
let bytes = atest::read_body(resp).await;
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
bytes,
|
bytes,
|
||||||
Bytes::from_static(
|
Bytes::from_static(
|
||||||
@ -67,7 +65,7 @@ mod test {
|
|||||||
);
|
);
|
||||||
|
|
||||||
let req = atest::TestRequest::with_uri("/?name=foo&lastname=bar").to_request();
|
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());
|
assert!(resp.status().is_success());
|
||||||
|
|
||||||
@ -76,7 +74,7 @@ mod test {
|
|||||||
"text/html; charset=utf-8"
|
"text/html; charset=utf-8"
|
||||||
);
|
);
|
||||||
|
|
||||||
let bytes = atest::read_body(resp);
|
let bytes = atest::read_body(resp).await;
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
bytes,
|
bytes,
|
||||||
Bytes::from_static(
|
Bytes::from_static(
|
||||||
@ -91,12 +89,16 @@ mod test {
|
|||||||
);
|
);
|
||||||
|
|
||||||
let req = atest::TestRequest::with_uri("/?name=foo").to_request();
|
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());
|
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 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());
|
assert!(resp.status().is_success());
|
||||||
|
|
||||||
@ -105,7 +107,7 @@ mod test {
|
|||||||
"text/html; charset=utf-8"
|
"text/html; charset=utf-8"
|
||||||
);
|
);
|
||||||
|
|
||||||
let bytes = atest::read_body(resp);
|
let bytes = atest::read_body(resp).await;
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
bytes,
|
bytes,
|
||||||
Bytes::from_static(
|
Bytes::from_static(
|
||||||
|
Loading…
Reference in New Issue
Block a user