|
1 | 1 | //! Non-blocking implementation, requires the `async` feature.
|
2 | 2 |
|
3 |
| -use crate::{Request, Response, Error, Result}; |
| 3 | +use crate::{Context, Error, Request, Response, Result}; |
4 | 4 | use async_trait::async_trait;
|
5 | 5 |
|
6 | 6 | #[async_trait]
|
7 | 7 | /// Trait for async services that maybe handle a request.
|
8 |
| -pub trait Service { |
| 8 | +pub trait Service<T: Send + Send> { |
9 | 9 | /// See [Service](crate::Service) for more information.
|
10 |
| - async fn handle(&self, req: &mut Request) -> Result<Option<Response>>; |
| 10 | + async fn handle( |
| 11 | + &self, |
| 12 | + request: &mut Request, |
| 13 | + ctx: &Context<T>, |
| 14 | + ) -> Result<Option<Response>>; |
11 | 15 | }
|
12 | 16 |
|
13 |
| -/// Call async services in order and return the first response message. |
14 |
| -/// |
15 |
| -/// See [handle](crate::handle) for more information. |
16 |
| -pub async fn handle<'a>( |
17 |
| - services: &'a Vec<&'a Box<dyn Service>>, |
18 |
| - request: &mut Request, |
19 |
| -) -> Result<Response> { |
20 |
| - for service in services { |
21 |
| - if let Some(result) = service.handle(request).await? { |
22 |
| - return Ok(result); |
23 |
| - } |
| 17 | +/// Serve requests. |
| 18 | +pub struct Server<'a, T: Send + Sync> { |
| 19 | + /// Services that the server should invoke for every request. |
| 20 | + services: Vec<&'a Box<dyn Service<T>>>, |
| 21 | +} |
| 22 | + |
| 23 | +impl<'a, T: Send + Sync> Server<'a, T> { |
| 24 | + /// Create a new server. |
| 25 | + pub fn new(services: Vec<&'a Box<dyn Service<T>>>) -> Self { |
| 26 | + Self {services} |
24 | 27 | }
|
25 | 28 |
|
26 |
| - let err = Error::MethodNotFound { |
27 |
| - name: request.method().to_string(), |
28 |
| - id: request.id.clone(), |
29 |
| - }; |
| 29 | + /// Call services in order and return the first response message. |
| 30 | + /// |
| 31 | + /// If no services match the incoming request this will |
| 32 | + /// return a `Error::MethodNotFound`. |
| 33 | + pub(crate) async fn handle( |
| 34 | + &self, |
| 35 | + request: &mut Request, |
| 36 | + ctx: &Context<T>, |
| 37 | + ) -> Result<Response> { |
| 38 | + for service in self.services.iter() { |
| 39 | + if let Some(result) = service.handle(request, ctx).await? { |
| 40 | + return Ok(result); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + let err = Error::MethodNotFound { |
| 45 | + name: request.method().to_string(), |
| 46 | + id: request.id.clone(), |
| 47 | + }; |
30 | 48 |
|
31 |
| - Ok((request, err).into()) |
32 |
| -} |
| 49 | + Ok((request, err).into()) |
| 50 | + } |
33 | 51 |
|
34 |
| -/// Infallible async service handler, errors are automatically converted to responses. |
35 |
| -pub async fn serve<'a>( |
36 |
| - services: &'a Vec<&'a Box<dyn Service>>, |
37 |
| - request: &mut Request, |
38 |
| -) -> Response { |
39 |
| - match handle(services, request).await { |
40 |
| - Ok(response) => response, |
41 |
| - Err(e) => e.into(), |
| 52 | + /// Infallible service handler, errors are automatically converted to responses. |
| 53 | + pub async fn serve( |
| 54 | + &self, |
| 55 | + request: &mut Request, |
| 56 | + ctx: &Context<T>, |
| 57 | + ) -> Response { |
| 58 | + match self.handle(request, ctx).await { |
| 59 | + Ok(response) => response, |
| 60 | + Err(e) => e.into(), |
| 61 | + } |
42 | 62 | }
|
43 | 63 | }
|
0 commit comments