Skip to content

Commit

Permalink
Find available port for screenshot server (#417)
Browse files Browse the repository at this point in the history
Signed-off-by: Sergio Castaño Arteaga <[email protected]>
  • Loading branch information
tegioz authored Dec 22, 2023
1 parent 00938b2 commit 4d17359
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/build/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ use std::{
ffi::OsStr,
fs::{self, File},
io::Write,
net::TcpListener,
path::{Path, PathBuf},
sync::Arc,
time::{Duration, Instant},
Expand Down Expand Up @@ -549,11 +550,15 @@ async fn prepare_screenshot(width: u32, output_dir: &Path) -> Result<()> {
}

// Launch server to serve landscape just built
const SVR_ADDR: &str = "127.0.0.1:8123";
let Some(port) = find_available_port() else {
return Err(format_err!("could not find an available port to listen on"));
};
let svr_addr = format!("127.0.0.1:{port}");
let svr_addr_copy = svr_addr.clone();
let landscape_dir = Some(PathBuf::from(&output_dir));
let server = tokio::spawn(async {
let args = ServeArgs {
addr: SVR_ADDR.to_string(),
addr: svr_addr_copy,
graceful_shutdown: false,
landscape_dir,
silent: true,
Expand All @@ -577,7 +582,7 @@ async fn prepare_screenshot(width: u32, output_dir: &Path) -> Result<()> {
let browser = Browser::new(options)?;
let tab = browser.new_tab()?;
tab.set_default_timeout(TIMEOUT);
let screenshot_url = format!("http://{SVR_ADDR}/screenshot");
let screenshot_url = format!("http://{svr_addr}/screenshot");
tab.navigate_to(&screenshot_url)?.wait_until_navigated()?;

// Take screenshot in PNG format and save it to a file
Expand Down Expand Up @@ -671,6 +676,11 @@ fn setup_output_dir(output_dir: &Path) -> Result<()> {
Ok(())
}

/// Find an available port to listen on.
fn find_available_port() -> Option<u16> {
(9000..10000).find(|port| TcpListener::bind(("127.0.0.1", *port)).is_ok())
}

mod filters {
use askama_escape::JsonEscapeBuffer;
use serde::Serialize;
Expand Down

0 comments on commit 4d17359

Please sign in to comment.