framework_web_axum/
cors.rs1use framework_core::MultiStringValue;
2use std::sync::LazyLock;
3
4static FIXED: LazyLock<AxumCors> = LazyLock::new(AxumCors::default);
5
6#[derive(Debug, Clone, Default)]
8pub struct AxumCors {
9 pub origin: Option<String>,
11 pub methods: Option<String>,
13 pub headers: Option<String>,
15 pub expose: Option<String>,
17 pub credentials: Option<String>,
19 pub max_age: Option<String>,
21}
22
23impl AxumCors {
24 pub fn fixed() -> &'static Self {
26 &FIXED
27 }
28
29 pub fn apply(&self, headers: &mut MultiStringValue) {
31 let items = [
32 ("access-control-allow-origin", &self.origin),
33 ("access-control-allow-methods", &self.methods),
34 ("access-control-allow-headers", &self.headers),
35 ("access-control-expose-headers", &self.expose),
36 ("access-control-allow-credentials", &self.credentials),
37 ("access-control-max-age", &self.max_age),
38 ];
39 for (name, value) in items {
40 if let Some(value) = value {
41 headers.set(name, value);
42 }
43 }
44 }
45}