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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
use std::collections::{BTreeSet, HashSet};
use std::convert::TryFrom;
use std::time::Duration;
use http::{header::HeaderName, HeaderValue};
use tonic::body::BoxBody;
use tonic::transport::NamedService;
use tower_service::Service;
use crate::service::GrpcWeb;
use crate::BoxError;
const DEFAULT_MAX_AGE: Duration = Duration::from_secs(24 * 60 * 60);
const DEFAULT_EXPOSED_HEADERS: [&str; 2] = ["grpc-status", "grpc-message"];
#[derive(Debug, Clone)]
pub struct Config {
pub(crate) allowed_origins: AllowedOrigins,
pub(crate) exposed_headers: HashSet<HeaderName>,
pub(crate) max_age: Option<Duration>,
pub(crate) allow_credentials: bool,
}
#[derive(Debug, Clone)]
pub(crate) enum AllowedOrigins {
Any,
#[allow(clippy::mutable_key_type)]
Only(BTreeSet<HeaderValue>),
}
impl AllowedOrigins {
pub(crate) fn is_allowed(&self, origin: &HeaderValue) -> bool {
match self {
AllowedOrigins::Any => true,
AllowedOrigins::Only(origins) => origins.contains(origin),
}
}
}
impl Config {
pub(crate) fn new() -> Config {
Config {
allowed_origins: AllowedOrigins::Any,
exposed_headers: DEFAULT_EXPOSED_HEADERS
.iter()
.cloned()
.map(HeaderName::from_static)
.collect(),
max_age: Some(DEFAULT_MAX_AGE),
allow_credentials: true,
}
}
pub fn allow_all_origins(self) -> Config {
Self {
allowed_origins: AllowedOrigins::Any,
..self
}
}
pub fn allow_origins<I>(self, origins: I) -> Config
where
I: IntoIterator,
HeaderValue: TryFrom<I::Item>,
{
#[allow(clippy::mutable_key_type)]
let origins = origins
.into_iter()
.map(|v| match TryFrom::try_from(v) {
Ok(uri) => uri,
Err(_) => panic!("invalid origin"),
})
.collect();
Self {
allowed_origins: AllowedOrigins::Only(origins),
..self
}
}
pub fn expose_headers<I>(mut self, headers: I) -> Config
where
I: IntoIterator,
HeaderName: TryFrom<I::Item>,
{
let iter = headers
.into_iter()
.map(|header| match TryFrom::try_from(header) {
Ok(header) => header,
Err(_) => panic!("invalid header"),
});
self.exposed_headers.extend(iter);
self
}
pub fn max_age<T: Into<Option<Duration>>>(self, max_age: T) -> Config {
Self {
max_age: max_age.into(),
..self
}
}
pub fn allow_credentials(self, allow_credentials: bool) -> Config {
Self {
allow_credentials,
..self
}
}
pub fn enable<S>(&self, service: S) -> GrpcWeb<S>
where
S: Service<http::Request<hyper::Body>, Response = http::Response<BoxBody>>,
S: NamedService + Clone + Send + 'static,
S::Future: Send + 'static,
S::Error: Into<BoxError> + Send,
{
tracing::trace!("enabled for {}", S::NAME);
GrpcWeb::new(service, self.clone())
}
}
impl Default for Config {
fn default() -> Self {
Config::new()
}
}