Rust library to generate static websites
5
fork

Configure Feed

Select the types of activity you want to include in your feed.

fix(cli): Show proper address and port for dev server

+68 -23
+38
Cargo.lock
··· 1637 1637 checksum = "d26c52dbd32dccf2d10cac7725f8eae5296885fb5703b261f7d0a0739ec807ab" 1638 1638 1639 1639 [[package]] 1640 + name = "local-ip-address" 1641 + version = "0.6.3" 1642 + source = "registry+https://github.com/rust-lang/crates.io-index" 1643 + checksum = "3669cf5561f8d27e8fc84cc15e58350e70f557d4d65f70e3154e54cd2f8e1782" 1644 + dependencies = [ 1645 + "libc", 1646 + "neli", 1647 + "thiserror 1.0.69", 1648 + "windows-sys 0.59.0", 1649 + ] 1650 + 1651 + [[package]] 1640 1652 name = "lock_api" 1641 1653 version = "0.4.12" 1642 1654 source = "registry+https://github.com/rust-lang/crates.io-index" ··· 1755 1767 "futures-util", 1756 1768 "http-body-util", 1757 1769 "inquire", 1770 + "local-ip-address", 1758 1771 "log", 1759 1772 "rand 0.9.0", 1760 1773 "serde", ··· 2172 2185 "log", 2173 2186 "wasi 0.11.0+wasi-snapshot-preview1", 2174 2187 "windows-sys 0.52.0", 2188 + ] 2189 + 2190 + [[package]] 2191 + name = "neli" 2192 + version = "0.6.5" 2193 + source = "registry+https://github.com/rust-lang/crates.io-index" 2194 + checksum = "93062a0dce6da2517ea35f301dfc88184ce18d3601ec786a727a87bf535deca9" 2195 + dependencies = [ 2196 + "byteorder", 2197 + "libc", 2198 + "log", 2199 + "neli-proc-macros", 2200 + ] 2201 + 2202 + [[package]] 2203 + name = "neli-proc-macros" 2204 + version = "0.1.4" 2205 + source = "registry+https://github.com/rust-lang/crates.io-index" 2206 + checksum = "0c8034b7fbb6f9455b2a96c19e6edf8dc9fc34c70449938d8ee3b4df363f61fe" 2207 + dependencies = [ 2208 + "either", 2209 + "proc-macro2", 2210 + "quote", 2211 + "serde", 2212 + "syn 1.0.109", 2175 2213 ] 2176 2214 2177 2215 [[package]]
+1
crates/cli/Cargo.toml
··· 36 36 ureq = "3.0.5" 37 37 tar = "0.4.43" 38 38 toml_edit = "0.22.23" 39 + local-ip-address = "0.6.3"
+2 -2
crates/cli/src/dev.rs
··· 17 17 18 18 use crate::logging::format_elapsed_time; 19 19 20 - pub async fn start_dev_env(cwd: &str) -> io::Result<()> { 20 + pub async fn start_dev_env(cwd: &str, host: bool) -> io::Result<()> { 21 21 info!(name: "dev", "Preparing dev environment…"); 22 22 let (sender_websocket, _) = broadcast::channel::<WebSocketMessage>(100); 23 23 24 24 let web_server_thread: tokio::task::JoinHandle<()> = 25 - tokio::spawn(server::start_dev_web_server(sender_websocket.clone())); 25 + tokio::spawn(server::start_dev_web_server(sender_websocket.clone(), host)); 26 26 27 27 let wx = Watchexec::new_async(move |mut action| { 28 28 Box::new({
+21 -18
crates/cli/src/dev/server.rs
··· 25 25 use futures::{stream::StreamExt, SinkExt}; 26 26 27 27 use crate::logging::{format_elapsed_time, FormatElapsedTimeOptions}; 28 + use local_ip_address::local_ip; 28 29 29 30 #[derive(Clone, Debug)] 30 31 pub struct WebSocketMessage { ··· 36 37 tx: broadcast::Sender<WebSocketMessage>, 37 38 } 38 39 39 - pub async fn start_dev_web_server(tx: broadcast::Sender<WebSocketMessage>) { 40 + pub async fn start_dev_web_server(tx: broadcast::Sender<WebSocketMessage>, host: bool) { 40 41 let start_time = std::time::Instant::now(); 41 42 async fn handle_404() -> (StatusCode, &'static str) { 42 43 (StatusCode::NOT_FOUND, "Not found") ··· 61 62 .with_state(AppState { tx }); 62 63 63 64 // run it with hyper, if --host 0.0.0.0 otherwise localhost 64 - let addr = if std::env::args().any(|arg| arg == "--host") { 65 - "0.0.0.0" 66 - } else { 67 - "localhost" 68 - }; 69 - let port = 3000; 70 - let listener = tokio::net::TcpListener::bind(format!("{}:{}", addr, port)) 65 + let addr = if host { "0.0.0.0" } else { "localhost" }; 66 + let listener = tokio::net::TcpListener::bind(format!("{}:{}", addr, 0)) 71 67 .await 72 68 .unwrap(); 73 69 74 70 debug!("listening on {}", listener.local_addr().unwrap()); 75 71 76 - log_server_start(start_time); 72 + log_server_start(start_time, host, listener.local_addr().unwrap()); 77 73 78 74 axum::serve( 79 75 listener, ··· 84 80 .unwrap(); 85 81 } 86 82 87 - fn log_server_start(start_time: std::time::Instant) { 83 + fn log_server_start(start_time: std::time::Instant, host: bool, addr: SocketAddr) { 88 84 info!(name: "SKIP_FORMAT", ""); 89 85 let elasped_time = format_elapsed_time(Ok(start_time.elapsed()), &Default::default()).unwrap(); 90 86 info!(name: "SKIP_FORMAT", "{} {}", "Maudit 👑".bold().bright_red(), format!("{} {}", "server started in".dimmed(), elasped_time)); 91 87 info!(name: "SKIP_FORMAT", ""); 92 88 93 - let url = "\x1b]8;;http://localhost:3000\x1b\\http://localhost:3000\x1b]8;;\x1b\\" 94 - .bold() 95 - .underline() 96 - .bright_blue(); 97 - let network_url = "\x1b]8;;http://192.168.0.1:3000\x1b\\http://192.168.0.1:3000\x1b]8;;\x1b\\" 98 - .bold() 99 - .underline() 100 - .bright_magenta(); 89 + let port = addr.port(); 90 + let url = 91 + format!("\x1b]8;;http://localhost:{port}\x1b\\http://localhost:{port}\x1b]8;;\x1b\\",) 92 + .bold() 93 + .underline() 94 + .bright_blue(); 95 + let network_url = if host { 96 + let local_ip = local_ip().unwrap(); 97 + format!("\x1b]8;;http://{local_ip}:{port}\x1b\\http://{local_ip}:{port}\x1b]8;;\x1b\\") 98 + .bold() 99 + .underline() 100 + .bright_magenta() 101 + } else { 102 + "Use --host to expose the server to your network".dimmed() 103 + }; 101 104 info!(name: "SKIP_FORMAT", "🮔 {} {}", "Local".bold(), url); 102 105 info!(name: "SKIP_FORMAT", "🮔 {} {}", "Network".bold(), network_url); 103 106 info!(name: "SKIP_FORMAT", "");
+6 -3
crates/cli/src/main.rs
··· 29 29 /// Build the project 30 30 Build, 31 31 /// Run the project in development mode 32 - Dev, 32 + Dev { 33 + #[clap(long)] 34 + host: bool, 35 + }, 33 36 /// Preview the project 34 37 Preview, 35 38 } ··· 61 64 62 65 let _ = start_preview_web_server(PathBuf::from("dist")).await; 63 66 } 64 - Commands::Dev {} => { 65 - let _ = start_dev_env(".").await; 67 + Commands::Dev { host } => { 68 + let _ = start_dev_env(".", *host).await; 66 69 } 67 70 } 68 71 }