forked from cloudwego/volo
-
Notifications
You must be signed in to change notification settings - Fork 0
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
b6fbb8d
commit e06b9fc
Showing
11 changed files
with
957 additions
and
9 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
use std::{convert::Infallible, net::SocketAddr}; | ||
|
||
use bytes::Bytes; | ||
use http::{Response, StatusCode}; | ||
use hyper::body::Incoming; | ||
use motore::service::service_fn; | ||
use serde::{Deserialize, Serialize}; | ||
use volo_http::{ | ||
request::Json, | ||
route::{Route, Router}, | ||
HttpContext, | ||
}; | ||
|
||
async fn hello( | ||
_cx: &mut HttpContext, | ||
_request: Incoming, | ||
) -> Result<Response<&'static str>, Infallible> { | ||
Ok(Response::new("hello, world")) | ||
} | ||
|
||
async fn echo(cx: &mut HttpContext, _request: Incoming) -> Result<Response<Bytes>, Infallible> { | ||
if let Some(echo) = cx.params.get("echo") { | ||
return Ok(Response::new(echo.clone())); | ||
} | ||
Ok(Response::builder() | ||
.status(StatusCode::BAD_REQUEST) | ||
.body(Bytes::new()) | ||
.unwrap()) | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Debug)] | ||
struct Person { | ||
name: String, | ||
age: u8, | ||
phones: Vec<String>, | ||
} | ||
|
||
async fn json( | ||
_cx: &mut HttpContext, | ||
Json(request): Json<Person>, | ||
) -> Result<Response<()>, Infallible> { | ||
let first_phone = request | ||
.phones | ||
.get(0) | ||
.map(|p| p.as_str()) | ||
.unwrap_or("no number"); | ||
println!( | ||
"{} is {} years old, {}'s first phone number is {}", | ||
request.name, request.age, request.name, first_phone | ||
); | ||
Ok(Response::new(())) | ||
} | ||
|
||
#[tokio::main(flavor = "multi_thread")] | ||
async fn main() { | ||
Router::build() | ||
.route("/", Route::builder().get(service_fn(hello)).build()) | ||
.route("/:echo", Route::builder().get(service_fn(echo)).build()) | ||
.route("/user", Route::builder().post(service_fn(json)).build()) | ||
.serve(SocketAddr::from(([127, 0, 0, 1], 3000))) | ||
.await | ||
.unwrap(); | ||
} |
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,74 @@ | ||
use std::{future::Future, marker::PhantomData}; | ||
|
||
use http::Response; | ||
use hyper::body::Incoming; | ||
|
||
use crate::{request::FromRequest, response::RespBody, DynError, HttpContext}; | ||
|
||
pub(crate) struct DispatchService<S, IB, OB> { | ||
inner: S, | ||
_marker: PhantomData<(IB, OB)>, | ||
} | ||
|
||
impl<S, IB, OB> DispatchService<S, IB, OB> { | ||
pub(crate) fn new(service: S) -> Self { | ||
Self { | ||
inner: service, | ||
_marker: PhantomData, | ||
} | ||
} | ||
} | ||
|
||
impl<S, IB, OB> Clone for DispatchService<S, IB, OB> | ||
where | ||
S: Clone, | ||
{ | ||
fn clone(&self) -> Self { | ||
Self { | ||
inner: self.inner.clone(), | ||
_marker: PhantomData, | ||
} | ||
} | ||
} | ||
|
||
unsafe impl<S, IB, OB> Send for DispatchService<S, IB, OB> where S: Send {} | ||
|
||
unsafe impl<S, IB, OB> Sync for DispatchService<S, IB, OB> where S: Sync {} | ||
|
||
impl<S, IB, OB> motore::Service<HttpContext, Incoming> for DispatchService<S, IB, OB> | ||
where | ||
S: motore::Service<HttpContext, IB, Response = Response<OB>> + Send + Sync + 'static, | ||
S::Error: std::error::Error + Send + Sync + 'static, | ||
OB: Into<RespBody>, | ||
IB: FromRequest + Send, | ||
for<'cx> <IB as FromRequest>::FromFut<'cx>: std::marker::Send, | ||
{ | ||
type Response = Response<RespBody>; | ||
|
||
type Error = DynError; | ||
|
||
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 { | ||
match IB::from(&*cx, req).await { | ||
Ok(body) => self | ||
.inner | ||
.call(cx, body) | ||
.await | ||
.map(|resp| { | ||
let (parts, body) = resp.into_parts(); | ||
Response::from_parts(parts, body.into()) | ||
}) | ||
.map_err(|e| Box::new(e) as DynError), | ||
Err(response) => Ok(response), | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.