Service pipeline
In the previous post, we described what a Service is and how to define it. Let's check an example: async fn endpoint(req: HttpRequest) -> Result<HttpResponse, Error> { // Load operation fr...

Source: DEV Community
In the previous post, we described what a Service is and how to define it. Let's check an example: async fn endpoint(req: HttpRequest) -> Result<HttpResponse, Error> { // Load operation from the request let oper = load_operation(req).await?; // Execute it using our engine let result = execute(oper).await?; // Convert the result into an HTTP response Ok(into_response(result).await?) } This is structurally a sequence of fallible async transformations. The natural representation is a composable pipeline using an and_then-style combinator, analogous to Result::and_then(): let pipeline = chain(authn) .and_then(load_operation) .and_then(authz) .and_then(execute) .and_then(into_response); The resulting pipeline is itself a Service<HttpRequest, Response = HttpResponse>. Each stage is independently swappable, and insertion is trivial. For example, introducing throttling becomes a purely local change: let pipeline = chain(authn) .and_then(throttling) .and_then(load_operation) .and