use console::Emoji;
use std::net::SocketAddr;
use std::path::PathBuf;
use warp::Filter;
use crate::{
errors::ExecutionError,
export_error_page,
parse::{ExportErrorPageOpts, Opts},
Tools,
};
static SERVING: Emoji<'_, '_> = Emoji("🛰️ ", "");
pub async fn serve_exported(
dir: PathBuf,
host: String,
port: u16,
tools: &Tools,
global_opts: &Opts,
) -> Result<i32, ExecutionError> {
let exit_code = export_error_page(
dir.clone(),
&ExportErrorPageOpts {
code: "404".to_string(),
output: "dist/exported/__export_404.html".to_string(),
},
tools,
global_opts,
false, )?;
if exit_code != 0 {
return Ok(exit_code);
}
let dir = dir.join("dist/exported");
let files = warp::any()
.and(warp::fs::dir(dir))
.or(warp::fs::file("dist/exported/__export_404.html"));
let host = if host == "localhost" {
"127.0.0.1".to_string()
} else {
host
};
let addr: SocketAddr = format!("{}:{}", host, port).parse().unwrap();
println!(
" [3/3] {} Your exported app is now live at <http://{host}:{port}>!",
SERVING,
host = host,
port = port
);
warp::serve(files).run(addr).await;
Ok(0)
}