-
Notifications
You must be signed in to change notification settings - Fork 183
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
Showing
6 changed files
with
155 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
use http::{Method, Uri}; | ||
|
||
use crate::{response::RespBody, HttpContext}; | ||
#[async_trait::async_trait] | ||
pub trait FromContext: Sized { | ||
type Rejection: Into<RespBody>; | ||
async fn from_context(context: &mut HttpContext) -> Result<Self, Self::Rejection>; | ||
} | ||
#[async_trait::async_trait] | ||
impl<T> FromContext for Option<T> | ||
where | ||
T: FromContext, | ||
{ | ||
type Rejection = &'static str; | ||
|
||
async fn from_context(context: &mut HttpContext) -> Result<Self, Self::Rejection> { | ||
Ok(T::from_context(context).await.ok()) | ||
} | ||
} | ||
|
||
#[async_trait::async_trait] | ||
impl FromContext for Uri { | ||
type Rejection = String; | ||
|
||
async fn from_context(context: &mut HttpContext) -> Result<Uri, Self::Rejection> { | ||
Ok(context.uri.clone()) | ||
} | ||
} | ||
|
||
#[async_trait::async_trait] | ||
impl FromContext for Method { | ||
type Rejection = String; | ||
|
||
async fn from_context(context: &mut HttpContext) -> Result<Method, Self::Rejection> { | ||
Ok(context.method.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
use std::{future::Future, marker::PhantomData}; | ||
|
||
use http::{Response, StatusCode}; | ||
use hyper::body::Incoming; | ||
|
||
use crate::{extract::FromContext, response::RespBody, HttpContext}; | ||
|
||
impl<H, T> Clone for HandlerService<H, T> | ||
where | ||
H: Clone, | ||
{ | ||
fn clone(&self) -> Self { | ||
Self { | ||
h: self.h.clone(), | ||
_mark: PhantomData, | ||
} | ||
} | ||
} | ||
pub trait Handler<'r, T> { | ||
type Future: Future<Output = RespBody> + Send + 'r; | ||
fn call(self, context: &'r mut HttpContext) -> Self::Future; | ||
} | ||
|
||
impl<'r, F, Fut, T1, Res> Handler<'r, T1> for F | ||
where | ||
F: FnOnce(T1) -> Fut + Clone + Send + 'r, | ||
Fut: Future<Output = Res> + Send + 'r, | ||
T1: FromContext + Send + 'r, | ||
Res: Into<RespBody>, | ||
{ | ||
type Future = impl Future<Output = RespBody> + Send + 'r; | ||
|
||
fn call(self, context: &'r mut HttpContext) -> Self::Future { | ||
async move { | ||
let t1 = match T1::from_context(context).await { | ||
Ok(value) => value, | ||
Err(rejection) => return rejection.into(), | ||
}; | ||
self(t1).await.into() | ||
} | ||
} | ||
} | ||
|
||
impl<'r, F, Fut, T1, T2, Res> Handler<'r, (T1, T2)> for F | ||
where | ||
F: FnOnce(T1, T2) -> Fut + Clone + Send + 'r, | ||
Fut: Future<Output = Res> + Send, | ||
T1: FromContext + Send + 'r, | ||
T2: FromContext + Send + 'r, | ||
Res: Into<RespBody>, | ||
{ | ||
type Future = impl Future<Output = RespBody> + Send + 'r; | ||
|
||
fn call(self, context: &'r mut HttpContext) -> Self::Future { | ||
async move { | ||
let t1 = match T1::from_context(context).await { | ||
Ok(value) => value, | ||
Err(rejection) => return rejection.into(), | ||
}; | ||
let t2 = match T2::from_context(context).await { | ||
Ok(value) => value, | ||
Err(rejection) => return rejection.into(), | ||
}; | ||
self(t1, t2).await.into() | ||
} | ||
} | ||
} | ||
pub struct HandlerService<H, T> { | ||
h: H, | ||
_mark: PhantomData<fn(T)>, | ||
} | ||
|
||
impl<H, T> HandlerService<H, T> { | ||
pub fn new(h: H) -> Self { | ||
Self { | ||
h, | ||
_mark: PhantomData, | ||
} | ||
} | ||
} | ||
|
||
impl<H, T> motore::Service<HttpContext, Incoming> for HandlerService<H, T> | ||
where | ||
H: for<'r> Handler<'r, T> + Clone + Send + Sync, | ||
{ | ||
type Response = Response<RespBody>; | ||
type Error = http::Error; | ||
type Future<'cx> = impl Future<Output = Result<Self::Response, Self::Error>> + Send + 'cx | ||
where | ||
HttpContext: 'cx, | ||
Self: 'cx; | ||
|
||
fn call<'cx, 's>(&'s self, cx: &'cx mut HttpContext, _req: Incoming) -> Self::Future<'cx> | ||
where | ||
's: 'cx, | ||
{ | ||
async move { | ||
let resp = self.h.clone().call(cx).await; | ||
Response::builder().status(StatusCode::OK).body(resp) | ||
} | ||
} | ||
} |
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