From 716f1fab54ef6c6b3a70f9cd4b2b70f7e9cdd7a8 Mon Sep 17 00:00:00 2001 From: Pinapelz Date: Wed, 5 Feb 2025 19:37:58 -0800 Subject: Initial commit --- src/main.rs | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 src/main.rs (limited to 'src') diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..877b86d --- /dev/null +++ b/src/main.rs @@ -0,0 +1,32 @@ +use actix_files as fs; +use actix_web::{web, App, HttpResponse, HttpServer, Result}; +use tera::Tera; + +async fn index(templates: web::Data) -> Result { + 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 +} -- cgit v1.2.3