diff --git a/Cargo.lock b/Cargo.lock index c360401..e314025 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -897,6 +897,25 @@ dependencies = [ "libc", ] +[[package]] +name = "is-docker" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "928bae27f42bc99b60d9ac7334e3a21d10ad8f1835a4e12ec3ec0464765ed1b3" +dependencies = [ + "once_cell", +] + +[[package]] +name = "is-wsl" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "173609498df190136aa7dea1a91db051746d339e18476eed5ca40521f02d7aa5" +dependencies = [ + "is-docker", + "once_cell", +] + [[package]] name = "is_terminal_polyfill" version = "1.70.1" @@ -1142,6 +1161,17 @@ version = "1.70.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a4895175b425cb1f87721b59f0f286c2092bd4af812243672510e1ac53e2e0ad" +[[package]] +name = "open" +version = "5.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2483562e62ea94312f3576a7aca397306df7990b8d89033e18766744377ef95" +dependencies = [ + "is-wsl", + "libc", + "pathdiff", +] + [[package]] name = "openssl-probe" version = "0.1.6" @@ -1170,6 +1200,12 @@ dependencies = [ "windows-targets 0.52.6", ] +[[package]] +name = "pathdiff" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df94ce210e5bc13cb6651479fa48d14f601d9858cfe0467f43ae157023b938d3" + [[package]] name = "payphone" version = "4.0.0" @@ -1184,6 +1220,7 @@ dependencies = [ "log", "notify", "once_cell", + "open", "rand", "rand_chacha", "scc", diff --git a/Cargo.toml b/Cargo.toml index 6c31d84..9d2df78 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,6 +14,7 @@ linkify = "0.10.0" log = { version = "0.4.27", features = ["serde"] } notify = "8.2.0" once_cell = "1.21.3" +open = "5.3.2" rand = "0.9.2" rand_chacha = { version = "0.9.0", features = ["os_rng"] } scc = "2.3.4" diff --git a/README.md b/README.md index c4e03b6..80ceb53 100644 --- a/README.md +++ b/README.md @@ -5,5 +5,5 @@ To read the documentation, run ```shell $ git clone --branch rewrite https://git.nyeki.dev/payphone/payphone.git $ cd payphone -$ cargo run -- docs +$ cargo run -- docs --open ``` diff --git a/src/cli.rs b/src/cli.rs index a4139c0..35c6bee 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -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, }, } diff --git a/src/main.rs b/src/main.rs index bf544e3..835ba1a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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!"); }