| 1 |
# Component composition example |
| 2 |
|
| 3 |
This directory contains an example of how to use `wasm-compose` |
| 4 |
to compose a component from other components. |
| 5 |
|
| 6 |
## Directory layout |
| 7 |
|
| 8 |
There are three subdirectories in this example: |
| 9 |
|
| 10 |
* `service` - a service component that responds with the original request body. |
| 11 |
* `middleware` - a middleware component that compresses response bodies. |
| 12 |
* `server` - a custom HTTP server that instantiates a service component for |
| 13 |
each HTTP request. |
| 14 |
|
| 15 |
## Overview |
| 16 |
|
| 17 |
The server will listen for `POST` requests at `http://localhost:8080`. |
| 18 |
|
| 19 |
When it receives a request, the server will instantiate a service component |
| 20 |
and forward it the request. |
| 21 |
|
| 22 |
Each service implements a `handler` interface defined in `service.wit` as: |
| 23 |
|
| 24 |
```wit |
| 25 |
interface handler { |
| 26 |
record request { |
| 27 |
headers: list<tuple<list<u8>, list<u8>>>, |
| 28 |
body: list<u8>, |
| 29 |
} |
| 30 |
|
| 31 |
record response { |
| 32 |
headers: list<tuple<list<u8>, list<u8>>>, |
| 33 |
body: list<u8> |
| 34 |
} |
| 35 |
|
| 36 |
enum error { |
| 37 |
bad-request, |
| 38 |
} |
| 39 |
|
| 40 |
execute: func(req: request) -> result<response, error> |
| 41 |
} |
| 42 |
``` |
| 43 |
|
| 44 |
A service handler will be passed a `request` containing only the headers and |
| 45 |
body and respond with a `response` containing only the headers and body. |
| 46 |
|
| 47 |
Note that this is an overly-simplistic (and inefficient) interface for |
| 48 |
describing HTTP request processing. |
| 49 |
|
| 50 |
### Execution flow |
| 51 |
|
| 52 |
The example implements a `service` component that initially executes like |
| 53 |
this: |
| 54 |
|
| 55 |
```mermaid |
| 56 |
sequenceDiagram |
| 57 |
participant server (native) |
| 58 |
participant service (wasm) |
| 59 |
server (native)->>service (wasm): execute(<request>) |
| 60 |
Note right of service (wasm): echo request body |
| 61 |
service (wasm)->>server (native): uncompressed response |
| 62 |
``` |
| 63 |
|
| 64 |
With a simple change to how the `service` component is composed, we can |
| 65 |
inject a `middleware` component that will gzip compress the response body, |
| 66 |
altering the execution flow to this: |
| 67 |
|
| 68 |
```mermaid |
| 69 |
sequenceDiagram |
| 70 |
participant server (native) |
| 71 |
participant middleware (wasm) |
| 72 |
participant service (wasm) |
| 73 |
server (native)->>middleware (wasm): execute(<request>) |
| 74 |
middleware (wasm)->>service (wasm): execute(<request>) |
| 75 |
Note right of service (wasm): echo request body |
| 76 |
service (wasm)->>middleware (wasm): uncompressed response |
| 77 |
middleware (wasm)->>server (native): compressed response |
| 78 |
``` |
| 79 |
|
| 80 |
All this without having to rebuild any of the original components! |
| 81 |
|
| 82 |
## Getting started |
| 83 |
|
| 84 |
The components in this example will be built with [`cargo component`](https://github.com/bytecodealliance/cargo-component). |
| 85 |
|
| 86 |
Follow the [installation instructions](https://github.com/bytecodealliance/cargo-component#installation) |
| 87 |
to install `cargo component` locally. |
| 88 |
|
| 89 |
> *Note*: `cargo component` is under active development. This example has been tested |
| 90 |
> with git sha [`df2eb26`](https://github.com/bytecodealliance/cargo-component/commit/df2eb2633fa6d0c234372fb0471b8e9867d135c6) |
| 91 |
> and may not work with other versions of `cargo component`. |
| 92 |
|
| 93 |
Additionally, it is assumed that `wasm-tools` has been installed from the |
| 94 |
root of this repository. |
| 95 |
|
| 96 |
## Building the components |
| 97 |
|
| 98 |
To build the `service` component, use `cargo component build`: |
| 99 |
|
| 100 |
```sh |
| 101 |
cd service |
| 102 |
cargo component build --release |
| 103 |
``` |
| 104 |
|
| 105 |
To build the `middleware` component, use `cargo component build`: |
| 106 |
|
| 107 |
```sh |
| 108 |
cd middleware |
| 109 |
cargo component build --release |
| 110 |
``` |
| 111 |
|
| 112 |
## Running the server |
| 113 |
|
| 114 |
Initially, we will run the server with the `service` component that responds |
| 115 |
with an uncompressed response body. |
| 116 |
|
| 117 |
The server can be run with `cargo run`: |
| 118 |
|
| 119 |
```sh |
| 120 |
cd server |
| 121 |
cargo run --release -- ../service/target/wasm32-wasi/release/svc.wasm |
| 122 |
``` |
| 123 |
|
| 124 |
This will start a HTTP server that listens at `http://localhost:8080`. |
| 125 |
|
| 126 |
## Sending a request |
| 127 |
|
| 128 |
Use `curl` to send a request to the service: |
| 129 |
|
| 130 |
``` |
| 131 |
$ curl localhost:8080 -d 'Hello, world!' -H "Content-Type: text/plain" -v |
| 132 |
``` |
| 133 |
|
| 134 |
This should output something like this: |
| 135 |
|
| 136 |
``` |
| 137 |
* Trying 127.0.0.1:8080... |
| 138 |
* connect to 127.0.0.1 port 8080 failed: Connection refused |
| 139 |
* Trying ::1:8080... |
| 140 |
* Connected to localhost (::1) port 8080 (#0) |
| 141 |
> POST / HTTP/1.1 |
| 142 |
> Host: localhost:8080 |
| 143 |
> User-Agent: curl/7.79.1 |
| 144 |
> Accept: */* |
| 145 |
> Content-Type: text/plain |
| 146 |
> Content-Length: 13 |
| 147 |
> |
| 148 |
* Mark bundle as not supporting multiuse |
| 149 |
< HTTP/1.1 200 OK |
| 150 |
< content-length: 35 |
| 151 |
< content-type: text/plain |
| 152 |
< date: Fri, 05 Aug 2022 01:39:43 GMT |
| 153 |
< |
| 154 |
* Connection #0 to host localhost left intact |
| 155 |
The request body was: Hello, world! |
| 156 |
``` |
| 157 |
|
| 158 |
Note that the response body matches the request body, but it was not |
| 159 |
compressed. |
| 160 |
|
| 161 |
## Composing with a middleware |
| 162 |
|
| 163 |
If we want to instead compress the response bodies for the service, we can |
| 164 |
easily compose a new component that sends requests through the `middleware` |
| 165 |
component without rebuilding any of the previously built components. |
| 166 |
|
| 167 |
The `server/config.yml` file contains the configuration needed to compose a new |
| 168 |
component from the `service` and `middleware` components. |
| 169 |
|
| 170 |
Run `wasm-compose` to compose the new component: |
| 171 |
|
| 172 |
```sh |
| 173 |
cd server |
| 174 |
wasm-tools compose -c config.yml -o service.wasm ../middleware/target/wasm32-wasi/release/middleware.wasm |
| 175 |
``` |
| 176 |
|
| 177 |
This results in a new `service.wasm` in the `server` directory where the |
| 178 |
`middleware` component is now used. |
| 179 |
|
| 180 |
## Running the server again |
| 181 |
|
| 182 |
If you haven't already, stop the currently running server by pressing `ctrl-c`. |
| 183 |
|
| 184 |
Start the server again with `cargo run` with the newly composed component: |
| 185 |
|
| 186 |
```sh |
| 187 |
cd server |
| 188 |
cargo run --release -- service.wasm |
| 189 |
``` |
| 190 |
|
| 191 |
The server should again be listening for requests at `http://localhost:8080`. |
| 192 |
|
| 193 |
## Sending one more request |
| 194 |
|
| 195 |
Use `curl` to send a request to the service, but this time pass the |
| 196 |
`--compressed` option: |
| 197 |
|
| 198 |
``` |
| 199 |
$ curl localhost:8080 -d 'Hello, world!' -H "Content-Type: text/plain" -v --compressed |
| 200 |
``` |
| 201 |
|
| 202 |
This should output something like this: |
| 203 |
|
| 204 |
``` |
| 205 |
* Trying 127.0.0.1:8080... |
| 206 |
* connect to 127.0.0.1 port 8080 failed: Connection refused |
| 207 |
* Trying ::1:8080... |
| 208 |
* Connected to localhost (::1) port 8080 (#0) |
| 209 |
> POST / HTTP/1.1 |
| 210 |
> Host: localhost:8080 |
| 211 |
> User-Agent: curl/7.79.1 |
| 212 |
> Accept: */* |
| 213 |
> Accept-Encoding: deflate, gzip |
| 214 |
> Content-Type: text/plain |
| 215 |
> Content-Length: 13 |
| 216 |
> |
| 217 |
* Mark bundle as not supporting multiuse |
| 218 |
< HTTP/1.1 200 OK |
| 219 |
< content-encoding: gzip |
| 220 |
< content-length: 58 |
| 221 |
< content-type: text/plain |
| 222 |
< date: Fri, 05 Aug 2022 01:47:59 GMT |
| 223 |
< |
| 224 |
* Connection #0 to host localhost left intact |
| 225 |
The request body was: Hello, world! |
| 226 |
``` |
| 227 |
|
| 228 |
Note the response used `gzip` for the content encoding. |
| 229 |
|
| 230 |
With such a small response body, the compressed size is actually _bigger_ |
| 231 |
than the uncompressed size. |
| 232 |
|
| 233 |
## Towards the future |
| 234 |
|
| 235 |
This example will hopefully continue to evolve with the component model |
| 236 |
proposal. |
| 237 |
|
| 238 |
Notably, requests and responses will be modeled as _resources_ once the |
| 239 |
proposal (and Wasmtime) supports them. |
| 240 |
|
| 241 |
This will enable the service to incrementally read data from the request |
| 242 |
body instead of passing it all in at once, for example. |
| 243 |
|
| 244 |
Additionally, the component model proposal will some day support |
| 245 |
asynchronous calls. This will enable the service to be invoked |
| 246 |
asynchronously rather than blocking the request thread like it does today. |