Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: add the HTTP request documentation. Improve features' docs #180

Merged
merged 5 commits into from
Jul 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions docs/docs/features/_category_.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"label": "Features",
"position": 2,
"link": {
"type": "generated-index"
"type": "doc",
"id": "all"
}
}
}
38 changes: 38 additions & 0 deletions docs/docs/features/all.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Features

Wasm Workers Server provides different features to develop serverless applications. Some of these features are related to the server like the static asset management, while others relate to workers like sending HTTP requests.

For that reason, we usually distinguish between server and worker features:

* **Server features**: customizes or expands Wasm Worker Server capabilities. For example, you can expose static assets by saving these files in a `public` folder.

* **Worker features**: expose new features to the individual workers so they can perform more complex tasks. For example, workers can access to a K/V store or use environment variables.

## Available features

### Server

* [Static assets management](./static-assets.md)
* [Multiple language runtimes](./multiple-language-runtimes.md)

### Workers

* [Key / Value store](./key-value.md)
* [HTTP Requests (fetch)](./http-requests.md)
* [Dynamic routes](./dynamic-routes.md)
* [Environment variables](./environment-variables.md)
* [Mount folders](./mount-folders.md)

## Language compatibility

You can develop workers in different languages. However, not all of them support all features. **The goal is to support all of them**, although there are some constraints that make some features more complex to implement in certain languages.

The following table shows the language compatibility for the different worker functions:

| Language | K/V Store | Environment Variables | Dynamic Routes | Folders | HTTP Requests |
| --- | --- | --- | --- | --- | --- |
| JavaScript | ✅ | ✅ | ✅ | ❌ | ✅ |
| Rust | ✅ | ✅ | ✅ | ✅ | ✅ |
| Go | ✅ | ✅ | ✅ | ✅ | ✅ |
| Ruby | ✅ | ✅ | ✅ | ✅ | ❌ |
| Python | ✅ | ✅ | ✅ | ✅ | ❌ |
14 changes: 12 additions & 2 deletions docs/docs/features/dynamic-routes.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
sidebar_position: 3
sidebar_position: 4
---

# Dynamic routes
Expand Down Expand Up @@ -48,4 +48,14 @@ $ tree .
└── show.js
```

In this case, the `./[resource]/[id]/show.js` worker replies to URLs like `/articles/2/show`.
In this case, the `./[resource]/[id]/show.js` worker replies to URLs like `/articles/2/show`.

## Language compatibility

| Language | Dynamic routes |
| --- | --- |
| JavaScript | ✅ |
| Rust | ✅ |
| Go | ✅ |
| Ruby | ✅ |
| Python | ✅ |
12 changes: 11 additions & 1 deletion docs/docs/features/environment-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,14 @@ JSON_MESSAGE = "Hello 👋! This message comes from an environment variable"
TOKEN = "$TOKEN"
```

This feature allows you to configure environment variables dynamically.
This feature allows you to configure environment variables dynamically.

## Language compatibility

| Language | Environment variables |
| --- | --- |
| JavaScript | ✅ |
| Rust | ✅ |
| Go | ✅ |
| Ruby | ✅ |
| Python | ✅ |
41 changes: 41 additions & 0 deletions docs/docs/features/http-requests.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
sidebar_position: 3
---

# HTTP Requests (fetch)

Often times, workers require to access data from an external resource like a website or an API. This feature allows workers to perform HTTP requests to external resources. It follows the capability-based model, so workers cannot perform any HTTP request until you configure the allowed hosts and HTTP methods.

In this configuration, you are allowing a worker to perform `GET` and `POST` HTTP requests to the [{JSON} Placeholder API](https://jsonplaceholder.typicode.com/):

```toml
name = "fetch"
version = "1"

[features]
[features.http_requests]
allowed_methods = ["GET", "POST"]
allowed_hosts = ["jsonplaceholder.typicode.com"]
```

Now, your worker can perform HTTP requests following those rules.

## Send HTTP requests in different languages

Depending on the language, the different kits expose this feature in a different way. The goal is to use a common API to perform HTTP requests in that language. For example, to perform HTTP requests in JavaScript you can use the [`fetch`](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) API.

Check these guides to perform HTTP requests in the different supported languages:

* [HTTP requests on JavaScript workers](../languages/javascript.md#send-an-http-request-fetch)
* [HTTP requests on Rust workers](../languages/rust.md#send-an-http-request)
* [HTTP requests on Go workers](../languages/go.md#send-an-http-request)

## Language compatibility

| Language | HTTP Requests |
| --- | --- |
| JavaScript | ✅ |
| Rust | ✅ |
| Go | ✅ |
| Ruby | ❌ |
| Python | ❌ |
12 changes: 11 additions & 1 deletion docs/docs/features/key-value.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,14 @@ The worker may access all the data and perform changes over it. Then, a new K/V

## Limitations

A known limitation of the snapshot approach is the data override when concurrent requests are writing to the same namespace. In the future, we will implement a consolidation mechanism or a different store type for applications that require to write intensively.
A known limitation of the snapshot approach is the data override when concurrent requests are writing to the same namespace. In the future, we will implement a consolidation mechanism or a different store type for applications that require to write intensively.

## Language compatibility

| Language | K/V store |
| --- | --- |
| JavaScript | ✅ |
| Rust | ✅ |
| Go | ✅ |
| Ruby | ✅ |
| Python | ✅ |
10 changes: 10 additions & 0 deletions docs/docs/features/mount-folders.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,13 @@ to = "/mnt/assets"
In the previous example, all folders starts with an underscore character (`_`). This folder name convention tells `wws` to ignore any file inside it when detecting available workers.

Note that those folders may include files that `wws` recognizes as workers (like `.js` or `.py`). By prefixing those folders with an `_`, you ensure `wws` won't process those files as workers.

## Language compatibility

| Language | Mount folders |
| --- | --- |
| JavaScript | ❌ |
| Rust | ✅ |
| Go | ✅ |
| Ruby | ✅ |
| Python | ✅ |
4 changes: 2 additions & 2 deletions docs/docs/features/static-assets.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
sidebar_position: 4
sidebar_position: 5
---

# Static assets
Expand Down Expand Up @@ -44,4 +44,4 @@ The `about.html` file will be served as `/about`.

## Index file in public

An `index.html` can be added to the `public` folder and it will be mounted in `/`. Note that static files have a lower priority, so if there's an `/index.js` and `/public/index.html` files, the `/` route will be served by the former.
An `index.html` can be added to the `public` folder and it will be mounted in `/`. Note that static files have a lower priority, so if there's an `/index.js` and `/public/index.html` files, the `/` route will be served by the former.
144 changes: 144 additions & 0 deletions docs/docs/languages/go.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,140 @@ To add a KV store to your worker, follow these steps:

1. Finally, open <http://127.0.0.1:8080/worker-kv> in your browser.

## Send an HTTP request

Wasm Workers allows you to send HTTP requests from your workers. Read more information about this feature in the [HTTP Requests](../features/http-requests.md) section.

To perform HTTP requests from your worker, follow these steps:

1. Create a new Go project:

```shell-session
go mod init fetch
```

1. Add the project dependencies:

```
go get -u github.com/vmware-labs/wasm-workers-server/kits/go/[email protected] \
github.com/tidwall/gjson
```

1. Create a `fetch.go` file with the following contents:

```go title="fetch.go"
package main

import (
"io"
"fmt"
"net/http"

"github.com/vmware-labs/wasm-workers-server/kits/go/worker"

"github.com/tidwall/gjson"
)

func main() {
worker.ServeFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("x-generated-by", "wasm-workers-server")
w.Write([]byte("Hello wasm!"))
})
}
```

1. Then, let's create the `http.Request` instance and pass it to the `worker.SendHttpRequest` method. In this example, we will call the [{JSON} Placeholder API](https://jsonplaceholder.typicode.com/) to retrieve a `Post`. You will read the content of the response using the `gjson` API:

```go title="fetch.go"
package main

import (
"io"
"fmt"
"net/http"

"github.com/vmware-labs/wasm-workers-server/kits/go/worker"

"github.com/tidwall/gjson"
)

func main() {
worker.ServeFunc(func(w http.ResponseWriter, r *http.Request) {
url := "https://jsonplaceholder.typicode.com/posts/1"

// Create the request
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
panic(err)
}

// Send the request
res, err := worker.SendHttpRequest(req)
if err != nil {
msg := fmt.Sprintf("Error when calling the API: %s", err);

// Send the reponse
w.Write([]byte(msg))

return
}

// Read the response and parse the title
resBody, err := io.ReadAll(res.Body)
if err != nil {
msg := fmt.Sprintf("Error when reading the response body: %s", err);

// Send the reponse
w.Write([]byte(msg))

return
}
res.Body.Close()

title := gjson.Get(string(resBody), "title")

w.Header().Set("x-generated-by", "wasm-workers-server")
w.Write([]byte(fmt.Sprintf("Title: %s", title.String())))
})
}
```

1. Compile the project to Wasm ([WASI](https://wasi.dev/)):

```shell-session
tinygo build -o fetch.wasm -target wasi fetch.go
```

1. Create a `fetch.toml` file with the following content. It enables the worker to perform HTTP requests to that host given that, by default, HTTP requests are forbidden.

Note the name of the TOML file must match the name of the worker. In this case we have `fetch.wasm` and `fetch.toml` in the same folder:

```toml title="fetch.toml"
name = "fetch"
version = "1"

[features]
[features.http_requests]
allowed_methods = ["GET"]
allowed_hosts = ["jsonplaceholder.typicode.com"]
```

1. Run your worker with `wws`. If you didn't download the `wws` server yet, check our [Getting Started](../get-started/quickstart.md) guide.

```shell-session
wws .

⚙️ Preparing the project from: .
⚙️ Loading routes from: .
⏳ Loading workers from 1 routes...
✅ Workers loaded in 135.717667ms.
- http://127.0.0.1:8080/fetch
=> ./fetch.wasm
🚀 Start serving requests at http://127.0.0.1:8080
```

1. Finally, open <http://127.0.0.1:8080/fetch> in your browser.

## Dynamic routes

You can define [dynamic routes by adding route parameters to your worker files](../features/dynamic-routes.md) (like `[id].wasm`). To read them in Go, follow these steps:
Expand Down Expand Up @@ -328,4 +462,14 @@ If you prefer, you can configure the environment variable value dynamically by f
* [Basic](https://github.com/vmware-labs/wasm-workers-server/tree/main/examples/go-basic)
* [Counter](https://github.com/vmware-labs/wasm-workers-server/tree/main/examples/go-kv)

## Contributors

The Go kit was originally authored by Mohammed Nafees ([@mnafees](https://github.com/mnafees))

## Feature compatibility

[Workers' features](../features/all.md) that are available in Go:

| [K/V Store](../features/key-value.md) | [Environment Variables](../features/environment-variables.md) | [Dynamic Routes](../features/dynamic-routes.md) | [Folders](../features/mount-folders.md) | [HTTP Requests](../features/http-requests.md) |
| --- | --- | --- | --- | --- |
| ✅ | ✅ | ✅ | ✅ | ✅ |
Loading