2019-03-26 04:29:00 +01:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
2020-04-28 21:18:18 +02:00
|
|
|
use actix_web::{
|
2020-12-17 22:34:40 +01:00
|
|
|
get, middleware::Logger, web, App, Error, HttpResponse, HttpServer, ResponseError,
|
2020-04-28 21:18:18 +02:00
|
|
|
};
|
2020-12-17 22:34:40 +01:00
|
|
|
use derive_more::Display;
|
2021-03-03 17:38:53 +01:00
|
|
|
use yarte::{auto, ywrite_min};
|
2020-04-28 21:18:18 +02:00
|
|
|
|
2020-12-17 22:34:40 +01:00
|
|
|
#[derive(Debug, Display)]
|
|
|
|
struct MyErr(pub &'static str);
|
|
|
|
|
|
|
|
impl ResponseError for MyErr {}
|
2019-03-26 04:29:00 +01:00
|
|
|
|
2021-10-07 00:56:44 +02:00
|
|
|
#[allow(unused_must_use)] // ywrite_min causes warning: unused borrow that must be used
|
2019-03-26 04:29:00 +01:00
|
|
|
#[get("/")]
|
2020-04-28 21:18:18 +02:00
|
|
|
async fn index(
|
|
|
|
query: web::Query<HashMap<String, String>>,
|
|
|
|
) -> Result<HttpResponse, Error> {
|
2020-12-17 22:34:40 +01:00
|
|
|
// `ywrite_min` is work in progress check your templates before put in production
|
|
|
|
// or use `ywrite_html`
|
|
|
|
Ok(HttpResponse::Ok()
|
|
|
|
.content_type("text/html; charset=utf-8")
|
2021-03-03 17:38:53 +01:00
|
|
|
.body(auto!(ywrite_min!(String, "{{> index }}"))))
|
2019-03-26 04:29:00 +01:00
|
|
|
}
|
2019-03-12 21:15:53 +01:00
|
|
|
|
2020-09-12 17:49:45 +02:00
|
|
|
#[actix_web::main]
|
2019-12-15 18:07:58 +01:00
|
|
|
async fn main() -> std::io::Result<()> {
|
2019-03-12 21:15:53 +01:00
|
|
|
std::env::set_var("RUST_LOG", "actix_web=info");
|
|
|
|
env_logger::init();
|
|
|
|
|
|
|
|
// start http server
|
2019-04-01 01:23:31 +02:00
|
|
|
HttpServer::new(move || App::new().wrap(Logger::default()).service(index))
|
2022-02-17 21:22:36 +01:00
|
|
|
.bind(("127.0.0.1", 8080))?
|
2019-12-25 17:48:33 +01:00
|
|
|
.run()
|
2019-12-15 18:07:58 +01:00
|
|
|
.await
|
2019-04-01 01:23:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
2019-12-15 18:07:58 +01:00
|
|
|
use actix_web::{http, test as atest, web::Bytes};
|
2019-04-01 01:23:31 +02:00
|
|
|
|
2020-12-17 22:34:40 +01:00
|
|
|
use super::*;
|
|
|
|
|
2022-01-29 17:39:36 +01:00
|
|
|
#[actix_web::test]
|
2019-12-15 18:07:58 +01:00
|
|
|
async fn test() {
|
2022-02-06 10:04:43 +01:00
|
|
|
let app = atest::init_service(App::new().service(index)).await;
|
2019-04-01 01:23:31 +02:00
|
|
|
|
2019-07-01 06:02:14 +02:00
|
|
|
let req = atest::TestRequest::with_uri("/").to_request();
|
2022-02-06 10:04:43 +01:00
|
|
|
let resp = atest::call_service(&app, req).await;
|
2019-07-01 06:02:14 +02:00
|
|
|
|
|
|
|
assert!(resp.status().is_success());
|
2019-04-01 01:23:31 +02:00
|
|
|
|
|
|
|
assert_eq!(
|
2019-07-01 06:02:14 +02:00
|
|
|
resp.headers().get(http::header::CONTENT_TYPE).unwrap(),
|
2019-09-29 17:03:03 +02:00
|
|
|
"text/html; charset=utf-8"
|
2019-04-01 01:23:31 +02:00
|
|
|
);
|
|
|
|
|
2019-12-15 18:07:58 +01:00
|
|
|
let bytes = atest::read_body(resp).await;
|
2019-04-01 01:23:31 +02:00
|
|
|
assert_eq!(
|
|
|
|
bytes,
|
|
|
|
Bytes::from_static(
|
2020-04-28 21:18:18 +02:00
|
|
|
"<!DOCTYPE html><html><head><meta charset=\"utf-8\"><title>Actix \
|
2022-02-06 10:04:43 +01:00
|
|
|
Web</title></head><body><h1 id=\"welcome\" \
|
2020-04-28 21:18:18 +02:00
|
|
|
class=\"welcome\">Welcome!</h1><div><h3>What is your name?</h3><form>Name: \
|
|
|
|
<input type=\"text\" name=\"name\"><br>Last name: <input type=\"text\" \
|
|
|
|
name=\"lastname\"><br><p><input type=\"submit\"></p></form></div></body></html>"
|
2019-04-01 01:23:31 +02:00
|
|
|
.as_ref()
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2019-07-01 06:02:14 +02:00
|
|
|
let req = atest::TestRequest::with_uri("/?name=foo&lastname=bar").to_request();
|
2022-02-06 10:04:43 +01:00
|
|
|
let resp = atest::call_service(&app, req).await;
|
2019-07-01 06:02:14 +02:00
|
|
|
|
|
|
|
assert!(resp.status().is_success());
|
2019-04-01 01:23:31 +02:00
|
|
|
|
|
|
|
assert_eq!(
|
2019-07-01 06:02:14 +02:00
|
|
|
resp.headers().get(http::header::CONTENT_TYPE).unwrap(),
|
2019-09-29 17:03:03 +02:00
|
|
|
"text/html; charset=utf-8"
|
2019-04-01 01:23:31 +02:00
|
|
|
);
|
|
|
|
|
2019-12-15 18:07:58 +01:00
|
|
|
let bytes = atest::read_body(resp).await;
|
2019-04-01 01:23:31 +02:00
|
|
|
assert_eq!(
|
|
|
|
bytes,
|
|
|
|
Bytes::from_static(
|
2020-04-28 21:18:18 +02:00
|
|
|
"<!DOCTYPE html><html><head><meta charset=\"utf-8\"><title>Actix \
|
2022-02-06 10:19:55 +01:00
|
|
|
Web</title></head><body><h1>Hi, foo bar!</h1><p id=\"hi\" \
|
2020-04-28 21:18:18 +02:00
|
|
|
class=\"welcome\">Welcome</p></body></html>"
|
2019-04-01 01:23:31 +02:00
|
|
|
.as_ref()
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2019-07-01 06:02:14 +02:00
|
|
|
let req = atest::TestRequest::with_uri("/?name=foo").to_request();
|
2022-02-06 10:04:43 +01:00
|
|
|
let resp = atest::call_service(&app, req).await;
|
2019-07-01 06:02:14 +02:00
|
|
|
|
|
|
|
assert!(resp.status().is_server_error());
|
|
|
|
|
2019-12-15 18:07:58 +01:00
|
|
|
let bytes = atest::read_body(resp).await;
|
|
|
|
|
2020-12-17 22:34:40 +01:00
|
|
|
assert_eq!(bytes, Bytes::from_static("Bad query".as_ref()));
|
2019-12-15 18:07:58 +01:00
|
|
|
|
2019-07-01 06:02:14 +02:00
|
|
|
let req = atest::TestRequest::with_uri("/?lastname=bar").to_request();
|
2022-02-06 10:04:43 +01:00
|
|
|
let resp = atest::call_service(&app, req).await;
|
2019-04-01 01:23:31 +02:00
|
|
|
|
2019-07-01 06:02:14 +02:00
|
|
|
assert!(resp.status().is_success());
|
2019-04-01 01:23:31 +02:00
|
|
|
|
|
|
|
assert_eq!(
|
2019-07-01 06:02:14 +02:00
|
|
|
resp.headers().get(http::header::CONTENT_TYPE).unwrap(),
|
2019-09-29 17:03:03 +02:00
|
|
|
"text/html; charset=utf-8"
|
2019-04-01 01:23:31 +02:00
|
|
|
);
|
|
|
|
|
2019-12-15 18:07:58 +01:00
|
|
|
let bytes = atest::read_body(resp).await;
|
2019-04-01 01:23:31 +02:00
|
|
|
assert_eq!(
|
|
|
|
bytes,
|
|
|
|
Bytes::from_static(
|
2020-04-28 21:18:18 +02:00
|
|
|
"<!DOCTYPE html><html><head><meta charset=\"utf-8\"><title>Actix \
|
2022-02-06 10:19:55 +01:00
|
|
|
Web</title></head><body><h1 id=\"welcome\" \
|
2020-04-28 21:18:18 +02:00
|
|
|
class=\"welcome\">Welcome!</h1><div><h3>What is your name?</h3><form>Name: \
|
|
|
|
<input type=\"text\" name=\"name\"><br>Last name: <input type=\"text\" \
|
|
|
|
name=\"lastname\"><br><p><input type=\"submit\"></p></form></div></body></html>"
|
2019-04-01 01:23:31 +02:00
|
|
|
.as_ref()
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2019-03-12 21:15:53 +01:00
|
|
|
}
|