Skip to content

Commit

Permalink
refactor default to index route
Browse files Browse the repository at this point in the history
  • Loading branch information
svenrademakers committed May 3, 2024
1 parent 2153ed3 commit a525f33
Showing 1 changed file with 6 additions and 13 deletions.
19 changes: 6 additions & 13 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -64,14 +63,6 @@ use tracing_subscriber::Layer;

const HTTP_PORT: u16 = 80;

async fn react_index() -> Result<NamedFile, ActixError> {
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();
Expand All @@ -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")
Expand All @@ -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)
Expand Down

0 comments on commit a525f33

Please sign in to comment.