1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
use std::str::FromStr;
use thiserror::Error;
use tower_http::cors::AllowOrigin;
#[macro_export]
macro_rules! env {
() => {};
($var:ident: $($tt:tt)*) => { $crate::service_util_paste! {
pub const $var: &str = stringify!($var);
fn [<has_set_ $var:lower>]() -> bool { [<$var _VALUE>].read().unwrap().is_some() }
$crate::env! { @ $var $($tt)* }
} };
(@ $var:ident Option<$ty:ty> $(, $($tt:tt)*)?) => { $crate::service_util_paste! {
$crate::service_util_lazy_static! {
static ref [<$var _VALUE>]: std::sync::Arc<std::sync::RwLock<Option<Result<Option<$ty>, $crate::EnvError>>>> = std::sync::Arc::new(std::sync::RwLock::new(None));
}
pub fn [<$var:lower>]() -> Result<Option<$ty>, $crate::EnvError> {
if [<has_set_ $var:lower>]() {
[<$var _VALUE>].read().unwrap().as_ref().unwrap().clone()
} else {
let res = $crate::service_util_opt_env($var);
let mut write_lock = [<$var _VALUE>].write().unwrap();
*write_lock = Some(res.clone());
res
}
}
$($crate::env! { $($tt)* })?
} };
(@ $var:ident $ty:ty $(, $($tt:tt)*)?) => { $crate::service_util_paste! {
$crate::service_util_lazy_static! {
static ref [<$var _VALUE>]: std::sync::Arc<std::sync::RwLock<Option<Result<$ty, $crate::EnvError>>>> = std::sync::Arc::new(std::sync::RwLock::new(None));
}
pub fn [<$var:lower>]() -> Result<$ty, $crate::EnvError> {
if [<has_set_ $var:lower>]() {
[<$var _VALUE>].read().unwrap().as_ref().unwrap().clone()
} else {
let res = $crate::service_util_env($var);
let mut write_lock = [<$var _VALUE>].write().unwrap();
*write_lock = Some(res.clone());
res
}
}
$($crate::env! { $($tt)* })?
} };
(@ $var:ident $ty:ty = $expr:expr $(, $($tt:tt)*)?) => { $crate::service_util_paste! {
$crate::service_util_lazy_static! {
static ref [<$var _VALUE>]: std::sync::Arc<std::sync::RwLock<Option<Result<$ty, $crate::EnvError>>>> = std::sync::Arc::new(std::sync::RwLock::new(None));
}
pub fn [<$var:lower>]() -> Result<$ty, $crate::EnvError> {
if [<has_set_ $var:lower>]() {
[<$var _VALUE>].read().unwrap().as_ref().unwrap().clone()
} else {
let res = $crate::service_util_opt_env($var).map(|x| x.unwrap_or_else(|| { $expr }.into()));
let mut write_lock = [<$var _VALUE>].write().unwrap();
*write_lock = Some(res.clone());
res
}
}
$($crate::env! { $($tt)* })?
} };
(@ $var:ident Option<$ty:ty> | $map_fn:path $(, $($tt:tt)*)?) => { $crate::service_util_paste! {
$crate::service_util_lazy_static! {
static ref [<$var _VALUE>]: std::sync::Arc<std::sync::RwLock<Option<Result<Option<$ty>, $crate::EnvError>>>> = std::sync::Arc::new(std::sync::RwLock::new(None));
}
pub fn [<$var:lower>]() -> Result<Option<$ty>, $crate::EnvError> {
if [<has_set_ $var:lower>]() {
[<$var _VALUE>].read().unwrap().as_ref().unwrap().clone()
} else {
let res = $crate::service_util_opt_env($var).map(|x| x.map($map_fn));
let mut write_lock = [<$var _VALUE>].write().unwrap();
*write_lock = Some(res.clone());
res
}
}
$($crate::env! { $($tt)* })?
} };
(@ $var:ident $ty:ty | $map_fn:path $(, $($tt:tt)*)?) => { $crate::service_util_paste! {
$crate::service_util_lazy_static! {
static ref [<$var _VALUE>]: std::sync::Arc<std::sync::RwLock<Option<Result<$ty, $crate::EnvError>>>> = std::sync::Arc::new(std::sync::RwLock::new(None));
}
pub fn [<$var:lower>]() -> Result<$ty, $crate::EnvError> {
if [<has_set_ $var:lower>]() {
[<$var _VALUE>].read().unwrap().as_ref().unwrap().clone()
} else {
let res = $crate::service_util_env($var).map($map_fn);
let mut write_lock = [<$var _VALUE>].write().unwrap();
*write_lock = Some(res.clone());
res
}
}
$($crate::env! { $($tt)* })?
} };
}
#[derive(Clone, Debug, Error)]
pub enum EnvError {
#[error("invalid value for environment variable `{0}`")]
InvalidValue(&'static str),
#[error("missing required environment variable `{0}`")]
Missing(&'static str),
}
pub fn service_util_env<T: FromStr>(var_name: &'static str) -> Result<T, EnvError> {
service_util_opt_env(var_name)?.ok_or(EnvError::Missing(var_name))
}
pub fn service_util_opt_env<T: FromStr>(var_name: &'static str) -> Result<Option<T>, EnvError> {
if let Ok(value) = std::env::var(var_name) {
value.parse().map(Some).or(Err(EnvError::InvalidValue(var_name)))
} else {
Ok(None)
}
}
pub fn parse_allowed_origin(allowed_origin: String) -> AllowOrigin {
if allowed_origin == "*" {
return AllowOrigin::any();
}
let urls = allowed_origin
.split(',')
.map(TryFrom::try_from)
.collect::<Result<Vec<hyper::http::HeaderValue>, _>>()
.unwrap();
AllowOrigin::list(urls)
}