Checkpoint
This commit is contained in:
+21
-10
@@ -100,7 +100,12 @@
|
||||
//! $ cargo run -- docs --open
|
||||
//! ```
|
||||
|
||||
use std::{net::SocketAddr, path::Path, process::Command, sync::mpsc};
|
||||
use std::{
|
||||
net::{IpAddr, Ipv4Addr, SocketAddr},
|
||||
path::Path,
|
||||
process::Command,
|
||||
sync::mpsc,
|
||||
};
|
||||
|
||||
use axum::{Router, response::Redirect, routing};
|
||||
use clap::Parser;
|
||||
@@ -150,8 +155,8 @@ pub async fn main() {
|
||||
} => {
|
||||
start(token, database_addr).await;
|
||||
}
|
||||
Commands::Docs { port, open } => {
|
||||
docs(port, open).await;
|
||||
Commands::Docs { bind, open } => {
|
||||
docs(bind, open).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -190,7 +195,7 @@ async fn start(token: String, database_addr: String) {
|
||||
bot.run(&token).await.expect("Could not run bot!");
|
||||
}
|
||||
|
||||
async fn docs(port: u16, open_on_start: bool) {
|
||||
async fn docs(bind: SocketAddr, open_on_start: bool) {
|
||||
log::info!("Compiling documentation...");
|
||||
|
||||
Command::new("cargo")
|
||||
@@ -229,7 +234,7 @@ async fn docs(port: u16, open_on_start: bool) {
|
||||
}
|
||||
});
|
||||
|
||||
log::info!("Starting documentation server on port {port}...");
|
||||
log::info!("Starting documentation server on address {bind}...");
|
||||
|
||||
let docs: Router<()> = Router::new()
|
||||
.route(
|
||||
@@ -241,11 +246,17 @@ async fn docs(port: u16, open_on_start: bool) {
|
||||
"/target/doc/"
|
||||
)));
|
||||
|
||||
let listener = TcpListener::bind(SocketAddr::from(([0, 0, 0, 0], port)))
|
||||
.await
|
||||
.expect("Could not bind to port!");
|
||||
let openable_address = if bind.ip() == IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)) {
|
||||
format!("http://localhost:{}", bind.port())
|
||||
} else {
|
||||
format!("http://{bind}/")
|
||||
};
|
||||
|
||||
log::info!("Documentation server is running on http://localhost:{port}/");
|
||||
let listener = TcpListener::bind(bind)
|
||||
.await
|
||||
.expect("Could not bind to address!");
|
||||
|
||||
log::info!("Documentation server is running on {openable_address}");
|
||||
|
||||
let handle = tokio::spawn(async move {
|
||||
axum::serve(listener, docs)
|
||||
@@ -254,7 +265,7 @@ async fn docs(port: u16, open_on_start: bool) {
|
||||
});
|
||||
|
||||
if open_on_start {
|
||||
match open::that(format!("http://localhost:{port}/")) {
|
||||
match open::that(openable_address) {
|
||||
Ok(_) => log::info!("Opened documentation in browser!"),
|
||||
Err(e) => log::error!("Failed to open documentation in browser: {e}"),
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user