Crates.io | service-layer-rs |
lib.rs | service-layer-rs |
version | 0.5.1 |
source | src |
created_at | 2024-09-30 04:26:29.107163 |
updated_at | 2024-10-05 14:28:04.917674 |
description | A simple alternative to the tower service layer, implemented using async trait, making the code more concise and easier to use. |
homepage | |
repository | https://github.com/dancingpeanut/service-layer-rs |
max_upload_size | |
id | 1391318 |
size | 13,574 |
A simple alternative to the tower service layer, implemented using async trait, making the code more concise and easier to use.
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 }
}
}
#[tokio::main]
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);
}
let svc = FnService::new(|request: String| async move {
println!("handle: {}", request);
Ok::<_, Infallible>(request)
});
// Box this service to allow for dynamic dispatch.
let svc = ServiceBuilder::new(svc)
.layer(LogLayer("Test".to_string()))
.build()
.boxed();
let res: Result<String, Infallible> = svc.call("hello".to_owned()).await;
println!("{:?}", res);