Skip to main content

cloudpub_common/
lib.rs

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