Add index page rendering
This commit is contained in:
parent
ab405fc1f8
commit
2847dad110
20
src/main.rs
20
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<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()
|
||||
|
|
Loading…
Reference in a new issue