mirror of
https://github.com/actix/examples
synced 2025-06-28 09:50:36 +02:00
Restructure folders (#411)
This commit is contained in:
committed by
GitHub
parent
9db98162b2
commit
c3407627d0
11
template_engines/handlebars/Cargo.toml
Normal file
11
template_engines/handlebars/Cargo.toml
Normal file
@ -0,0 +1,11 @@
|
||||
[package]
|
||||
name = "template_handlebars"
|
||||
version = "1.0.0"
|
||||
authors = ["Alexandru Tiniuc <tiniuc.alexandru@gmail.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
actix-http = "2"
|
||||
actix-web = "3"
|
||||
handlebars = { version = "3.0.0", features = ["dir_source"] }
|
||||
serde_json = "1.0"
|
8
template_engines/handlebars/README.md
Normal file
8
template_engines/handlebars/README.md
Normal file
@ -0,0 +1,8 @@
|
||||
# Handlebars
|
||||
|
||||
This is an example of how to use Actix Web with the [Handlebars templating language](https://crates.io/crates/handlebars), which is currently the most popular crate that achieves this. After starting the server with `cargo run`, you may visit the following pages:
|
||||
|
||||
- http://localhost:8080
|
||||
- http://localhost:8080/Emma/documents
|
||||
- http://localhost:8080/Bob/passwords
|
||||
- http://localhost:8080/some-non-existing-page - 404 error rendered using template
|
110
template_engines/handlebars/src/main.rs
Normal file
110
template_engines/handlebars/src/main.rs
Normal file
@ -0,0 +1,110 @@
|
||||
#[macro_use]
|
||||
extern crate actix_web;
|
||||
|
||||
#[macro_use]
|
||||
extern crate serde_json;
|
||||
|
||||
use actix_http::{body::Body, Response};
|
||||
use actix_web::dev::ServiceResponse;
|
||||
use actix_web::http::StatusCode;
|
||||
use actix_web::middleware::errhandlers::{ErrorHandlerResponse, ErrorHandlers};
|
||||
use actix_web::{web, App, HttpResponse, HttpServer, Result};
|
||||
|
||||
use handlebars::Handlebars;
|
||||
|
||||
use std::io;
|
||||
|
||||
// Macro documentation can be found in the actix_web_codegen crate
|
||||
#[get("/")]
|
||||
async fn index(hb: web::Data<Handlebars<'_>>) -> HttpResponse {
|
||||
let data = json!({
|
||||
"name": "Handlebars"
|
||||
});
|
||||
let body = hb.render("index", &data).unwrap();
|
||||
|
||||
HttpResponse::Ok().body(body)
|
||||
}
|
||||
|
||||
#[get("/{user}/{data}")]
|
||||
async fn user(
|
||||
hb: web::Data<Handlebars<'_>>,
|
||||
web::Path(info): web::Path<(String, String)>,
|
||||
) -> HttpResponse {
|
||||
let data = json!({
|
||||
"user": info.0,
|
||||
"data": info.1
|
||||
});
|
||||
let body = hb.render("user", &data).unwrap();
|
||||
|
||||
HttpResponse::Ok().body(body)
|
||||
}
|
||||
|
||||
#[actix_web::main]
|
||||
async fn main() -> io::Result<()> {
|
||||
// Handlebars uses a repository for the compiled templates. This object must be
|
||||
// shared between the application threads, and is therefore passed to the
|
||||
// Application Builder as an atomic reference-counted pointer.
|
||||
let mut handlebars = Handlebars::new();
|
||||
handlebars
|
||||
.register_templates_directory(".html", "./static/templates")
|
||||
.unwrap();
|
||||
let handlebars_ref = web::Data::new(handlebars);
|
||||
|
||||
HttpServer::new(move || {
|
||||
App::new()
|
||||
.wrap(error_handlers())
|
||||
.app_data(handlebars_ref.clone())
|
||||
.service(index)
|
||||
.service(user)
|
||||
})
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
||||
// Custom error handlers, to return HTML responses when an error occurs.
|
||||
fn error_handlers() -> ErrorHandlers<Body> {
|
||||
ErrorHandlers::new().handler(StatusCode::NOT_FOUND, not_found)
|
||||
}
|
||||
|
||||
// Error handler for a 404 Page not found error.
|
||||
fn not_found<B>(res: ServiceResponse<B>) -> Result<ErrorHandlerResponse<B>> {
|
||||
let response = get_error_response(&res, "Page not found");
|
||||
Ok(ErrorHandlerResponse::Response(
|
||||
res.into_response(response.into_body()),
|
||||
))
|
||||
}
|
||||
|
||||
// Generic error handler.
|
||||
fn get_error_response<B>(res: &ServiceResponse<B>, error: &str) -> Response<Body> {
|
||||
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.
|
||||
let fallback = |e: &str| {
|
||||
Response::build(res.status())
|
||||
.content_type("text/plain")
|
||||
.body(e.to_string())
|
||||
};
|
||||
|
||||
let hb = request
|
||||
.app_data::<web::Data<Handlebars>>()
|
||||
.map(|t| t.get_ref());
|
||||
match hb {
|
||||
Some(hb) => {
|
||||
let data = json!({
|
||||
"error": error,
|
||||
"status_code": res.status().as_str()
|
||||
});
|
||||
let body = hb.render("error", &data);
|
||||
|
||||
match body {
|
||||
Ok(body) => Response::build(res.status())
|
||||
.content_type("text/html")
|
||||
.body(body),
|
||||
Err(_) => fallback(error),
|
||||
}
|
||||
}
|
||||
None => fallback(error),
|
||||
}
|
||||
}
|
10
template_engines/handlebars/static/templates/error.html
Normal file
10
template_engines/handlebars/static/templates/error.html
Normal file
@ -0,0 +1,10 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<title>{{error}}</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>{{status_code}} {{error}}</h1>
|
||||
</body>
|
||||
</html>
|
12
template_engines/handlebars/static/templates/index.html
Normal file
12
template_engines/handlebars/static/templates/index.html
Normal file
@ -0,0 +1,12 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<title>{{name}} Example</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h1>{{name}} example</h1>
|
||||
<p>This is an example of how to use {{name}} with Actix-Web.</p>
|
||||
</body>
|
||||
</html>
|
12
template_engines/handlebars/static/templates/user.html
Normal file
12
template_engines/handlebars/static/templates/user.html
Normal file
@ -0,0 +1,12 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<title>{{user}}'s homepage</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h1>Welcome back, {{user}}</h1>
|
||||
<p>Here's your {{data}}.</p>
|
||||
</body>
|
||||
</html>
|
Reference in New Issue
Block a user