diff --git a/src/main.rs b/src/main.rs index 5af199f..137ea57 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,9 +3,9 @@ use std::net::SocketAddr; use std::path::PathBuf; use actix_files::NamedFile; -use actix_web::http::StatusCode; -use actix_web::middleware::ErrorHandlers; use actix_web::{get, web, App, HttpRequest, HttpResponse, HttpServer, Responder}; +use actix_web::http::{Method, StatusCode}; +use actix_web::middleware::{self, ErrorHandlers}; use clap::Parser; use handlebars::Handlebars; @@ -56,6 +56,19 @@ async fn template( } } +#[get(r"/")] +async fn template_index(shared: web::Data>) -> actix_web::Result { + if shared.handlebars.has_template("index") { + let body = shared + .handlebars + .render("index", &()) + .map_err(|_| UserError::InternalError)?; + Ok(HttpResponse::Ok().body(body)) + } else { + Err(UserError::NotFound)? + } +} + #[get("/{filename:.*[^/]+}")] async fn static_file( shared: web::Data>, @@ -104,6 +117,7 @@ async fn main() -> Result<(), std::io::Error> { HttpServer::new(move || { App::new() + .wrap(middleware::NormalizePath::trim()) .wrap( ErrorHandlers::new() .handler(StatusCode::NOT_FOUND, generic_error) @@ -112,6 +126,8 @@ async fn main() -> Result<(), std::io::Error> { .app_data(shared_data.clone()) .service(template) .service(static_file) + .service(template_index) + .default_service(web::route().method(Method::GET)) }) .bind(config.address)? .run()