From afbf35670a79e298cd76d6242a08cd2dfee4df45 Mon Sep 17 00:00:00 2001 From: Tuyen Pham Date: Sat, 13 Aug 2022 13:40:54 +0700 Subject: [PATCH] [+] Add the ability to config the hostname and port in book.toml --- guide/src/format/configuration/general.md | 17 +++++++++++++++++ src/cmd/serve.rs | 16 ++++++++++++---- src/config.rs | 20 ++++++++++++++++++++ 3 files changed, 49 insertions(+), 4 deletions(-) diff --git a/guide/src/format/configuration/general.md b/guide/src/format/configuration/general.md index a00247ec..75e52d1f 100644 --- a/guide/src/format/configuration/general.md +++ b/guide/src/format/configuration/general.md @@ -17,6 +17,10 @@ edition = "2018" build-dir = "my-example-book" create-missing = false +[serve] +hostname = "localhost" +port = "3000" + [preprocessor.index] [preprocessor.links] @@ -108,3 +112,16 @@ use-default-preprocessors = true # use the default preprocessors default preprocessors from running. - Adding `[preprocessor.links]`, for example, will ensure, regardless of `use-default-preprocessors` that `links` it will run. + +### Serve options + +This controls the serve process of your book. + +```toml +[serve] +hostname = "localhost" +port = "3000" +``` + +- **hostname:** The hostname to serve the book, by default it will be `localhost`. +- **port:** The port to serve the book, by default it will be `3000`. diff --git a/src/cmd/serve.rs b/src/cmd/serve.rs index bafbfd52..4a249b9e 100644 --- a/src/cmd/serve.rs +++ b/src/cmd/serve.rs @@ -41,7 +41,6 @@ pub fn make_subcommand<'help>() -> App<'help> { .short('n') .long("hostname") .takes_value(true) - .default_value("localhost") .forbid_empty_values(true) .help("Hostname to listen on for HTTP connections"), ) @@ -50,7 +49,6 @@ pub fn make_subcommand<'help>() -> App<'help> { .short('p') .long("port") .takes_value(true) - .default_value("3000") .forbid_empty_values(true) .help("Port to use for HTTP connections"), ) @@ -62,8 +60,18 @@ pub fn execute(args: &ArgMatches) -> Result<()> { let book_dir = get_book_dir(args); let mut book = MDBook::load(&book_dir)?; - let port = args.value_of("port").unwrap(); - let hostname = args.value_of("hostname").unwrap(); + let hostname: String = match (args.value_of("hostname"), &book.config.serve.hostname) { + (Some(h), _) => h.to_owned(), + (_, Some(h)) => h.to_owned(), + (_, _) => "localhost".to_owned(), + }; + + let port: String = match (args.value_of("port"), &book.config.serve.port) { + (Some(p), _) => p.to_owned(), + (_, Some(p)) => p.to_owned(), + (_, _) => "3000".to_owned(), + }; + let open_browser = args.is_present("open"); let address = format!("{}:{}", hostname, port); diff --git a/src/config.rs b/src/config.rs index b7d03d1a..78e38a2d 100644 --- a/src/config.rs +++ b/src/config.rs @@ -70,6 +70,8 @@ pub struct Config { pub book: BookConfig, /// Information about the build environment. pub build: BuildConfig, + /// Information about the serve environment + pub serve: ServeConfig, /// Information about Rust language support. pub rust: RustConfig, rest: Value, @@ -289,6 +291,7 @@ impl Default for Config { Config { book: BookConfig::default(), build: BuildConfig::default(), + serve: ServeConfig::default(), rust: RustConfig::default(), rest: Value::Table(Table::default()), } @@ -333,6 +336,12 @@ impl<'de> Deserialize<'de> for Config { .transpose()? .unwrap_or_default(); + let serve: ServeConfig = table + .remove("serve") + .map(|serve| serve.try_into().map_err(D::Error::custom)) + .transpose()? + .unwrap_or_default(); + let rust: RustConfig = table .remove("rust") .map(|rust| rust.try_into().map_err(D::Error::custom)) @@ -342,6 +351,7 @@ impl<'de> Deserialize<'de> for Config { Ok(Config { book, build, + serve, rust, rest: Value::Table(table), }) @@ -449,6 +459,16 @@ impl Default for BuildConfig { } } +/// Configuration for the serve procedure +#[derive(Debug, Default, Clone, PartialEq, Serialize, Deserialize)] +#[serde(default, rename_all = "kebab-case")] +pub struct ServeConfig { + /// Default hostname + pub hostname: Option, + /// Default port + pub port: Option, +} + /// Configuration for the Rust compiler(e.g., for playground) #[derive(Debug, Default, Clone, PartialEq, Serialize, Deserialize)] #[serde(default, rename_all = "kebab-case")]