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
+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!");
}