2022-02-02 16:18:19 +01:00
|
|
|
use std::collections::HashMap;
|
2022-07-09 22:08:11 +02:00
|
|
|
|
|
|
|
use actix_web::{
|
|
|
|
body::BoxBody,
|
|
|
|
dev::ServiceResponse,
|
|
|
|
error,
|
|
|
|
http::{header::ContentType, StatusCode},
|
|
|
|
middleware::{self, ErrorHandlerResponse, ErrorHandlers},
|
2022-10-16 22:36:23 +02:00
|
|
|
web, App, Error, HttpResponse, HttpServer, Responder, Result,
|
2022-07-09 22:08:11 +02:00
|
|
|
};
|
2022-10-16 22:20:01 +02:00
|
|
|
use actix_web_lab::respond::Html;
|
2019-12-18 19:25:56 +01:00
|
|
|
use tera::Tera;
|
2018-04-13 03:18:42 +02:00
|
|
|
|
2019-03-10 07:38:15 +01:00
|
|
|
// store tera template in application state
|
2019-12-07 18:59:24 +01:00
|
|
|
async fn index(
|
2019-03-17 04:23:09 +01:00
|
|
|
tmpl: web::Data<tera::Tera>,
|
2019-03-10 07:38:15 +01:00
|
|
|
query: web::Query<HashMap<String, String>>,
|
2022-10-16 22:36:23 +02:00
|
|
|
) -> Result<impl Responder, Error> {
|
2018-05-08 20:08:43 +02:00
|
|
|
let s = if let Some(name) = query.get("name") {
|
2019-03-10 07:38:15 +01:00
|
|
|
// submitted form
|
2018-04-13 03:18:42 +02:00
|
|
|
let mut ctx = tera::Context::new();
|
2022-02-25 22:07:22 +01:00
|
|
|
ctx.insert("name", name);
|
|
|
|
ctx.insert("text", "Welcome!");
|
2019-03-10 07:38:15 +01:00
|
|
|
tmpl.render("user.html", &ctx)
|
2018-04-13 03:18:42 +02:00
|
|
|
.map_err(|_| error::ErrorInternalServerError("Template error"))?
|
|
|
|
} else {
|
2019-03-10 07:38:15 +01:00
|
|
|
tmpl.render("index.html", &tera::Context::new())
|
2018-04-13 03:18:42 +02:00
|
|
|
.map_err(|_| error::ErrorInternalServerError("Template error"))?
|
|
|
|
};
|
2022-10-16 22:20:01 +02:00
|
|
|
|
|
|
|
Ok(Html(s))
|
2018-04-13 03:18:42 +02:00
|
|
|
}
|
|
|
|
|
2020-09-12 17:49:45 +02:00
|
|
|
#[actix_web::main]
|
2019-12-07 18:59:24 +01:00
|
|
|
async fn main() -> std::io::Result<()> {
|
2023-03-14 04:11:49 +01:00
|
|
|
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
|
|
|
|
|
|
|
|
log::info!("starting HTTP server at http://localhost:8080");
|
2018-04-13 03:18:42 +02:00
|
|
|
|
2019-03-10 07:38:15 +01:00
|
|
|
HttpServer::new(|| {
|
2022-02-18 03:44:02 +01:00
|
|
|
let tera = Tera::new(concat!(env!("CARGO_MANIFEST_DIR"), "/templates/**/*")).unwrap();
|
2018-04-13 03:18:42 +02:00
|
|
|
|
2019-03-10 07:38:15 +01:00
|
|
|
App::new()
|
2022-02-02 16:18:19 +01:00
|
|
|
.app_data(web::Data::new(tera))
|
2023-03-14 04:11:49 +01:00
|
|
|
.wrap(middleware::Logger::default())
|
2019-03-10 07:38:15 +01:00
|
|
|
.service(web::resource("/").route(web::get().to(index)))
|
2020-05-19 17:21:39 +02:00
|
|
|
.service(web::scope("").wrap(error_handlers()))
|
2019-03-10 03:03:09 +01:00
|
|
|
})
|
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-07 18:59:24 +01:00
|
|
|
.await
|
2018-04-13 03:18:42 +02:00
|
|
|
}
|
2020-05-19 17:21:39 +02:00
|
|
|
|
|
|
|
// Custom error handlers, to return HTML responses when an error occurs.
|
2022-02-02 16:18:19 +01:00
|
|
|
fn error_handlers() -> ErrorHandlers<BoxBody> {
|
2020-05-19 17:21:39 +02:00
|
|
|
ErrorHandlers::new().handler(StatusCode::NOT_FOUND, not_found)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Error handler for a 404 Page not found error.
|
2022-02-02 16:18:19 +01:00
|
|
|
fn not_found<B>(res: ServiceResponse<B>) -> Result<ErrorHandlerResponse<BoxBody>> {
|
2020-05-19 17:21:39 +02:00
|
|
|
let response = get_error_response(&res, "Page not found");
|
2022-02-02 16:18:19 +01:00
|
|
|
Ok(ErrorHandlerResponse::Response(ServiceResponse::new(
|
|
|
|
res.into_parts().0,
|
|
|
|
response.map_into_left_body(),
|
|
|
|
)))
|
2020-05-19 17:21:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Generic error handler.
|
2022-02-02 16:18:19 +01:00
|
|
|
fn get_error_response<B>(res: &ServiceResponse<B>, error: &str) -> HttpResponse {
|
2020-05-19 17:21:39 +02:00
|
|
|
let request = res.request();
|
|
|
|
|
|
|
|
// Provide a fallback to a simple plain text response in case an error occurs during the
|
|
|
|
// rendering of the error page.
|
2023-07-09 04:32:47 +02:00
|
|
|
let fallback = |err: &str| {
|
2022-02-02 16:18:19 +01:00
|
|
|
HttpResponse::build(res.status())
|
|
|
|
.content_type(ContentType::plaintext())
|
2023-07-09 04:32:47 +02:00
|
|
|
.body(err.to_string())
|
2020-05-19 17:21:39 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
let tera = request.app_data::<web::Data<Tera>>().map(|t| t.get_ref());
|
|
|
|
match tera {
|
|
|
|
Some(tera) => {
|
|
|
|
let mut context = tera::Context::new();
|
|
|
|
context.insert("error", error);
|
|
|
|
context.insert("status_code", res.status().as_str());
|
|
|
|
let body = tera.render("error.html", &context);
|
|
|
|
|
|
|
|
match body {
|
2022-02-02 16:18:19 +01:00
|
|
|
Ok(body) => HttpResponse::build(res.status())
|
|
|
|
.content_type(ContentType::html())
|
2020-05-19 17:21:39 +02:00
|
|
|
.body(body),
|
|
|
|
Err(_) => fallback(error),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => fallback(error),
|
|
|
|
}
|
|
|
|
}
|