Add docs server

This commit is contained in:
2025-08-10 22:50:55 -03:00
parent 1f002cb7ca
commit edeaa1335f
5 changed files with 419 additions and 27 deletions
Generated
+270 -6
View File
@@ -144,6 +144,60 @@ version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8"
[[package]]
name = "axum"
version = "0.8.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "021e862c184ae977658b36c4500f7feac3221ca5da43e3f25bd04ab6c79a29b5"
dependencies = [
"axum-core",
"bytes",
"form_urlencoded",
"futures-util",
"http",
"http-body",
"http-body-util",
"hyper",
"hyper-util",
"itoa",
"matchit",
"memchr",
"mime",
"percent-encoding",
"pin-project-lite",
"rustversion",
"serde",
"serde_json",
"serde_path_to_error",
"serde_urlencoded",
"sync_wrapper",
"tokio",
"tower",
"tower-layer",
"tower-service",
"tracing",
]
[[package]]
name = "axum-core"
version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "68464cd0412f486726fb3373129ef5d2993f90c34bc2bc1c1e9943b2f4fc7ca6"
dependencies = [
"bytes",
"futures-core",
"http",
"http-body",
"http-body-util",
"mime",
"pin-project-lite",
"rustversion",
"sync_wrapper",
"tower-layer",
"tower-service",
"tracing",
]
[[package]]
name = "backtrace"
version = "0.3.75"
@@ -165,6 +219,12 @@ version = "0.22.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6"
[[package]]
name = "bitflags"
version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "bitflags"
version = "2.9.1"
@@ -442,6 +502,24 @@ version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9c4f5dac5e15c24eb999c26181a6ca40b39fe946cbe4c263c7209467bc83af2"
[[package]]
name = "form_urlencoded"
version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e13624c2627564efccf4934284bdd98cbaa14e79b0b5a141218e507b3a823456"
dependencies = [
"percent-encoding",
]
[[package]]
name = "fsevent-sys"
version = "4.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "76ee7a02da4d231650c7cea31349b889be2f45ddb3ef3032d2ec8185f6313fd2"
dependencies = [
"libc",
]
[[package]]
name = "futures"
version = "0.3.31"
@@ -652,12 +730,24 @@ dependencies = [
"pin-project-lite",
]
[[package]]
name = "http-range-header"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9171a2ea8a68358193d15dd5d70c1c10a2afc3e7e4c5bc92bc9f025cebd7359c"
[[package]]
name = "httparse"
version = "1.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6dbf3de79e51f3d586ab4cb9d5c3e2c14aa28ed23d180cf89b4df0454a69cc87"
[[package]]
name = "httpdate"
version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9"
[[package]]
name = "hyper"
version = "1.6.0"
@@ -671,6 +761,7 @@ dependencies = [
"http",
"http-body",
"httparse",
"httpdate",
"itoa",
"pin-project-lite",
"smallvec",
@@ -775,13 +866,33 @@ dependencies = [
"hashbrown 0.15.4",
]
[[package]]
name = "inotify"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f37dccff2791ab604f9babef0ba14fbe0be30bd368dc541e2b08d07c8aa908f3"
dependencies = [
"bitflags 2.9.1",
"inotify-sys",
"libc",
]
[[package]]
name = "inotify-sys"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e05c02b5e89bff3b946cedeca278abc628fe811e604f027c45a8aa3cf793d0eb"
dependencies = [
"libc",
]
[[package]]
name = "io-uring"
version = "0.7.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d93587f37623a1a17d94ef2bc9ada592f5465fe7732084ab7beefabe5c77c0c4"
dependencies = [
"bitflags",
"bitflags 2.9.1",
"cfg-if",
"libc",
]
@@ -839,6 +950,26 @@ dependencies = [
"wasm-bindgen",
]
[[package]]
name = "kqueue"
version = "1.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eac30106d7dce88daf4a3fcb4879ea939476d5074a9b7ddd0fb97fa4bed5596a"
dependencies = [
"kqueue-sys",
"libc",
]
[[package]]
name = "kqueue-sys"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed9625ffda8729b85e45cf04090035ac368927b8cebc34898e7c120f52e4838b"
dependencies = [
"bitflags 1.3.2",
"libc",
]
[[package]]
name = "libc"
version = "0.2.174"
@@ -893,12 +1024,34 @@ dependencies = [
"twox-hash",
]
[[package]]
name = "matchit"
version = "0.8.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "47e1ffaa40ddd1f3ed91f717a33c8c0ee23fff369e3aa8772b9605cc1d22f4c3"
[[package]]
name = "memchr"
version = "2.7.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "32a282da65faaf38286cf3be983213fcf1d2e2a58700e808f83f4ea9a4804bc0"
[[package]]
name = "mime"
version = "0.3.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a"
[[package]]
name = "mime_guess"
version = "2.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f7c44f8e672c00fe5308fa235f821cb4198414e1c77935c1ab6948d3fd78550e"
dependencies = [
"mime",
"unicase",
]
[[package]]
name = "miniz_oxide"
version = "0.8.9"
@@ -915,10 +1068,35 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "78bed444cc8a2160f01cbcf811ef18cac863ad68ae8ca62092e8db51d51c761c"
dependencies = [
"libc",
"log",
"wasi 0.11.1+wasi-snapshot-preview1",
"windows-sys 0.59.0",
]
[[package]]
name = "notify"
version = "8.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4d3d07927151ff8575b7087f245456e549fea62edf0ec4e565a5ee50c8402bc3"
dependencies = [
"bitflags 2.9.1",
"fsevent-sys",
"inotify",
"kqueue",
"libc",
"log",
"mio",
"notify-types",
"walkdir",
"windows-sys 0.60.2",
]
[[package]]
name = "notify-types"
version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5e0826a989adedc2a244799e823aece04662b66609d96af8dff7ac6df9a8925d"
[[package]]
name = "num-conv"
version = "0.1.0"
@@ -997,12 +1175,14 @@ name = "payphone"
version = "4.0.0"
dependencies = [
"async-trait",
"axum",
"clap",
"colored",
"dotenvy",
"include_dir",
"linkify",
"log",
"notify",
"once_cell",
"rand",
"rand_chacha",
@@ -1011,6 +1191,7 @@ dependencies = [
"thiserror 2.0.12",
"time",
"tokio",
"tower-http",
"twilight-gateway",
"twilight-http",
"twilight-model",
@@ -1126,7 +1307,7 @@ version = "0.5.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5407465600fb0548f1442edf71dd20683c6ed326200ace4b1ef0763521bb3b77"
dependencies = [
"bitflags",
"bitflags 2.9.1",
]
[[package]]
@@ -1378,7 +1559,7 @@ version = "3.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "271720403f46ca04f7ba6f55d438f8bd878d6b8ca0a1046e8228c4145bcbb316"
dependencies = [
"bitflags",
"bitflags 2.9.1",
"core-foundation",
"core-foundation-sys",
"libc",
@@ -1437,6 +1618,16 @@ dependencies = [
"serde",
]
[[package]]
name = "serde_path_to_error"
version = "0.1.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "59fab13f937fa393d08645bf3a84bdfe86e296747b506ada67bb15f10f218b2a"
dependencies = [
"itoa",
"serde",
]
[[package]]
name = "serde_repr"
version = "0.1.20"
@@ -1448,6 +1639,18 @@ dependencies = [
"syn",
]
[[package]]
name = "serde_urlencoded"
version = "0.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d3491c14715ca2294c4d6a88f15e84739788c1d030eed8c110436aafdaa2f3fd"
dependencies = [
"form_urlencoded",
"itoa",
"ryu",
"serde",
]
[[package]]
name = "sha1_smol"
version = "1.0.1"
@@ -1548,6 +1751,12 @@ dependencies = [
"unicode-ident",
]
[[package]]
name = "sync_wrapper"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0bf256ce5efdfa370213c1dabab5935a12e49f2c58d15e9eac2870d3b4f27263"
[[package]]
name = "synstructure"
version = "0.13.2"
@@ -1707,6 +1916,54 @@ dependencies = [
"tokio-util",
]
[[package]]
name = "tower"
version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d039ad9159c98b70ecfd540b2573b97f7f52c3e8d9f8ad57a24b916a536975f9"
dependencies = [
"futures-core",
"futures-util",
"pin-project-lite",
"sync_wrapper",
"tokio",
"tower-layer",
"tower-service",
"tracing",
]
[[package]]
name = "tower-http"
version = "0.6.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "adc82fd73de2a9722ac5da747f12383d2bfdb93591ee6c58486e0097890f05f2"
dependencies = [
"bitflags 2.9.1",
"bytes",
"futures-core",
"futures-util",
"http",
"http-body",
"http-body-util",
"http-range-header",
"httpdate",
"mime",
"mime_guess",
"percent-encoding",
"pin-project-lite",
"tokio",
"tokio-util",
"tower-layer",
"tower-service",
"tracing",
]
[[package]]
name = "tower-layer"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "121c2a6cda46980bb0fcd1647ffaf6cd3fc79a013de288782836f6df9c48780e"
[[package]]
name = "tower-service"
version = "0.3.3"
@@ -1719,6 +1976,7 @@ version = "0.1.41"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "784e0ac535deb450455cbfa28a6f0df145ea1bb7ae51b821cf5e7927fdcfbdd0"
dependencies = [
"log",
"pin-project-lite",
"tracing-attributes",
"tracing-core",
@@ -1756,7 +2014,7 @@ version = "0.16.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "863ef55467bcf6a2958162766fd0b72f33112e36971b37f9ec09b86d4fd0f7d1"
dependencies = [
"bitflags",
"bitflags 2.9.1",
"fastrand",
"flate2",
"futures-core",
@@ -1822,7 +2080,7 @@ version = "0.16.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "191e2efa051dfbd9bed4c9f6bd3f5e007bda909c687a1db2760371a3d566617d"
dependencies = [
"bitflags",
"bitflags 2.9.1",
"serde",
"serde-value",
"serde_repr",
@@ -1854,6 +2112,12 @@ version = "2.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b907da542cbced5261bd3256de1b3a1bf340a3d37f93425a07362a1d687de56"
[[package]]
name = "unicase"
version = "2.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "75b844d17643ee918803943289730bec8aac480150456169e647ed0b576ba539"
[[package]]
name = "unicode-ident"
version = "1.0.18"
@@ -2319,7 +2583,7 @@ version = "0.39.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6f42320e61fe2cfd34354ecb597f86f413484a798ba44a8ca1165c58d42da6c1"
dependencies = [
"bitflags",
"bitflags 2.9.1",
]
[[package]]
+3
View File
@@ -5,12 +5,14 @@ edition = "2024"
[dependencies]
async-trait = "0.1.88"
axum = "0.8.4"
clap = { version = "4.5.42", features = ["derive", "env", "string"] }
colored = "3.0.0"
dotenvy = "0.15.7"
include_dir = "0.7.4"
linkify = "0.10.0"
log = { version = "0.4.27", features = ["serde"] }
notify = "8.2.0"
once_cell = "1.21.3"
rand = "0.9.2"
rand_chacha = { version = "0.9.0", features = ["os_rng"] }
@@ -19,6 +21,7 @@ scylla = { version = "1.3.1", features = ["time-03"] }
thiserror = "2.0.12"
time = { version = "0.3.41", features = ["formatting", "local-offset", "macros"] }
tokio = { version = "1.47.1", features = ["macros", "rt-multi-thread"] }
tower-http = { version = "0.6.6", features = ["fs"] }
twilight-gateway = "0.16.0"
twilight-http = { version = "0.16.0", features = ["simd-json"] }
twilight-model = "0.16.0"
+55 -18
View File
@@ -23,28 +23,26 @@
//! PAYPHONE_LOGGING="info"
//! PAYPHONE_DATABASE_ADDR="your_database_addr"
//! ```
//!
//!
//! # Parsing Arguments
//!
//!
//! The arguments are parsed using the `Args::parse()` method, which returns an instance of the
//! [`Args`] struct. This struct contains the parsed values of the command line arguments, and
//! can be used to initialize the bot and start the event loop.
//!
//!
//! ```
//! // Read the environment variables from the `.env` file, if it exists, and load them as proper
//! // env variables.
//! dotenvy::dotenv().ok();
//!
//!
//! let args = Args::parse();
//! ```
/// Payphone's argument parser.
#[derive(clap::Parser)]
#[command(name = "Payphone CLI")]
#[command(version, about = "Payphone's command line interface.")]
pub struct Args {
/// The Discord bot token to run the bot with.
#[arg(env = "PAYPHONE_TOKEN")]
pub token: String,
/// The minimum level logs have to be to be outputted into the console, or off for no logs.
#[arg(
long,
@@ -57,28 +55,33 @@ pub struct Args {
)]
pub logging: LevelFilter,
/// The socket address of the database to connect to, a Scylla node.
#[arg(
long,
short,
env = "PAYPHONE_DATABASE_ADDR",
value_name = "ADDR",
default_value_t = String::from("127.0.0.1:9042"))
]
pub database_addr: String,
#[command(subcommand)]
pub command: Option<Commands>,
}
/// An enum with the different logging levels that can be used in the CLI.
///
///
/// This enum is used to parse the `--logging` or `-l` command line argument and then map the value
/// to [`log::LevelFilter`].
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, clap::ValueEnum)]
pub enum LevelFilter {
/// No logs at all.
Off,
/// The most verbose level, used for debugging purposes.
Trace,
/// Events that are useful to be displayed during development, but not in production.
Debug,
/// Used for general information about the bot's state and milestones.
Info,
/// Used for warnings, when something unexpected happens but the bot can still continue
/// running.
Warn,
/// Used for errors, when something goes wrong and the bot cannot continue running.
Error,
}
@@ -95,3 +98,37 @@ impl Into<log::LevelFilter> for LevelFilter {
}
}
}
#[derive(clap::Subcommand, Clone)]
pub enum Commands {
/// Starts the bot with the provided token.
Start {
/// The Discord bot token to run the bot with.
#[arg(env = "PAYPHONE_TOKEN")]
token: String,
/// The socket address of the database to connect to, a Scylla node.
#[arg(
long,
short,
env = "PAYPHONE_DATABASE_ADDR",
value_name = "ADDR",
default_value_t = String::from("127.0.0.1:9042"))
]
database_addr: String,
},
/// Starts a documentation server for the project which serves `cargo doc` and refreshes on
/// changes.
Docs {
/// The port to run the documentation server on.
#[arg(
long,
short,
env = "PAYPHONE_DOCS_PORT",
value_name = "PORT",
default_value_t = 3000
)]
port: u16,
},
}
+4
View File
@@ -1,3 +1,7 @@
//! A framework for building Discord bots, built with Twilight.
//!
//!
pub mod bot;
pub mod commands;
pub mod events;
+87 -3
View File
@@ -75,11 +75,17 @@
//!
//! More documentation about each group can be found in the respective submodule's documentation.
use std::{net::SocketAddr, path::Path, process::Command, sync::mpsc};
use axum::{Router, response::Redirect, routing};
use clap::Parser;
use notify::{Event, EventKind, Watcher};
use tokio::net::TcpListener;
use tower_http::services::ServeDir;
use twilight_gateway::Intents;
use crate::{
cli::Args,
cli::{Args, Commands},
framework::bot::Bot,
groups::{
about::{commands::HelpCommand, components::HelpGoBackButton},
@@ -111,13 +117,29 @@ pub async fn main() {
logging::init(args.logging.into());
if let Some(command) = args.command {
match command {
Commands::Start {
token,
database_addr,
} => {
start(token, database_addr).await;
}
Commands::Docs { port } => {
docs(port).await;
}
}
}
}
async fn start(token: String, database_addr: String) {
log::info!("Welcome to Payphone, Nyeki!");
log::info!(
"Payphone is running on version v{}",
env!("CARGO_PKG_VERSION")
);
let db = database::init(args.database_addr)
let db = database::init(database_addr)
.await
.expect("Could not connect to the database!");
@@ -140,5 +162,67 @@ pub async fn main() {
.on_server_update(ServerUpdateHandler)
.on_server_delete(ServerDeleteHandler);
bot.run(&args.token).await.expect("Could not run bot!");
bot.run(&token).await.expect("Could not run bot!");
}
async fn docs(port: u16) {
log::info!("Compiling documentation...");
Command::new("cargo")
.args(["doc", "--no-deps"])
.output()
.expect("Failed to run `cargo doc`");
tokio::spawn(async {
let (tx, rx) = mpsc::channel::<notify::Result<Event>>();
let mut watcher = notify::recommended_watcher(tx).expect("Failed to create watcher");
watcher
.watch(Path::new("src/"), notify::RecursiveMode::Recursive)
.expect("Failed to watch src directory");
for res in rx {
match res {
Ok(event) => {
match event.kind {
EventKind::Create(_) | EventKind::Modify(_) | EventKind::Remove(_) => {
log::info!("Source files changed, recompiling...");
}
_ => continue,
}
Command::new("cargo")
.args(["doc", "--no-deps"])
.output()
.expect("Failed to run `cargo doc`");
}
Err(e) => {
log::error!("Watch error: {e:?}");
}
}
}
});
log::info!("Starting documentation server on port {port}...");
let docs: Router<()> = Router::new()
.route(
"/",
routing::get(|| async { Redirect::permanent("/payphone/") }),
)
.fallback_service(ServeDir::new(concat!(
env!("CARGO_MANIFEST_DIR"),
"/target/doc/"
)));
let listener = TcpListener::bind(SocketAddr::from(([0, 0, 0, 0], port)))
.await
.expect("Could not bind to port!");
log::info!("Documentation server is running on http://localhost:{port}/");
axum::serve(listener, docs)
.await
.expect("Could not start documentation server!");
}