Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Single page app #19

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,25 @@ pub fn serve(
}

let path = super::local_path_for_request(&req.uri(), &config.root_dir);
if config.single_page_app_mode {
if let Some(ref path) = path {
if !path.is_file() {
let display_path = path.display().to_string();
info!(
"replacing 404 or directory redirect for \"{}\" with index page",
&display_path[1..] // skip the initial '.'
);
let root_index = config.root_dir.join("index.html");
if root_index.is_file() {
return Box::new(
File::open(root_index.clone())
.map_err(Error::from)
.and_then(move |file| super::respond_with_file(file, root_index)),
);
}
}
}
}
if path.is_none() {
return Box::new(future::result(resp));
}
Expand Down
15 changes: 14 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ fn run() -> Result<()> {
// Create the configuration from the command line arguments. It
// includes the IP address and port to listen on and the path to use
// as the HTTP server's root directory.
let config = Config::from_args();
let config = Config::from_args_implied();

// Display the configuration to be helpful
info!("basic-http-server {}", env!("CARGO_PKG_VERSION"));
Expand Down Expand Up @@ -112,6 +112,19 @@ pub struct Config {
/// Enable developer extensions
#[structopt(short = "x")]
use_extensions: bool,
/// Single page app mode (404s are redirected to '/') (implies '-x')
#[structopt(long = "single-page-app")]
single_page_app_mode: bool,
}

impl Config {
fn from_args_implied() -> Self {
let mut config = Config::from_args();
if config.single_page_app_mode {
config.use_extensions = true;
}
config
}
}

/// The function that returns a future of an HTTP response for each hyper
Expand Down