Add index page rendering

pull/8/head
Tristan Daniël Maat 2022-09-09 18:40:49 +01:00
parent ab405fc1f8
commit 2847dad110
Signed by: tlater
GPG Key ID: 49670FD774E43268
1 changed files with 18 additions and 2 deletions

View File

@ -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<SharedData<'_>>) -> actix_web::Result<impl Responder> {
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<SharedData<'_>>,
@ -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()