Skip to content

Commit

Permalink
feat(exporting): made export server serve 404 automatically
Browse files Browse the repository at this point in the history
This makes 404 page development *much* more convenient.
  • Loading branch information
arctic-hen7 committed Sep 16, 2022
1 parent 60557af commit cb4c5b1
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 9 deletions.
17 changes: 14 additions & 3 deletions packages/perseus-cli/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,9 +284,18 @@ async fn core_watch(dir: PathBuf, opts: Opts) -> Result<i32, Error> {
if export_opts.serve {
// Tell any connected browsers to reload
order_reload(opts.reload_server_host.to_string(), opts.reload_server_port);
serve_exported(dir, export_opts.host.to_string(), export_opts.port).await;
// This will terminate if we get an error exporting the 404 page
serve_exported(
dir,
export_opts.host.to_string(),
export_opts.port,
&tools,
&opts,
)
.await?
} else {
0
}
0
}
Subcommand::Serve(ref serve_opts) => {
create_dist(&dir)?;
Expand Down Expand Up @@ -351,7 +360,9 @@ async fn core_watch(dir: PathBuf, opts: Opts) -> Result<i32, Error> {
Subcommand::ExportErrorPage(ref eep_opts) => {
create_dist(&dir)?;
let tools = Tools::new(&dir, &opts).await?;
export_error_page(dir, eep_opts, &tools, &opts)?
export_error_page(
dir, eep_opts, &tools, &opts, true, /* Do prompt the user */
)?
}
Subcommand::New(ref new_opts) => new(dir, new_opts, &opts)?,
Subcommand::Init(ref init_opts) => init(dir, init_opts)?,
Expand Down
15 changes: 12 additions & 3 deletions packages/perseus-cli/src/export_error_page.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::cmd::run_cmd_directly;
use crate::cmd::run_cmd;
use crate::errors::ExecutionError;
use crate::install::Tools;
use crate::parse::{ExportErrorPageOpts, Opts};
Expand All @@ -11,8 +11,10 @@ pub fn export_error_page(
opts: &ExportErrorPageOpts,
tools: &Tools,
global_opts: &Opts,
prompt: bool,
) -> Result<i32, ExecutionError> {
run_cmd_directly(
// This function would tell the user everything if something goes wrong
let (_stdout, _stderr, exit_code) = run_cmd(
format!(
"{} run {} -- {} {}",
tools.cargo_engine,
Expand All @@ -26,5 +28,12 @@ pub fn export_error_page(
("PERSEUS_ENGINE_OPERATION", "export_error_page"),
("CARGO_TARGET_DIR", "dist/target_engine"),
],
)
|| {},
)?;

if prompt {
println!("🖨 Error page exported for code '{}'!", opts.code);
}

Ok(exit_code)
}
40 changes: 37 additions & 3 deletions packages/perseus-cli/src/serve_exported.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,45 @@ 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("🛰️ ", "");

/// Serves an exported app, assuming it's already been exported.
pub async fn serve_exported(dir: PathBuf, host: String, port: u16) {
pub async fn serve_exported(
dir: PathBuf,
host: String,
port: u16,
tools: &Tools,
global_opts: &Opts,
) -> Result<i32, ExecutionError> {
// Export the 404 page so we can serve that directly for convenience (we don't
// need to delete this, since we'll just put it in the `dist/exported`
// directory)
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, // Don't prompt the user
)?;
if exit_code != 0 {
return Ok(exit_code);
}

let dir = dir.join("dist/exported");
// We actually don't have to worry about HTML file extensions at all
let files = warp::any().and(warp::fs::dir(dir));
let files = warp::any()
.and(warp::fs::dir(dir))
.or(warp::fs::file("dist/exported/__export_404.html"));
// Parse `localhost` into `127.0.0.1` (picky Rust `std`)
let host = if host == "localhost" {
"127.0.0.1".to_string()
Expand All @@ -26,5 +58,7 @@ pub async fn serve_exported(dir: PathBuf, host: String, port: u16) {
port = port
);

warp::serve(files).run(addr).await
let _ = warp::serve(files).run(addr).await;
// We will never get here (the above runs forever)
Ok(0)
}

0 comments on commit cb4c5b1

Please sign in to comment.