|
1 | | -use std::time::Duration; |
2 | | - |
| 1 | +use conductor_config::plugins::{CorsListStringConfig, CorsPluginConfig, CorsStringConfig}; |
3 | 2 | use http::{HeaderValue, Method}; |
4 | | -use schemars::JsonSchema; |
5 | | -use serde::{Deserialize, Deserializer}; |
6 | 3 | use tower_http::cors::{Any, CorsLayer}; |
7 | 4 | use tracing::{debug, info}; |
8 | 5 |
|
9 | 6 | use super::core::Plugin; |
10 | 7 |
|
11 | 8 | pub struct CorsPlugin(pub CorsPluginConfig); |
12 | 9 |
|
13 | | -#[derive(Deserialize, Debug, Clone, JsonSchema)] |
14 | | -#[serde(untagged)] |
15 | | -pub enum CorsListStringConfig { |
16 | | - #[serde(deserialize_with = "deserialize_wildcard")] |
17 | | - Wildcard, |
18 | | - List(Vec<String>), |
19 | | -} |
20 | | - |
21 | | -#[derive(Deserialize, Debug, Clone, JsonSchema)] |
22 | | -#[serde(untagged)] |
23 | | -pub enum CorsStringConfig { |
24 | | - #[serde(deserialize_with = "deserialize_wildcard")] |
25 | | - Wildcard, |
26 | | - Value(String), |
27 | | -} |
28 | | - |
29 | | -fn deserialize_wildcard<'de, D>(deserializer: D) -> Result<(), D::Error> |
30 | | -where |
31 | | - D: Deserializer<'de>, |
32 | | -{ |
33 | | - #[derive(Deserialize)] |
34 | | - enum Helper { |
35 | | - #[serde(rename = "*")] |
36 | | - Wildcard, |
37 | | - } |
38 | | - |
39 | | - Helper::deserialize(deserializer).map(|_| ()) |
40 | | -} |
41 | | - |
42 | | -#[derive(Deserialize, Debug, Clone, JsonSchema)] |
43 | | -pub struct CorsPluginConfig { |
44 | | - /// Access-Control-Allow-Credentials (default: false) |
45 | | - allow_credentials: Option<bool>, |
46 | | - /// Access-Control-Allow-Methods (default: Any) |
47 | | - allowed_methods: Option<CorsListStringConfig>, |
48 | | - /// Access-Control-Allow-Origin (default: Any) |
49 | | - allowed_origin: Option<CorsStringConfig>, |
50 | | - /// Access-Control-Allow-Headers (default: Any) |
51 | | - allowed_headers: Option<CorsListStringConfig>, |
52 | | - /// Access-Control-Allow-Origin (default: false) |
53 | | - allow_private_network: Option<bool>, |
54 | | - /// Access-Control-Max-Age (default: empty) |
55 | | - max_age: Option<Duration>, |
56 | | -} |
57 | | - |
58 | | -impl CorsPluginConfig { |
59 | | - pub fn is_empty_config(&self) -> bool { |
60 | | - self.allow_credentials.is_none() |
61 | | - && self.allowed_methods.is_none() |
62 | | - && self.allowed_origin.is_none() |
63 | | - && self.allowed_headers.is_none() |
64 | | - && self.allow_private_network.is_none() |
65 | | - && self.max_age.is_none() |
66 | | - } |
67 | | -} |
68 | | - |
69 | 10 | #[async_trait::async_trait] |
70 | 11 | impl Plugin for CorsPlugin { |
71 | 12 | fn on_endpoint_creation( |
|
0 commit comments