perseus_cli/
serve_exported.rs1use console::Emoji;
2use std::net::SocketAddr;
3use std::path::PathBuf;
4use warp::Filter;
5
6use crate::{
7 errors::ExecutionError,
8 export_error_page,
9 parse::{ExportErrorPageOpts, Opts},
10 Tools,
11};
12
13static SERVING: Emoji<'_, '_> = Emoji("🛰️ ", "");
14
15pub async fn serve_exported(
17 dir: PathBuf,
18 host: String,
19 port: u16,
20 tools: &Tools,
21 global_opts: &Opts,
22) -> Result<i32, ExecutionError> {
23 let exit_code = export_error_page(
27 dir.clone(),
28 &ExportErrorPageOpts {
29 code: "404".to_string(),
30 output: "dist/exported/__export_404.html".to_string(),
31 },
32 tools,
33 global_opts,
34 false, )?;
36 if exit_code != 0 {
37 return Ok(exit_code);
38 }
39
40 let dir = dir.join("dist/exported");
41 let files = warp::any()
43 .and(warp::fs::dir(dir))
44 .or(warp::fs::file("dist/exported/__export_404.html"));
45 let host = if host == "localhost" {
47 "127.0.0.1".to_string()
48 } else {
49 host
50 };
51 let addr: SocketAddr = format!("{}:{}", host, port).parse().unwrap();
53 println!(
55 " [3/3] {} Your exported app is now live at <http://{host}:{port}>!",
56 SERVING,
57 host = host,
58 port = port
59 );
60
61 warp::serve(files).run(addr).await;
62 Ok(0)
64}