Skip to content

Commit

Permalink
Diagrams for ProtocolStack, HandlerAspect and Middleware (#2826)
Browse files Browse the repository at this point in the history
middleware diagrams.

Co-authored-by: Nabil Abdel-Hafeez <[email protected]>
  • Loading branch information
khajavi and 987Nabil authored May 16, 2024
1 parent d14be1e commit ac358fa
Show file tree
Hide file tree
Showing 7 changed files with 982 additions and 2 deletions.
249 changes: 249 additions & 0 deletions docs/reference/aop/handler-aspect.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions docs/reference/aop/handler_aspect.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ A `HandlerAspect` is a wrapper around `ProtocolStack` with the two following fea

- It is specialized to work with an output context `CtxOut` that can be passed through the middleware stack. This allows each layer to add its output context to the transformation process. So the `CtxOut` will be a tuple of all the output contexts that each layer in the stack has added. These output contexts are useful when we are writing middleware that needs to pass some information, which is the result of some computation based on the input request, to the handler that is at the end of the middleware stack.

The diagram below illustrates how `HandlerAspect` works:

<div style={{textAlign: 'center', margin: '10px'}}>

![HandlerAspect Diagram](handler-aspect.svg)

</div>

Now, we are ready to see the definition of `HandlerAspect`:

```scala
Expand Down
8 changes: 8 additions & 0 deletions docs/reference/aop/middleware.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ trait Middleware[-UpperEnv] { self =>

This abstraction allows middleware to engage with the `Routes` environment, and also the ability to tweak existing routes or add/remove routes as needed.

The diagram below illustrates how `Middleware` works:

<div style={{textAlign: 'center', margin: '10px'}}>

![Middleware Diagram](middleware.svg)

</div>

## Usage

The `@@` operator attaches middleware to routes and HTTP applications. The example below shows a middleware attached to an `Routes`:
Expand Down
244 changes: 244 additions & 0 deletions docs/reference/aop/middleware.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
194 changes: 194 additions & 0 deletions docs/reference/aop/multiple-protocol-stack.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 25 additions & 2 deletions docs/reference/aop/protocol-stack.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,35 @@ The `ProtocolStack` data type has 5 type parameters, one for the ZIO environment

- **Incoming Output**: This refers to the data leaving the middleware and heading towards the server or the next middleware in the chain. This could include modified request data or additional metadata added by the middleware.

**Outgoing Input**: This refers to data coming into the middleware from the handler or the previous middleware in the chain. It typically includes the HTTP response from the server, including headers, status codes, and the response body.
- **Outgoing Input**: This refers to data coming into the middleware from the handler or the previous middleware in the chain. It typically includes the HTTP response from the server, including headers, status codes, and the response body.

**Outgoing Output**: This refers to data leaving the middleware and heading back to the client. It could include modified response data, additional headers, or any other transformations applied by the middleware.
- **Outgoing Output**: This refers to data leaving the middleware and heading back to the client. It could include modified response data, additional headers, or any other transformations applied by the middleware.

A `ProtocolStack` can be created by combining multiple middleware functions using the `++` operator. Using the `++` operator, we can stack multiple middleware functions on top of each other to create a composite middleware that applies each middleware in the order they are stacked.

The diagram below illustrates how `ProtocolStack` works:

<div style={{textAlign: 'center', margin: '10px'}}>

![ProtocolStack Diagram](protocol-stack.svg)

</div>

Here is the flow of data through the `ProtocolStack`:

1. The incoming input `II` is transformed by the first layer of the protocol stack to produce the incoming output `IO`.
2. The incoming output `IO` is passed to the next layer of the protocol stack (if exists) to produce a new incoming output. This process continues until all layers have been applied.
3. The incoming output `IO` is passed to the handler, which is the last layer where the actual processing of the request takes place. The handler processes the incoming output and produces the outgoing input `OI`.
4. The outgoing input `OI` is passed to the last layer of the protocol stack to produce the outgoing output `OO`.
5. The outgoing output `OO` is passed to the previous layer of the protocol stack (if exists) to produce a new outgoing output. This process continues until all layers have been applied.
6. The outgoing output `OO` is returned as the final result of the protocol stack.

<div style={{textAlign: 'center', margin: '10px'}}>

![Multiple ProtocolStack Diagram](multiple-protocol-stack.svg)

</div>

## Creating a ProtocolStack

There are several ways to create a `ProtocolStack`. One simple way is to start with an `identity` stack, which is a protocol stack that does nothing and simply passes the inputs to the outputs unchanged. Then, we can modify it by mapping over the inputs and outputs to apply transformations:
Expand Down
254 changes: 254 additions & 0 deletions docs/reference/aop/protocol-stack.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit ac358fa

Please sign in to comment.