Skip to content

Commit f8d2bff

Browse files
committed
Add PORT_env().
1 parent 6ca6389 commit f8d2bff

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

Readme.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ Functions Expressions Impls Traits Methods Dependency
147147
│ [build-dependencies]
148148
0/0 0/0 0/0 0/0 0/0 ❓ │ └── autocfg 1.1.0
149149
0/0 2/4 0/0 0/0 0/0 ☢️ │ ├── async-io 1.13.0
150+
│ │ [build-dependencies]
151+
0/0 0/0 0/0 0/0 0/0 ❓ │ │ └── autocfg 1.1.0
150152
4/4 91/91 16/16 0/0 1/1 ☢️ │ │ ├── async-lock 2.7.0
151153
0/0 0/0 0/0 0/0 0/0 ❓ │ │ ├── cfg-if 1.0.0
152154
0/0 168/168 2/2 0/0 1/1 ☢️ │ │ ├── concurrent-queue 2.2.0
@@ -172,13 +174,11 @@ Functions Expressions Impls Traits Methods Dependency
172174
│ │ │ [build-dependencies]
173175
0/1 0/201 0/2 0/0 0/4 ❓ │ │ │ └── cc 1.0.79
174176
0/0 24/24 0/0 0/0 3/3 ☢️ │ │ ├── slab 0.4.8
175-
0/0 5/5 0/0 0/0 0/0 ☢️ │ │ │ └── serde 1.0.159
176177
│ │ │ [build-dependencies]
177178
0/0 0/0 0/0 0/0 0/0 ❓ │ │ │ └── autocfg 1.1.0
179+
0/0 5/5 0/0 0/0 0/0 ☢️ │ │ │ └── serde 1.0.159
178180
3/6 540/673 2/4 0/0 3/4 ☢️ │ │ ├── socket2 0.4.9
179181
0/0 21/21 0/0 0/0 4/4 ☢️ │ │ └── waker-fn 1.1.0
180-
│ │ [build-dependencies]
181-
0/0 0/0 0/0 0/0 0/0 ❓ │ │ └── autocfg 1.1.0
182182
0/0 28/28 4/4 0/0 0/0 ☢️ │ ├── blocking 1.3.1
183183
0/0 0/0 0/0 0/0 0/0 ❓ │ └── futures-lite 1.13.0
184184
0/0 0/0 0/0 0/0 0/0 🔒 ├── fixed-buffer 0.5.0
@@ -244,6 +244,7 @@ See [rust-webserver-comparison.md](https://github.com/mleonhard/servlin/blob/mai
244244
- `Response::not_implemented_501()`
245245
- `Response::service_unavailable_503()`
246246
- `EventSender::is_connected()`
247+
- `PORT_env()`
247248
- v0.1.1 - Add `EventSender::unconnected`.
248249
- v0.1.0 - Rename library to Servlin.
249250

src/accept.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,24 @@ pub fn socket_addr_all_interfaces(port: u16) -> SocketAddr {
2121
SocketAddr::new(IpAddr::V6(Ipv6Addr::UNSPECIFIED), port)
2222
}
2323

24+
/// Reads and parses the `PORT` environment variable.
25+
///
26+
/// # Panics
27+
/// Panics with a useful error message when the PORT env var is not an integer in 1..65536.
28+
#[allow(non_snake_case)]
29+
#[must_use]
30+
pub fn PORT_env() -> u16 {
31+
let port: u16 = std::env::var("PORT")
32+
.expect("PORT env var")
33+
.parse()
34+
.expect("failed parsing PORT env var");
35+
if (1..=65535).contains(&port) {
36+
port
37+
} else {
38+
panic!("PORT env var is out of range 1-65535: {port}")
39+
}
40+
}
41+
2442
/// # Errors
2543
/// Returns an error when we fail to bind to the address.
2644
pub async fn listen_127_0_0_1_any_port() -> Result<async_net::TcpListener, std::io::Error> {

src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@
110110
//! - `Response::not_implemented_501()`
111111
//! - `Response::service_unavailable_503()`
112112
//! - `EventSender::is_connected()`
113+
//! - `PORT_env()`
113114
//! - v0.1.1 - Add `EventSender::unconnected`.
114115
//! - v0.1.0 - Rename library to Servlin.
115116
//!
@@ -143,7 +144,7 @@ mod token_set;
143144
mod util;
144145

145146
pub use crate::accept::{
146-
socket_addr_127_0_0_1, socket_addr_127_0_0_1_any_port, socket_addr_all_interfaces,
147+
socket_addr_127_0_0_1, socket_addr_127_0_0_1_any_port, socket_addr_all_interfaces, PORT_env,
147148
};
148149
pub use crate::ascii_string::AsciiString;
149150
pub use crate::body_async_reader::BodyAsyncReader;

0 commit comments

Comments
 (0)