Skip to main content

cloudpub_common/
lib.rs

1pub mod config;
2pub mod constants;
3pub mod data;
4pub mod fair_channel;
5pub mod lease;
6pub mod logging;
7pub mod protocol;
8pub mod proxy_protocol;
9pub mod routing;
10pub mod transport;
11pub mod unix_tcp;
12pub mod utils;
13
14include!(concat!(env!("OUT_DIR"), "/build-vars.rs"));
15
16use std::sync::LazyLock;
17
18/// Server domain; baked profile value, overridable at runtime via CLOUDPUB_DOMAIN.
19pub static DOMAIN: LazyLock<String> = LazyLock::new(|| {
20    std::env::var("CLOUDPUB_DOMAIN").unwrap_or_else(|_| DEFAULT_DOMAIN.to_string())
21});
22
23/// Public HTTPS port; baked profile value, overridable at runtime via CLOUDPUB_PORT.
24pub static PORT: LazyLock<u16> = LazyLock::new(|| {
25    std::env::var("CLOUDPUB_PORT")
26        .ok()
27        .and_then(|v| v.parse().ok())
28        .unwrap_or(DEFAULT_PORT)
29});
30
31/// Site display name; baked profile value, overridable at runtime via CLOUDPUB_SITE_NAME.
32pub static SITE_NAME: LazyLock<String> = LazyLock::new(|| {
33    std::env::var("CLOUDPUB_SITE_NAME").unwrap_or_else(|_| DEFAULT_SITE_NAME.to_string())
34});
35
36/// Free-tier traffic limit in bytes; 0 disables the check.
37/// Baked profile value, overridable at runtime via CLOUDPUB_TRAFFIC_LIMIT.
38#[cfg(target_pointer_width = "64")]
39pub static TRAFFIC_LIMIT: LazyLock<usize> = LazyLock::new(|| {
40    std::env::var("CLOUDPUB_TRAFFIC_LIMIT")
41        .ok()
42        .and_then(|v| v.parse().ok())
43        .unwrap_or(DEFAULT_TRAFFIC_LIMIT)
44});
45
46#[cfg(feature = "rustls")]
47pub use rustls_pemfile;
48#[cfg(feature = "rustls")]
49pub use tokio_rustls;
50pub use {prost, serde_json, tokio_tungstenite};
51
52#[cfg(test)]
53mod build_var_tests {
54    // Single test for all statics: LazyLock initializes once per process,
55    // so the env must be set before the first access
56    #[test]
57    fn runtime_env_overrides() {
58        std::env::set_var("CLOUDPUB_DOMAIN", "override.example");
59        std::env::set_var("CLOUDPUB_PORT", "8443");
60        std::env::set_var("CLOUDPUB_SITE_NAME", "Override");
61        std::env::set_var("CLOUDPUB_TRAFFIC_LIMIT", "12345");
62        assert_eq!(super::DOMAIN.as_str(), "override.example");
63        assert_eq!(*super::PORT, 8443);
64        assert_eq!(super::SITE_NAME.as_str(), "Override");
65        assert_eq!(*super::TRAFFIC_LIMIT, 12345);
66    }
67}