SolarEnergy/central_backend/src/server/web_app_controller.rs

47 lines
1.6 KiB
Rust
Raw Normal View History

2024-08-07 14:44:30 +00:00
#[cfg(debug_assertions)]
pub use serve_static_debug::{root_index, serve_assets_content};
#[cfg(not(debug_assertions))]
pub use serve_static_release::{root_index, serve_assets_content};
#[cfg(debug_assertions)]
mod serve_static_debug {
use actix_web::{HttpResponse, Responder};
pub async fn root_index() -> impl Responder {
HttpResponse::Ok()
.body("Solar energy secure home: Hello world! Debug=on for Solar platform!")
}
pub async fn serve_assets_content() -> impl Responder {
HttpResponse::NotFound().body("Hello world! Static assets are not served in debug mode")
}
}
#[cfg(not(debug_assertions))]
mod serve_static_release {
use actix_web::{web, HttpResponse, Responder};
use rust_embed::RustEmbed;
#[derive(RustEmbed)]
#[folder = "static/"]
struct Asset;
fn handle_embedded_file(path: &str, can_fallback: bool) -> HttpResponse {
match (Asset::get(path), can_fallback) {
(Some(content), _) => HttpResponse::Ok()
.content_type(mime_guess::from_path(path).first_or_octet_stream().as_ref())
.body(content.data.into_owned()),
(None, false) => HttpResponse::NotFound().body("404 Not Found"),
(None, true) => handle_embedded_file("index.html", false),
}
}
pub async fn root_index() -> impl Responder {
handle_embedded_file("index.html", false)
}
pub async fn serve_assets_content(path: web::Path<String>) -> impl Responder {
handle_embedded_file(&format!("assets/{}", path.as_ref()), false)
}
}