Add --open flag support to the docs server

This commit is contained in:
2025-08-11 01:22:00 -03:00
parent 7bb3a3387a
commit 84e3b533f0
5 changed files with 82 additions and 15 deletions
+18
View File
@@ -37,6 +37,14 @@
//!
//! let args = Args::parse();
//! ```
//!
//! # Running the Bot
//!
//! To start the bot, run:
//!
//! ```shell
//! $ cargo run -- start [TOKEN]
//! ```
/// Payphone's argument parser.
#[derive(clap::Parser)]
@@ -130,5 +138,15 @@ pub enum Commands {
default_value_t = 3000
)]
port: u16,
#[arg(
long,
short,
env = "PAYPHONE_DOCS_OPEN",
value_name = "OPEN",
default_value_t = false,
help = "Open the documentation in the default browser after starting the server."
)]
open: bool,
},
}
+25 -14
View File
@@ -22,19 +22,19 @@
//!
//! To stop the database cluster at any time, run `docker compose down`. To stop it and delete the
//! volumes (resetting the database), run `docker compose down -v`.
//!
//!
//! Before booting up scylla, you'll need to set the `aio-max-nr` kernel parameter to `1048576` or
//! higher. You can check [Scylla's documentation](https://hub.docker.com/r/scylladb/scylla) for
//! the math of the value at which you'll need to set the parameter. In linux, you can set it by
//! running:
//!
//!
//! ```shell
//! $ sudo sysctl -w fs.aio-max-nr=1048576
//! ```
//!
//!
//! That will set it up for your current session. To make it persistent across reboots, add the
//! following to your `/etc/sysctl.conf` file (or to a new file in `/etc/sysctl.d/`):
//!
//!
//! ```conf
//! fs.aio-max-nr = 1048576
//! ```
@@ -90,14 +90,14 @@
//! consistency and readability.
//!
//! More documentation about each group can be found in the respective submodule's documentation.
//!
//!
//! # Documentation Server
//!
//!
//! Payphone has a built-in documentation server that serves the project's documentation and refreshes
//! it on changes. To start the documentation server, run the following command:
//!
//!
//! ```shell
//! $ cargo run -- docs
//! $ cargo run -- docs --open
//! ```
use std::{net::SocketAddr, path::Path, process::Command, sync::mpsc};
@@ -150,8 +150,8 @@ pub async fn main() {
} => {
start(token, database_addr).await;
}
Commands::Docs { port } => {
docs(port).await;
Commands::Docs { port, open } => {
docs(port, open).await;
}
}
}
@@ -190,7 +190,7 @@ async fn start(token: String, database_addr: String) {
bot.run(&token).await.expect("Could not run bot!");
}
async fn docs(port: u16) {
async fn docs(port: u16, open_on_start: bool) {
log::info!("Compiling documentation...");
Command::new("cargo")
@@ -247,7 +247,18 @@ async fn docs(port: u16) {
log::info!("Documentation server is running on http://localhost:{port}/");
axum::serve(listener, docs)
.await
.expect("Could not start documentation server!");
let handle = tokio::spawn(async move {
axum::serve(listener, docs)
.await
.expect("Could not start documentation server!")
});
if open_on_start {
match open::that(format!("http://localhost:{port}/")) {
Ok(_) => log::info!("Opened documentation in browser!"),
Err(e) => log::error!("Failed to open documentation in browser: {e}"),
}
}
handle.await.expect("Documentation server task failed!");
}