Skip to content

Commit cb4c5b1

Browse files
committed
feat(exporting): made export server serve 404 automatically
This makes 404 page development *much* more convenient.
1 parent 60557af commit cb4c5b1

File tree

3 files changed

+63
-9
lines changed

3 files changed

+63
-9
lines changed

packages/perseus-cli/src/bin/main.rs

+14-3
Original file line numberDiff line numberDiff line change
@@ -284,9 +284,18 @@ async fn core_watch(dir: PathBuf, opts: Opts) -> Result<i32, Error> {
284284
if export_opts.serve {
285285
// Tell any connected browsers to reload
286286
order_reload(opts.reload_server_host.to_string(), opts.reload_server_port);
287-
serve_exported(dir, export_opts.host.to_string(), export_opts.port).await;
287+
// This will terminate if we get an error exporting the 404 page
288+
serve_exported(
289+
dir,
290+
export_opts.host.to_string(),
291+
export_opts.port,
292+
&tools,
293+
&opts,
294+
)
295+
.await?
296+
} else {
297+
0
288298
}
289-
0
290299
}
291300
Subcommand::Serve(ref serve_opts) => {
292301
create_dist(&dir)?;
@@ -351,7 +360,9 @@ async fn core_watch(dir: PathBuf, opts: Opts) -> Result<i32, Error> {
351360
Subcommand::ExportErrorPage(ref eep_opts) => {
352361
create_dist(&dir)?;
353362
let tools = Tools::new(&dir, &opts).await?;
354-
export_error_page(dir, eep_opts, &tools, &opts)?
363+
export_error_page(
364+
dir, eep_opts, &tools, &opts, true, /* Do prompt the user */
365+
)?
355366
}
356367
Subcommand::New(ref new_opts) => new(dir, new_opts, &opts)?,
357368
Subcommand::Init(ref init_opts) => init(dir, init_opts)?,

packages/perseus-cli/src/export_error_page.rs

+12-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::cmd::run_cmd_directly;
1+
use crate::cmd::run_cmd;
22
use crate::errors::ExecutionError;
33
use crate::install::Tools;
44
use crate::parse::{ExportErrorPageOpts, Opts};
@@ -11,8 +11,10 @@ pub fn export_error_page(
1111
opts: &ExportErrorPageOpts,
1212
tools: &Tools,
1313
global_opts: &Opts,
14+
prompt: bool,
1415
) -> Result<i32, ExecutionError> {
15-
run_cmd_directly(
16+
// This function would tell the user everything if something goes wrong
17+
let (_stdout, _stderr, exit_code) = run_cmd(
1618
format!(
1719
"{} run {} -- {} {}",
1820
tools.cargo_engine,
@@ -26,5 +28,12 @@ pub fn export_error_page(
2628
("PERSEUS_ENGINE_OPERATION", "export_error_page"),
2729
("CARGO_TARGET_DIR", "dist/target_engine"),
2830
],
29-
)
31+
|| {},
32+
)?;
33+
34+
if prompt {
35+
println!("🖨 Error page exported for code '{}'!", opts.code);
36+
}
37+
38+
Ok(exit_code)
3039
}

packages/perseus-cli/src/serve_exported.rs

+37-3
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,45 @@ use std::net::SocketAddr;
33
use std::path::PathBuf;
44
use warp::Filter;
55

6+
use crate::{
7+
errors::ExecutionError,
8+
export_error_page,
9+
parse::{ExportErrorPageOpts, Opts},
10+
Tools,
11+
};
12+
613
static SERVING: Emoji<'_, '_> = Emoji("🛰️ ", "");
714

815
/// Serves an exported app, assuming it's already been exported.
9-
pub async fn serve_exported(dir: PathBuf, host: String, port: u16) {
16+
pub 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+
// Export the 404 page so we can serve that directly for convenience (we don't
24+
// need to delete this, since we'll just put it in the `dist/exported`
25+
// directory)
26+
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, // Don't prompt the user
35+
)?;
36+
if exit_code != 0 {
37+
return Ok(exit_code);
38+
}
39+
1040
let dir = dir.join("dist/exported");
1141
// We actually don't have to worry about HTML file extensions at all
12-
let files = warp::any().and(warp::fs::dir(dir));
42+
let files = warp::any()
43+
.and(warp::fs::dir(dir))
44+
.or(warp::fs::file("dist/exported/__export_404.html"));
1345
// Parse `localhost` into `127.0.0.1` (picky Rust `std`)
1446
let host = if host == "localhost" {
1547
"127.0.0.1".to_string()
@@ -26,5 +58,7 @@ pub async fn serve_exported(dir: PathBuf, host: String, port: u16) {
2658
port = port
2759
);
2860

29-
warp::serve(files).run(addr).await
61+
let _ = warp::serve(files).run(addr).await;
62+
// We will never get here (the above runs forever)
63+
Ok(0)
3064
}

0 commit comments

Comments
 (0)