-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #184 from Enraged-Dun-Cookie-Development-Team/fix-…
…官网跨域支持 修复一堆之前合并误删的代码
- Loading branch information
Showing
16 changed files
with
105 additions
and
148 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
use axum_starter::{prepare, PrepareMiddlewareEffect}; | ||
use http::{HeaderValue, Method}; | ||
use tower_http::cors::CorsLayer; | ||
|
||
pub trait CorsConfigTrait { | ||
fn allow_origins(&self) -> Vec<HeaderValue>; | ||
|
||
fn allow_methods(&self) -> Vec<Method>; | ||
} | ||
|
||
#[prepare(PrepareCors)] | ||
pub fn prepare_cors<T: CorsConfigTrait>(cfg: &T) -> CorsMiddleware { | ||
CorsMiddleware( | ||
CorsLayer::new() | ||
.allow_origin(cfg.allow_origins()) | ||
.allow_methods(cfg.allow_methods()), | ||
) | ||
} | ||
|
||
pub struct CorsMiddleware(CorsLayer); | ||
|
||
impl<S> PrepareMiddlewareEffect<S> for CorsMiddleware { | ||
type Middleware = CorsLayer; | ||
|
||
fn take(self, _: &mut axum_starter::StateCollector) -> Self::Middleware { | ||
self.0 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
pub mod cors; | ||
pub mod panic_report; | ||
pub mod tracing_request; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
use http::{HeaderValue, Method}; | ||
use serde::{Deserialize, Deserializer}; | ||
|
||
use crate::bootstrap::middleware::cors::CorsConfigTrait; | ||
|
||
#[derive(Deserialize, Debug, Clone, Default)] | ||
pub struct CorsConfigImpl { | ||
#[serde(alias = "origins", deserialize_with = "de_origins")] | ||
allow_origins: Vec<HeaderValue>, | ||
#[serde(alias = "methods", deserialize_with = "de_methods")] | ||
allow_methods: Vec<Method>, | ||
} | ||
|
||
impl CorsConfigTrait for CorsConfigImpl { | ||
fn allow_origins(&self) -> Vec<HeaderValue> { self.allow_origins.clone() } | ||
|
||
fn allow_methods(&self) -> Vec<Method> { self.allow_methods.clone() } | ||
} | ||
|
||
fn de_origins<'de, D: Deserializer<'de>>( | ||
de: D, | ||
) -> Result<Vec<HeaderValue>, D::Error> { | ||
let vec = Vec::<String>::deserialize(de)?; | ||
vec.iter().map(|path| path.parse()).try_fold( | ||
Vec::with_capacity(vec.len()), | ||
|mut vec, value| { | ||
vec.push(value.map_err(serde::de::Error::custom)?); | ||
Ok(vec) | ||
}, | ||
) | ||
} | ||
|
||
fn de_methods<'de, D: Deserializer<'de>>( | ||
de: D, | ||
) -> Result<Vec<Method>, D::Error> { | ||
let vec = Vec::<String>::deserialize(de)?; | ||
vec.iter() | ||
.map(|method| Method::try_from(method.as_str())) | ||
.try_fold(Vec::with_capacity(vec.len()), |mut vec, value| { | ||
vec.push(value.map_err(serde::de::Error::custom)?); | ||
Ok(vec) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters