seedelf_cli/
web_server.rsuse colored::Colorize;
use include_dir::{include_dir, Dir};
use std::net::SocketAddr;
use warp::Filter;
const STATIC_DIR: Dir = include_dir!("static");
pub async fn run_web_server(message: String, network_flag: bool) {
let addr: SocketAddr = ([127, 0, 0, 1], 44203).into();
println!(
"{} {}",
"\nStarting server at".bright_cyan(),
format!("http://{}/", addr).bright_white()
);
println!("{}", "Hit Ctrl-C To Stop Server".bright_yellow());
let html_route = warp::path::end().map(move || {
let html_file = STATIC_DIR
.get_file("index.html")
.expect("Failed to read HTML file");
let mut html = html_file
.contents_utf8()
.expect("Failed to read HTML")
.to_string();
let dynamic_json = format!(r#"{{ "message": "{}" }}"#, message);
html = html.replace(r#"{ "message": "ACAB000000000000" }"#, &dynamic_json);
if network_flag {
html = html.replace(
r#"{ "network": "FADECAFE00000000" }"#,
format!(r#"{{ "network": "{}" }}"#, "preprod.").as_str(),
);
} else {
html = html.replace(
r#"{ "network": "FADECAFE00000000" }"#,
r#"{ "network": "" }"#,
);
}
warp::reply::html(html)
});
let js_route = warp::path("index.js").map(|| {
let file = STATIC_DIR
.get_file("index.js")
.expect("JavaScript file not found");
warp::reply::with_header(file.contents(), "Content-Type", "application/javascript")
});
let favicon_route = warp::path("favicon.ico").map(|| {
let file = STATIC_DIR
.get_file("favicon.ico")
.expect("Favicon not found");
warp::reply::with_header(file.contents(), "Content-Type", "image/x-icon")
});
let css_route = warp::path("index.css").map(|| {
let file = STATIC_DIR
.get_file("index.css")
.expect("CSS file not found");
warp::reply::with_header(file.contents(), "Content-Type", "text/css")
});
let routes = html_route.or(js_route).or(favicon_route).or(css_route);
let (_, server) = warp::serve(routes).bind_with_graceful_shutdown(addr, shutdown_signal());
server.await;
println!("{}", "Server has stopped.".bright_purple());
}
async fn shutdown_signal() {
tokio::signal::ctrl_c()
.await
.expect("Failed to install Ctrl-C handler");
println!("{}", "\nShutdown signal received...".red());
}