-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[feat]条件跨域请求约束添加 #206
Merged
The head ref may contain hidden characters: "feat-\u4FEE\u6539\u8DE8\u57DF\u4E2D\u95F4\u4EF6"
Merged
[feat]条件跨域请求约束添加 #206
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,110 @@ | ||
use std::{collections::HashSet, sync::Arc}; | ||
|
||
use axum_starter::{prepare, PrepareMiddlewareEffect}; | ||
use http::{HeaderValue, Method}; | ||
use tower_http::cors::CorsLayer; | ||
|
||
use std::{task::Poll}; | ||
|
||
use tower::{Layer, Service}; | ||
use tower_http::cors::{Any, Cors}; | ||
|
||
|
||
pub trait CorsConfigTrait { | ||
fn allow_origins(&self) -> Vec<HeaderValue>; | ||
|
||
fn allow_methods(&self) -> Vec<Method>; | ||
|
||
fn bypass_paths(&self) -> Arc<HashSet<String>>; | ||
} | ||
|
||
#[prepare(PrepareCors)] | ||
pub fn prepare_cors<T: CorsConfigTrait>(cfg: &T) -> CorsMiddleware { | ||
CorsMiddleware( | ||
CorsLayer::new() | ||
.allow_origin(cfg.allow_origins()) | ||
.allow_methods(cfg.allow_methods()), | ||
) | ||
#[derive(Clone)] | ||
pub struct ConditionCors<S> { | ||
bypass_cors: Cors<S>, | ||
default_cors: Cors<S>, | ||
bypass_paths: Arc<HashSet<String>>, | ||
} | ||
|
||
pub struct CorsMiddleware(CorsLayer); | ||
impl<S, Req, Resp> Service<http::Request<Req>> for ConditionCors<S> | ||
where | ||
S: Service<http::Request<Req>, Response = http::Response<Resp>>, | ||
Req: Default, | ||
Resp: Default, | ||
{ | ||
type Response = <Cors<S> as Service<http::Request<Req>>>::Response; | ||
|
||
type Error = <Cors<S> as Service<http::Request<Req>>>::Error; | ||
|
||
impl<S> PrepareMiddlewareEffect<S> for CorsMiddleware { | ||
type Middleware = CorsLayer; | ||
type Future = <Cors<S> as Service<http::Request<Req>>>::Future; | ||
|
||
fn poll_ready( | ||
&mut self, cx: &mut std::task::Context<'_>, | ||
) -> Poll<Result<(), Self::Error>> { | ||
match ( | ||
self.bypass_cors.poll_ready(cx), | ||
self.default_cors.poll_ready(cx), | ||
) { | ||
(Poll::Ready(bypass), Poll::Ready(default)) => { | ||
Poll::Ready(bypass.and_then(|_| default)) | ||
} | ||
(Poll::Ready(_), Poll::Pending) => Poll::Pending, | ||
(Poll::Pending, Poll::Ready(_)) => Poll::Pending, | ||
(Poll::Pending, Poll::Pending) => Poll::Pending, | ||
} | ||
} | ||
|
||
fn call(&mut self, req: http::Request<Req>) -> Self::Future { | ||
let uri = req.uri().path(); | ||
if self.bypass_paths.contains(uri) { | ||
self.bypass_cors.call(req) | ||
} else { | ||
self.default_cors.call(req) | ||
} | ||
} | ||
} | ||
|
||
#[derive(Clone)] | ||
pub struct ConditionCorsLayer { | ||
bypass_cors: CorsLayer, | ||
default_cors: CorsLayer, | ||
bypass_paths: Arc<HashSet<String>> | ||
} | ||
|
||
impl<S: Clone> Layer<S> for ConditionCorsLayer { | ||
type Service = ConditionCors<S>; | ||
|
||
fn layer(&self, inner: S) -> Self::Service { | ||
ConditionCors { | ||
bypass_cors: self.bypass_cors.layer(inner.clone()), | ||
default_cors: self.default_cors.layer(inner), | ||
bypass_paths: self.bypass_paths.clone(), | ||
} | ||
} | ||
} | ||
|
||
pub struct ConditionCorsMiddlewarePrepare<T: CorsConfigTrait>(T); | ||
|
||
impl<S: Clone, T: CorsConfigTrait + 'static> PrepareMiddlewareEffect<S> | ||
for ConditionCorsMiddlewarePrepare<T> | ||
{ | ||
type Middleware = ConditionCorsLayer; | ||
|
||
fn take(self, _: &mut axum_starter::StateCollector) -> Self::Middleware { | ||
self.0 | ||
ConditionCorsLayer { | ||
bypass_cors: CorsLayer::new() | ||
.allow_origin(Any) | ||
.allow_methods(Any), | ||
default_cors: CorsLayer::new() | ||
.allow_origin(self.0.allow_origins()) | ||
.allow_methods(self.0.allow_methods()), | ||
bypass_paths: self.0.bypass_paths(), | ||
} | ||
} | ||
} | ||
|
||
#[prepare(ConditionCorsPrepare)] | ||
pub fn prepare_condition_cors<T: CorsConfigTrait + Clone>( | ||
config: &T, | ||
) -> ConditionCorsMiddlewarePrepare<T> { | ||
ConditionCorsMiddlewarePrepare(config.clone()) | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里建议使用
Arc::clone
来指定调用Arc的拷贝