Expand description
Service Layer
§Example
use service_layer_rs::util::FnService;
use service_layer_rs::{Layer, Service, ServiceBuilder};
use std::convert::Infallible;
pub struct LogService<S> {
inner: S,
name: String,
}
impl<S, Request> Service<Request> for LogService<S>
where
S: Service<Request>,
Request: Send + 'static,
{
type Response = S::Response;
type Error = S::Error;
async fn call(
&self,
request: Request,
) -> Result<Self::Response, Self::Error> {
println!("LogService<{}> start", self.name);
let res = self.inner.call(request).await;
println!("LogService<{}> end", self.name);
res
}
}
pub struct LogLayer(pub String);
impl<S> Layer<S> for LogLayer
where
S: Send + Sync + 'static
{
type Service = LogService<S>;
fn layer(self, inner: S) -> Self::Service {
LogService { inner, name: self.0 }
}
}
async fn main() {
let svc = FnService::new(|request: String| async move {
println!("handle: {}", request);
Ok::<_, Infallible>(request)
});
let svc = ServiceBuilder::service(svc)
.layer(LogLayer("Test".to_string()))
.build();
let ss = svc.boxed();
let res: Result<String, Infallible> = ss.call("hello".to_owned()).await;
println!("{:?}", res);
}
Re-exports§
pub use service::BoxService;
pub use service::Service;
Modules§
Structs§
- Service
Builder - Builder types to compose layers and services
Traits§
- Layer
- Decorates a Service