Parent [>]
1//! Global application configuration set once at startup.
2//!
3//! Values are stored in `OnceLock` statics and accessed via the corresponding
4//! getter functions.  All setters accept `Option` and silently skip `None`.
5
6use std::sync::OnceLock;
7
8/// Package name read from `Cargo.toml` at compile time.
9pub const PKG_NAME: &str = env!("CARGO_PKG_NAME");
10
11/// Package version read from `Cargo.toml` at compile time.
12pub const PKG_VERSION: &str = env!("CARGO_PKG_VERSION");
13
14static HOSTNAME: OnceLock<String> = OnceLock::new();
15static SSH_PREFIX: OnceLock<String> = OnceLock::new();
16static BASE_URL: OnceLock<String> = OnceLock::new();
17static FAVICON: OnceLock<Vec<u8>> = OnceLock::new();
18
19/// Returns the configured hostname, or `"index"` if none was provided.
20pub fn hostname() -> &'static str {
21    HOSTNAME.get().map_or("index", |s| s.as_str())
22}
23
24/// Returns the configured SSH prefix, or an empty string if none was provided.
25pub fn ssh_prefix() -> &'static str {
26    SSH_PREFIX.get().map_or("", |s| s.as_str())
27}
28
29/// Returns the configured base URL path prefix, or an empty string if none was
30/// provided.
31pub fn base_url() -> &'static str {
32    BASE_URL.get().map_or("", |s| s.as_str())
33}
34
35/// Returns the favicon bytes loaded at startup.
36pub fn favicon() -> &'static [u8] {
37    FAVICON.get().map_or(&[], |v| v.as_slice())
38}
39
40/// Sets the hostname shown in breadcrumbs and clone URLs.
41/// Silently skips `None`.
42pub fn set_hostname(v: Option<String>) {
43    if let Some(v) = v {
44        let _ = HOSTNAME.set(v);
45    }
46}
47
48/// Sets the path prefix used in SSH clone URLs.  Silently skips `None`.
49pub fn set_ssh_prefix(v: Option<String>) {
50    if let Some(v) = v {
51        let _ = SSH_PREFIX.set(v);
52    }
53}
54
55/// Sets the URL path prefix for reverse-proxy deployments.
56/// Silently skips `None`.
57pub fn set_base_url(v: Option<String>) {
58    if let Some(v) = v {
59        let _ = BASE_URL.set(v);
60    }
61}
62
63/// Sets the favicon bytes served at `/favicon.ico`.
64pub fn set_favicon(v: Vec<u8>) {
65    let _ = FAVICON.set(v);
66}