-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2de7cdc
commit b68d97d
Showing
6 changed files
with
89 additions
and
50 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
use super::builder::RouterBuilder; | ||
|
||
pub trait ExtendRouter<I, O, E> { | ||
fn extend(&mut self, router: RouterBuilder<I, O, E>) -> RouterBuilder<I, O, E>; | ||
} | ||
|
||
impl<I, O, E, F: Fn(RouterBuilder<I, O, E>) -> RouterBuilder<I, O, E>> ExtendRouter<I, O, E> for F { | ||
fn extend(&mut self, router: RouterBuilder<I, O, E>) -> RouterBuilder<I, O, E> { | ||
self(router) | ||
} | ||
} | ||
|
||
pub fn extend_fn<I, O, E, F: Fn(RouterBuilder<I, O, E>) -> RouterBuilder<I, O, E>>(f: F) -> impl ExtendRouter<I, O, E> { | ||
f | ||
} |
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 |
---|---|---|
@@ -1,24 +1,39 @@ | ||
use std::sync::Arc; | ||
|
||
use common::http::router::extend::{extend_fn, ExtendRouter}; | ||
use common::http::router::middleware::Middleware; | ||
use common::http::RouteError; | ||
use common::make_response; | ||
use hyper::body::Incoming; | ||
use hyper::http::header; | ||
use serde_json::json; | ||
|
||
use crate::api::error::ApiError; | ||
use crate::api::Body; | ||
use crate::global::ApiGlobal; | ||
|
||
pub fn cors_middleware<G: ApiGlobal>(_: &Arc<G>) -> Middleware<Body, RouteError<ApiError>> { | ||
Middleware::post(|mut resp| async move { | ||
resp.headers_mut() | ||
.insert(header::ACCESS_CONTROL_ALLOW_ORIGIN, "*".parse().unwrap()); | ||
resp.headers_mut() | ||
.insert(header::ACCESS_CONTROL_ALLOW_METHODS, "GET, POST, OPTIONS".parse().unwrap()); | ||
resp.headers_mut().insert( | ||
header::ACCESS_CONTROL_ALLOW_HEADERS, | ||
"Content-Type, Authorization".parse().unwrap(), | ||
); | ||
pub fn cors_middleware<G: ApiGlobal>(_: &Arc<G>) -> impl ExtendRouter<Incoming, Body, RouteError<ApiError>> { | ||
extend_fn(|router| { | ||
router | ||
.middleware(Middleware::post(|mut resp| async move { | ||
resp.headers_mut() | ||
.insert(header::ACCESS_CONTROL_ALLOW_ORIGIN, "*".parse().unwrap()); | ||
resp.headers_mut() | ||
.insert(header::ACCESS_CONTROL_ALLOW_METHODS, "GET, POST, OPTIONS".parse().unwrap()); | ||
resp.headers_mut().insert( | ||
header::ACCESS_CONTROL_ALLOW_HEADERS, | ||
"Content-Type, Authorization".parse().unwrap(), | ||
); | ||
|
||
Ok(resp) | ||
Ok(resp) | ||
})) | ||
.options("/*", |_| async move { | ||
Ok(make_response!( | ||
hyper::StatusCode::OK, | ||
json!({ | ||
"success": true, | ||
}) | ||
)) | ||
}) | ||
}) | ||
} |
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