[+] Add the ability to config the hostname and port in book.toml

This commit is contained in:
Tuyen Pham 2022-08-13 13:40:54 +07:00
parent fff067b2a8
commit afbf35670a
3 changed files with 49 additions and 4 deletions

View File

@ -17,6 +17,10 @@ edition = "2018"
build-dir = "my-example-book" build-dir = "my-example-book"
create-missing = false create-missing = false
[serve]
hostname = "localhost"
port = "3000"
[preprocessor.index] [preprocessor.index]
[preprocessor.links] [preprocessor.links]
@ -108,3 +112,16 @@ use-default-preprocessors = true # use the default preprocessors
default preprocessors from running. default preprocessors from running.
- Adding `[preprocessor.links]`, for example, will ensure, regardless of - Adding `[preprocessor.links]`, for example, will ensure, regardless of
`use-default-preprocessors` that `links` it will run. `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`.

View File

@ -41,7 +41,6 @@ pub fn make_subcommand<'help>() -> App<'help> {
.short('n') .short('n')
.long("hostname") .long("hostname")
.takes_value(true) .takes_value(true)
.default_value("localhost")
.forbid_empty_values(true) .forbid_empty_values(true)
.help("Hostname to listen on for HTTP connections"), .help("Hostname to listen on for HTTP connections"),
) )
@ -50,7 +49,6 @@ pub fn make_subcommand<'help>() -> App<'help> {
.short('p') .short('p')
.long("port") .long("port")
.takes_value(true) .takes_value(true)
.default_value("3000")
.forbid_empty_values(true) .forbid_empty_values(true)
.help("Port to use for HTTP connections"), .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 book_dir = get_book_dir(args);
let mut book = MDBook::load(&book_dir)?; let mut book = MDBook::load(&book_dir)?;
let port = args.value_of("port").unwrap(); let hostname: String = match (args.value_of("hostname"), &book.config.serve.hostname) {
let hostname = args.value_of("hostname").unwrap(); (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 open_browser = args.is_present("open");
let address = format!("{}:{}", hostname, port); let address = format!("{}:{}", hostname, port);

View File

@ -70,6 +70,8 @@ pub struct Config {
pub book: BookConfig, pub book: BookConfig,
/// Information about the build environment. /// Information about the build environment.
pub build: BuildConfig, pub build: BuildConfig,
/// Information about the serve environment
pub serve: ServeConfig,
/// Information about Rust language support. /// Information about Rust language support.
pub rust: RustConfig, pub rust: RustConfig,
rest: Value, rest: Value,
@ -289,6 +291,7 @@ impl Default for Config {
Config { Config {
book: BookConfig::default(), book: BookConfig::default(),
build: BuildConfig::default(), build: BuildConfig::default(),
serve: ServeConfig::default(),
rust: RustConfig::default(), rust: RustConfig::default(),
rest: Value::Table(Table::default()), rest: Value::Table(Table::default()),
} }
@ -333,6 +336,12 @@ impl<'de> Deserialize<'de> for Config {
.transpose()? .transpose()?
.unwrap_or_default(); .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 let rust: RustConfig = table
.remove("rust") .remove("rust")
.map(|rust| rust.try_into().map_err(D::Error::custom)) .map(|rust| rust.try_into().map_err(D::Error::custom))
@ -342,6 +351,7 @@ impl<'de> Deserialize<'de> for Config {
Ok(Config { Ok(Config {
book, book,
build, build,
serve,
rust, rust,
rest: Value::Table(table), 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<String>,
/// Default port
pub port: Option<String>,
}
/// Configuration for the Rust compiler(e.g., for playground) /// Configuration for the Rust compiler(e.g., for playground)
#[derive(Debug, Default, Clone, PartialEq, Serialize, Deserialize)] #[derive(Debug, Default, Clone, PartialEq, Serialize, Deserialize)]
#[serde(default, rename_all = "kebab-case")] #[serde(default, rename_all = "kebab-case")]