yggdrasil/utils/
server.rs1#![cfg(feature = "server")]
6
7use sha2::{Digest, Sha256};
8
9pub fn sha256_hex(input: &str) -> String {
13 let mut hasher = Sha256::new();
14 hasher.update(input.as_bytes());
15 hex::encode(hasher.finalize())
16}
17
18pub static EMAIL_REGEX: std::sync::LazyLock<regex::Regex> = std::sync::LazyLock::new(|| {
22 regex::Regex::new(r"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$")
23 .expect("EMAIL_REGEX 正则模式应在编译期通过校验")
24});
25
26pub const MAX_FILE_SIZE: usize = 5 * 1024 * 1024;
30
31pub fn parse_migrate_startup_timeout() -> u64 {
36 std::env::var("MIGRATE_STARTUP_TIMEOUT_SECS")
37 .ok()
38 .and_then(|s| s.parse::<u64>().ok())
39 .unwrap_or(30)
40}
41
42pub const DEFAULT_SSR_CACHE_SECS: u64 = 3600;
44
45pub fn parse_bool_value(value: Option<&str>, default: bool) -> bool {
46 match value.map(str::trim) {
47 Some(value)
48 if value.eq_ignore_ascii_case("1")
49 || value.eq_ignore_ascii_case("true")
50 || value.eq_ignore_ascii_case("yes")
51 || value.eq_ignore_ascii_case("on") =>
52 {
53 true
54 }
55 Some(value)
56 if value.eq_ignore_ascii_case("0")
57 || value.eq_ignore_ascii_case("false")
58 || value.eq_ignore_ascii_case("no")
59 || value.eq_ignore_ascii_case("off") =>
60 {
61 false
62 }
63 _ => default,
64 }
65}
66
67pub fn parse_env_bool(name: &str, default: bool) -> bool {
69 let value = std::env::var(name).ok();
70 parse_bool_value(value.as_deref(), default)
71}
72
73pub fn parse_ssr_cache_secs() -> u64 {
75 std::env::var("SSR_CACHE_SECS")
76 .ok()
77 .and_then(|value| value.trim().parse().ok())
78 .unwrap_or(DEFAULT_SSR_CACHE_SECS)
79}
80
81pub fn escape_like_pattern(s: &str) -> String {
86 s.replace('\\', "\\\\")
87 .replace('%', "\\%")
88 .replace('_', "\\_")
89}
90#[cfg(test)]
91mod tests {
92 use super::parse_bool_value;
93
94 #[test]
95 fn parse_bool_value_accepts_common_spellings() {
96 for value in ["1", "true", "yes", "on"] {
97 assert!(parse_bool_value(Some(value), false));
98 }
99 for value in ["0", "false", "no", "off"] {
100 assert!(!parse_bool_value(Some(value), true));
101 }
102 }
103
104 #[test]
105 fn parse_bool_value_uses_default_for_unknown_values() {
106 assert!(parse_bool_value(Some("maybe"), true));
107 assert!(!parse_bool_value(Some("maybe"), false));
108 assert!(parse_bool_value(None, true));
109 assert!(!parse_bool_value(None, false));
110 }
111}