From ac987344703112090a563202751a572ea4ccc980 Mon Sep 17 00:00:00 2001 From: Sven Rademakers Date: Fri, 3 May 2024 09:19:36 +0100 Subject: [PATCH] refactor default to index route --- src/main.rs | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/src/main.rs b/src/main.rs index 39d86c0..0fcdf5f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -34,11 +34,10 @@ use actix_files::Files; use actix_files::NamedFile; use actix_web::http::KeepAlive; use actix_web::{ - error::ErrorInternalServerError, http::{self}, web, web::Data, - App, Error as ActixError, HttpRequest, HttpResponse, HttpServer, + App, HttpRequest, HttpResponse, HttpServer, }; use anyhow::Context; use app::{bmc_application::BmcApplication, event_application::run_event_listener}; @@ -64,14 +63,6 @@ use tracing_subscriber::Layer; const HTTP_PORT: u16 = 80; -async fn react_index() -> Result { - let config = Config::try_from(config_path()) - .map_err(|e| ErrorInternalServerError(format!("Error parsing config file: {}", e)))?; - let www_root = config.www.clone(); - NamedFile::open(www_root.join("index.html")) - .map_err(|e| ErrorInternalServerError(e)) -} - #[actix_web::main] async fn main() -> anyhow::Result<()> { let _logger_lifetime = init_logger(); @@ -94,6 +85,7 @@ async fn main() -> anyhow::Result<()> { run_event_listener(bmc.clone().into_inner())?; let run_server = HttpServer::new(move || { + let www_root = config.www.clone(); App::new() .service( web::scope("/api/bmc") @@ -107,9 +99,10 @@ async fn main() -> anyhow::Result<()> { ) // Serve a static tree of files of the web UI. Must be the last item. .service(Files::new("/", &config.www).index_file("index.html")) - .default_service( - web::route().to(react_index) - ) + .default_service(web::to(move || { + let www_index = www_root.join("index.html"); + NamedFile::open_async(www_index) + })) }) .bind_openssl((config.host.clone(), config.port), tls)? .keep_alive(KeepAlive::Os)