blob: 877b86d95109aeb87de4e609358a4240072be108 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
use actix_files as fs;
use actix_web::{web, App, HttpResponse, HttpServer, Result};
use tera::Tera;
async fn index(templates: web::Data<Tera>) -> Result<HttpResponse> {
let mut context = tera::Context::new();
context.insert("name", "World");
let rendered = templates
.render("index.html", &context)
.map_err(|e| {
eprintln!("Template rendering error: {}", e);
actix_web::error::ErrorInternalServerError("Template rendering error")
})?;
Ok(HttpResponse::Ok().content_type("text/html").body(rendered))
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
let tera = Tera::new("templates/**/*").expect("Error initializing Tera");
HttpServer::new(move || {
App::new()
.app_data(web::Data::new(tera.clone()))
.route("/", web::get().to(index))
.service(fs::Files::new("/static", "./static").show_files_listing())
})
.bind("127.0.0.1:8080")?
.run()
.await
}
|