Mountain/Binary/Initialize/PortSelector.rs
1//! # PortSelector
2//!
3//! Selects an unused port for the localhost server.
4//!
5//! ## RESPONSIBILITIES
6//!
7//! ### Port Selection
8//! - Find an unused port on localhost
9//! - Return port number for server binding
10//! - Handle port selection errors
11//!
12//! ## ARCHITECTURAL ROLE
13//!
14//! ### Position in Mountain
15//! - Early initialization component in Binary subsystem
16//! - Provides port for localhost server
17//!
18//! ### Dependencies
19//! - portpicker: Port selection utility
20//!
21//! ### Dependents
22//! - Fn() main entry point: Uses selected port
23//!
24//! ## SECURITY
25//!
26//! ### Considerations
27//! - Ensure port is not previously bound by other services
28//!
29//! ## PERFORMANCE
30//!
31//! ### Considerations
32//! - Port selection should be fast
33//! - Consider port caching for development
34
35/// Select an unused port for the localhost server.
36///
37/// Finds an available port on localhost and returns it.
38///
39/// # Returns
40///
41/// Returns the selected port number.
42///
43/// # Panics
44///
45/// Panics if port selection fails.
46pub fn Select() -> u16 {
47 portpicker::pick_unused_port().expect("FATAL: Failed to find a free port for Localhost Server")
48}
49
50/// Build localhost URL from port number.
51///
52/// Creates a localhost URL from the provided port number.
53///
54/// # Arguments
55///
56/// * `Port` - Port number
57///
58/// # Returns
59///
60/// Returns the localhost URL string.
61pub fn BuildUrl(Port:u16) -> String { format!("http://localhost:{}", Port) }