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();
18static FAVICON_MIME: OnceLock<&'static str> = OnceLock::new();
19
20/// Returns the configured hostname, or `"index"` if none was provided.
21pub fn hostname() -> &'static str {
22    HOSTNAME.get().map_or("index", |s| s.as_str())
23}
24
25/// Returns the configured SSH prefix, or an empty string if none was provided.
26pub fn ssh_prefix() -> &'static str {
27    SSH_PREFIX.get().map_or("", |s| s.as_str())
28}
29
30/// Returns the configured base URL path prefix, or an empty string if none was
31/// provided.
32pub fn base_url() -> &'static str {
33    BASE_URL.get().map_or("", |s| s.as_str())
34}
35
36/// Returns the favicon bytes loaded at startup.
37pub fn favicon() -> &'static [u8] {
38    FAVICON.get().map_or(&[], |v| v.as_slice())
39}
40
41/// Returns the MIME type of the favicon.
42pub fn favicon_mime_type() -> &'static str {
43    FAVICON_MIME.get().copied().unwrap_or("image/svg+xml")
44}
45
46/// Sets the hostname shown in breadcrumbs and clone URLs.
47/// Silently skips `None`.
48pub fn set_hostname(v: Option<String>) {
49    if let Some(v) = v {
50        let _ = HOSTNAME.set(v);
51    }
52}
53
54/// Sets the path prefix used in SSH clone URLs.  Silently skips `None`.
55pub fn set_ssh_prefix(v: Option<String>) {
56    if let Some(v) = v {
57        let _ = SSH_PREFIX.set(v);
58    }
59}
60
61/// Sets the URL path prefix for reverse-proxy deployments.
62/// Silently skips `None`.
63pub fn set_base_url(v: Option<String>) {
64    if let Some(v) = v {
65        let _ = BASE_URL.set(v);
66    }
67}
68
69/// Sets the favicon bytes and MIME type served at `/favicon.ico`.
70pub fn set_favicon(v: Vec<u8>, mime: &'static str) {
71    let _ = FAVICON.set(v);
72    let _ = FAVICON_MIME.set(mime);
73}